c3 charts dynamic bubble size in scatter plot, wrong index - dynamic

I'm trying to make a chart like with c3.js.
For the bubble size I create an array for each "continent" holding the population as a factor to increase the bubble size. Also the country name is stored in the same manner.
When adding the data points to the chart as well as when adding the bubble sizes/country names to the array, the indices are the same. E.g. col_2[0] in the bubbleInfo array is "China". Also in the data columns col_2[0] and col_2_x[0] are 76 and 12000 which are the values for China.
However, in the section where I dynamically get the bubble radius/country name, the index I get from the function parameter is not the one of the col_2 arrays. Instead I get the index in the order in which the dots are spead along the x-Axis.
E.g. I add for x-Axis China (12000), India(5800), Indonesia(9000) in this order.
I'd expect to get index 1 for India, but I get index 0, because 5800 is the lowest of the x values.
Because of that I cannot properly map the indices on the bubble sizes/country names, since the indices are wrong.
Is this a bug and if so, how can I properly map the bubble sizes/country names then?
Here is the JSFiddle:
var chart_3_bubbleSize = {
"col_2": [10.0, 9.0, 3.9, 2.5, ],
"col_1": [3.0, 2.5, ],
"col_3": [2.5, 5.5, ],
};
...
var chart_3_bubbleInfo = {
"col_2": ["China", "India", "Indonesia", "Japan", ],
"col_1": ["Russia", "Germany", ],
"col_3": ["Mexico", "USA", ],
};
...
columns: [
['col_2', 76, 66, 71, 86],
['col_2_x', 12000, 5800, 9000, 36000],
['col_1', 72, 80.4],
['col_1_x', 25000, 40000],
['col_3', 76, 78],
['col_3_x', 16000, 50000],
],
...
point:
{
r: function(d)
{
/*d.index gives the index according to the order along the x-axis, which leads to wrong result, when trying to map to country names/bubble sizes*/
return 2 * chart_3_bubbleSize[d.id][d.index];
}
},
https://jsfiddle.net/51oLxqyt/1/
The first green bubble in the lower left corner should be India, but it has the label "China" and the bubble size of China, because China is at index 0 in the bubbleInfo and bubbleSize arrays.

There is an not documented attribute data.xSort = false, which keeps the original index, which makes it possible to map more dimension e.g. for scatter charts.
More info here: https://github.com/c3js/c3/issues/547#issuecomment-56292971

Related

React Google Charts data structure: data objects with additional fields

How do I best use data objects for React Google Charts so I can access more information than just the 2 co-ordinates values.
Example:
https://codesandbox.io/s/react-google-charts-trendline-k727v9
The data object is just an array with x/y values: price and mileage.
export const data = [
["mileage", "price", { role: "style" }],
[5000, 12700, "green"],
[10000, 11200, "red"],
[15000, 9800, "red"],
[20000, 9200, "green"],
[25000, 8000, "red"],
[30000, 7200, "green"],
[35000, 5400, "green"]
];
The real object I would like to use would have the same x/y values, but a number of more key/value pairs, e.g.:
export const data = [
["mileage", "price", 'status', 'url', 'age'],
[5000, 12700, 'sold', 'sth.com', 13],
[10000, 11200, 'sold', 'sth.com', 13],
[15000, 9800, 'sold', 'sth.com', 13],
[20000, 9200, 'sold', 'sth.com', 13],
[25000, 8000, 'sold', 'sth.com', 13],
[30000, 7200, 'sold', 'sth.com', 13],
[35000, 5400, 'sold', 'sth.com', 13]
];
First of all sth. likle this throws an error because the values are not all of the same type, numbers and strings in this case.
Secondly I would like to be able to access these additional values in my chart event function. E.g. Click on a node and not only access price and mileage but also display all the other data associated with this node.
Obviously I could filter my original data array, split it into two, then fit the pieces back together again when I need a bit of data. But is that really the way to go?
I have use Nivo Charts before. There you could have complex objects and simply define which keys should be used to paint the nodes and still access all other keys/values associated with a data object. So I would use "mileage" and "price to paint the dots" but still have all the other key/values accessible as part of the data object for anything else I may want to do with it. I have not been able to figure out how to do sth. similar with React Google Charts.
Also d3-shape allows you to draw a line specifying 2 key/value pairs as the coordinates from a data object that can have any number of key/value pairs, not just x/y, e.g.:
const lineGenerator = line()
.x((d) => xScale(d.data.myField))
.y((d) => yScale(d.data.myOtherField))
Thank you!

Formatting Manipulate output to have 2 cells in Mathematica

