Vega: compute tooltip key from a signal - tooltip

Apparently, tooltip object accepts (computes) signals in the values, but not the objects, and does not work well with {[signal]: value} notation.
Is there any alternative way to set the tooltip keys dynamically, based on the vega signal?

for now “hacked” it through custom sanitize function for the vegaTooltip:
function mySanitize(value){
return String(value).replace(/&/g, '&').replace(/</g, '<').replace('METRIC', VIEW.signal('metric'))
};

Related

Format Altair choropleth map legend scale and tooltip

I am trying to make the tool tip 'Percentage' be an actual percent and not a decimal. Even when I include alt.Tooltip('Percentage:Q',format='.2%'), it doesn't seem to work.
Also, I am trying to make the legend scale from 0-100% instead of 40-70%.
Any help would be appreciated!
import altair as alt
from vega_datasets import data
states = alt.topo_feature(data.us_10m.url, 'states')
variable_list = ['Percentage', 'State Name', 'state_id']
alt.Chart(states).mark_geoshape().encode(
color=alt.Color('Percentage:Q', title='Positive NFB', legend=alt.Legend(format=".0%"), scale=alt.Scale(scheme='yellowgreen')),
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]).properties(title="Percentage of People in Households with Positive NFB"
).transform_lookup(
lookup='id',
from_=alt.LookupData(states_positive_NFB, 'state_id', variable_list)
).properties(
width=500,
height=300
).project(
type='albersUsa'
)
Current map:
To change the domain of the color scale, you can pass the domain argument to alt.Scale(): e.g.
alt.Scale(scheme='yellowgreen', domain=[0, 1])
To make the tooltip format appear, you can remove the duplicated tooltip encoding, as the first one appears to be taking precedence. That is, rather than
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]
you should use
tooltip=['State Name:N', alt.Tooltip('Percentage:Q', format='.2%')]

How to call function on all items in a list?

Is there an idiomatic way to apply a function to all items in a list ?
For example, in Python, say we wish to capitalize all strings in a list, we can use a loop :
regimentNames = ['Night Riflemen', 'Jungle Scouts', 'The Dragoons', 'Midnight Revengence', 'Wily Warriors']
# create a variable for the for loop results
regimentNamesCapitalized_f = []
# for every item in regimentNames
for i in regimentNames:
# capitalize the item and add it to regimentNamesCapitalized_f
regimentNamesCapitalized_f.append(i.upper())
But a more concise way is:
capitalizer = lambda x: x.upper()
regimentNamesCapitalized_m = list(map(capitalizer, regimentNames)); regimentNamesCapitalized_m
What is an equivalent way to call a function on all items in a list in Dart ?
If you want to apply a function to all items in a List (or Iterable) and collect the results, Dart provides an Iterable.map function that is equivalent to Python's map:
// Dart
regimentNamesCapitalized_m = regimentNames.map((x) => x.toUpperCase()).toList();
Python also provides list comprehensions, which usually are considered more Pythonic and often are preferred to the functional approach:
# Python
regimentNamesCapitalized_m = [x.upper() for x in regimentNames]
Dart's equivalent of Python's list comprehensions is collection-for:
// Dart
regimentNamesCapitalized_m = [for (var x in regimentNames) x.toUpperCase()];
If you're calling a function for its side-effect and don't care about its return value, you could use Iterable.forEach instead of Iterable.map. In such cases, however, I personally prefer explicit loops:
I think they're more readable by virtue of being more common.
They're more flexible. You can use break or continue to control iteration.
They might be more efficient. .forEach involves an extra function call per iteration to invoke the supplied callback.
The answer seems to be to use anonymous functions, or pass a function to a lists forEach method.
Passing a function:
void capitalise(var string) {
var foo = string.toUpperCase();
print(foo);
}
var list = ['apples', 'bananas', 'oranges'];
list.forEach(capitalise);
Using an anonymous function:
list.forEach((item){
print(item.toUpperCase());
});
If the function is going to be used only in one place, I think its better to use the anonymous function, as it is easy to read what is happening in the list.
If the function is going to be used in multiple places, then its better to pass the function instead of using an anonymous function.