The following output code outputs an array from the manipulate statement. I would like to output the fitting and plot as two separate output cells that update dynamically. I think it should be pretty simple, but I am having trouble with it. I've tried using the CellPrint[] function, but did not get it to work.
Thanks,
Tal
temperatures(*mK*)= {300, 200, 150, 100, 75, 50, 25, 11, 10};
F[t_, \[Nu]_] := t^\[Nu];
rd (*uOhms*)= {27173.91304, 31250., 42372.88136, 200601.80542,
1.05263*10^6, 1.33333*10^7, 1.33333*10^8, 2.*10^8, 2.1*10^8};
logRd = Log10[rd];
f[\[Nu]0_] := Module[{\[Nu]},
\[Nu] = \[Nu]0;
data = Transpose[{F[temperatures, \[Nu]]*10^3, logRd}];
fitToHexatic = LinearModelFit[data[[4 ;; 6]], x, x];
plota =
Plot[fitToHexatic["BestFit"], {x, 0, data[[-1]][[1]]},
Axes -> False];
plotb = ListPlot[data, Axes -> False];
{fitToHexatic, Show[{plota, plotb}, Axes -> True]}
]
Manipulate[
f[nu],
{nu, -0.2, -1}
]
Screenshot of the output:
You don't need to use a Manipulate. You can get more control with lower level functions. E.g.
Slider[Dynamic[nu, (f[#]; nu = #) &], {-0.2, -1}]
Dynamic[Normal[fitToHexatic]]
Dynamic[Show[{plota, plotb}, Axes -> True]]
See also Prototypical Manipulate in lower level functions.

Setting a max axis value or range step for a Morris Bar Chart?

I was wondering if it is possible to set a max axis value (say, I want the highest point of my data to be the top end of the y-axis) on a bar chart? I see there are options for ymin and ymax on line charts but I can't seem to find any information about the bar charts.
Also, it would be helpful if anyone knew how to force the range between axis lanes to be a certain amount (say step up by 250 each line instead of the generated amount which in my case is too high for my liking).
Set a maximum value for the y axis
You can, indeed, set the ymax for bar charts also (even though this is not documented).
Morris.Bar({
element: 'bar-example',
data: [
{ y: '2006', a: 100, b: 90 },
{ y: '2007', a: 75, b: 65 },
{ y: '2008', a: 50, b: 40 },
{ y: '2009', a: 75, b: 65 },
{ y: '2010', a: 50, b: 40 },
{ y: '2011', a: 75, b: 65 },
{ y: '2012', a: 100, b: 90 }
],
xkey: 'y',
ymax: 300, // set this value according to your liking
ykeys: ['a', 'b'],
labels: ['Series A', 'Series B']
});
And have your y axis set to this maximum value:
Set a range value for the y axis
It seems that it's not possible to set a range value for the y axis. This value appears to be computed according to the values of the data passed to Morris.Bar.
Not documented, but you can set maximum y by applying ymax. You can manipulate the range by setting numLines (also not documented).
E.g.
var chart = new Morris.Bar({
...
ymin: 0,
ymax: 7,
numLines: 8,
...
});
The above defined chart will display values from 0 to 7 and display a grid line for each integer between 0 and 7 (therefore 8 as a parameter)
To change to ymax call this
chart.options["ymax"] = 300;
Where chart is your chart variable
I want the highest point of my data to be the top end of the y-axis
The documentation is very sparse and confusing but this is possible using the ymin variable which is only documented in the Lines & Area Charts. The default value for that variable seems to be auto 0 and changing it to just auto seems to produce the desired result as you can see below.
how to force the range between axis lanes to be a certain amount
This does not seem to be possible, natively. However, you can kind of hack the axis label with the following function. It will round the value to multiples of 250 BUT the grid lines won't be at the number shown. E.g. say a grid line is at 570. The function below will change the label to 500 but the line will still show at 570 mark.
yLabelFormat: function(d) {
return Math.round(d) - (Math.round(d) % 250);
},
As others have mentioned, you can set ymax to a value that you want your upper bound to be but since you want the highest data point to be the upper bound, set ymax to auto. You can also try changing numLines to different values for a better aesthetic.

Mathematica Manipulate & PopupMenu

I'm new here and I've one question about the Manipulate function i Mathematica: I need to plot some data of a nested list where the first coordinate selects a category (of stocks, like banks, automobiles, pharmaceuticals, ...) and inside every category there are years and months coordinates, so it should be something like
In:= list[[cat]][[yr]][[mnth]]
Out= {1,2,3,4,5,6}
which are sorted stock prices belonging to category cat.
Now I'd like to plot this with an dynamic index in Manipulate with a PopupMenu which allows to select the category i need to plot: I already have a vector sect which at position cat has the sector referring to cat in list (which is to say sect[[i]] is the category of list[[i]]), but results are poor.
I've tried to use Manipulate[...,{index,sect}] and it seems to be the right way since there actually is a popup menu in the output, but it still gives error about syntax in the control cycle I need to plot only the right sector, something like
If[ sect[[j]] == index, Plot[ list[[j]] ] ].
So I'm stuck here, thanks to anyone will help!
I'm not convinced your data structure is optimal, but here's a sample set of data:
data2 = {
{"stock",
{
Range[6], Range[6, 12],
Range[12, 18]
},
{
Range[18, 24], Range[24, 30],
Range[30, 36]
}
},
{"bank",
Table[Range[i, i + 5], {i, 1, 18, 6}],
Table[Range[i, i + 5], {i, 18, 30, 6}]
}
};
and the Manipulate:
Manipulate[ListPlot[data2[[cat, year, month]]],
{{cat, 1, "Category"}, {1 -> "stock", 2 -> "bank"},
ControlType -> PopupMenu},
{{year, 2, "Year"}, {2 -> "2010", 3 -> "2011"},
ControlType -> PopupMenu},
{{month, 1, "Month"}, {1 -> "Jan", 2 -> "Feb", 3 -> "Mar"},
ControlType -> PopupMenu}]

Dojox chart number of Y axis labels

When I create a DojoX vertical bar chart, it automatically calculates the labels on the Y axis to cover the largest value. In this case, it created 3 Y-axis labels, 100, 200 300.
How do I increase the number of labels, to say intervals of 50 rather than intervals of 100?
When you add an axis, there are several options you can use for the tick marks. Example:
chart.addAxis("y", {
majorTicks : true, // default
majorLabels : true, // default
majorTick : {length: 4, color: "#FFF"},
majorTickStep : 10,
minorTicks : true,
minorLabels : true,
minorTick : { length: 2, color: "#CCC"},
minorTickStep : 5,
...
vertical: true
});
Check the API docs for more: http://dojotoolkit.org/api/1.8/dojox/charting/axis2d/Default