Convert from Flux to Mono

How can I convert from a Flux with 1 element to a Mono?
Flux.fromArray(arrayOf(1,2,1,1,1,2))
.distinct()
.take(1)
How do I make this a Mono(1)?
Instead of take(1), you could use next().
This will transform the Flux into a valued Mono by taking the first emitted item, or an empty Mono if the Flux is empty itself.
Here is a list:
Flux#single will work if there is one element from Flux. Eg: flux.take(1).single();
Flux#next will get you the first element. Eg: flux.next();
Flux#last for last element. Eg: flux.last();
Flux#singleOrEmpty is similar to Optional. Eg: flux.take(0).singleOrEmpty();
Flux#collect, it depends on use case.
flux.collect(Collectors.reducing((i1, i2) -> i1))
.map(op -> op.get());
Flux#elementAt for i'th index. Eg: flux.elementAt(1);
Flux#shareNext for first found element. flux.shareNext();
Flux#reduce for reduction op. Eg: flux.reduce((i1,i2) -> i1);
Or,you could use single() on the filtered Flux
Also the very simple way is to use Mono.from()
Mono<Integer> mono = Mono.from(flux);
If your flux has more than one element, then it will just take the first element emitted by the flux.

How to select Highvalues of the candlestick with data source

I always see the code as datasource="series0".
If series(0) is a candlestick and I want to use Highvalues or Closevalues of the candlestick, how so I select that data? Something like datasource="series0.Highvalues"? (It's worth noting that I use teechart2011 Eval and VB6).
If series(1) is the financial function ExpMovAvg, how to define the width of the ExpMovAvg line with code?
Similarly how do I use the Closevalues in Series(0) for this function? Not merely datasource="series0". Thanks !
I always see the code as datasource="series0", if series(0) is a candlestick and I want to use Highvalues or Closevalues of the candlestick,how to select that data? datasource="series0.Highvalues"? (I use teechart2011 Eval and VB6)
Here you have a simple example in VB6. You can assign any of the 4 ValueLists in the Candle series (Open, Close, High, Low) to the be used by the function with the MandatoryValueList.ValueSource property:
TChart1.AddSeries scCandle
TChart1.Series(0).FillSampleValues
TChart1.AddSeries scLine
TChart1.Series(1).SetFunction tfExpMovAvg
TChart1.Series(1).DataSource = TChart1.Series(0)
TChart1.Series(1).MandatoryValueList.ValueSource = "Close" '"Open" "High" "Low"
if series(1) is the financial function ExpMovAvg, how to define the
width of the ExpMovAvg line with code?
You can set the series' Pen.Width property as follows:
TChart1.Series(1).Pen.Width = 2
Similarly how to use the Closevalues in Series(0) for this function?
not merely datasource="series0", thanks !
This is the same above, isn't it?

Custom Array Functions in Open Office Calc

Could someone please tell me how to write a custom function in Open Office Basic to be used in Open Office Calc and that returns an array of values. An example of one such built-in function is MINVERSE. I need to write a custom function that populates a range of cells in much the same way.
Help would be much appreciated.
Yay, I just figured it out: all you do is return an array from your macro, BUT you also have to press Ctrl+Shift+Enter when typing in the cell formula to call your function (which is also the case when working with other arrays in calc). Here's an example:
Function MakeArray
Dim ret(2,2)
ret(0,0) = 1
ret(1,0) = 2
ret(0,1) = 3
ret(1,1) = 4
MakeArray = ret
End Function
FWIW, damjan's MakeArray function returns a Variant containing an array, I think. (The type returned by MakeArray is unspecified, so it defaults to Variant. A Variant is a container with a descriptive header, apparently cast as needed by the interpreter.)
Almost, but not quite, the same thing as returning an array. According to http://www.cpearson.com/excel/passingandreturningarrays.htm, Microsoft did not introduce the ability to return an array until 2000. His example [ LoadNumbers(Low As Long, High As Long) As Long()] does not compile in OO, flagging a syntax error on the parens following Long. It appears that OO's Basic emulates the pre-2k VBA.

Resources