pepstock-org / Charba

J2CL and GWT Charts library based on CHART.JS
https://pepstock-org.github.io/Charba-Wiki/docs
Apache License 2.0
62 stars 6 forks source link

Maximum value of Y-axis #38

Closed elatoskinas closed 5 years ago

elatoskinas commented 5 years ago

Is it possible to get the current maximum value of the y-axis of a Chart without doing calculations on the data itself? For instance, taking the very first Line Chart example from the wiki: https://github.com/pepstock-org/Charba/wiki/ChartLine

So the max value is y = 100 on the Y axis. Is it possible to get this number?

stockiNail commented 5 years ago

@lightingft if you need the max value of the axis, you should access to chart node, as following:

if (chart.isInitialized()){
     // internal chart entities
     ChartNode node = chart.getNode();
     // gets scales
     ScalesNode scales = node.getScales();
     // gets sclaes map
     Map<String,ScaleItem> scalesMap = scales.getItems();
     // gets the Y axis
     ScaleItem item = scalesMap.get(Scales.DEFAULT_Y_AXIS_ID);
     // gets max
     int max= item.getMax();
}
stockiNail commented 5 years ago

@lightingft be aware that the previous source code is providing you the MAX value of axis, NOT the max value of your data.

To have it, you could use Collections.max(Collection, Comparator).

elatoskinas commented 5 years ago

@lightingft if you need the max value of the axis, you should access to chart node, as following:

if (chart.isInitialized()){
     // internal chart entities
     ChartNode node = chart.getNode();
     // gets scales
     ScalesNode scales = node.getScales();
     // gets sclaes map
     Map<String,ScaleItem> scalesMap = scales.getItems();
     // gets the Y axis
     ScaleItem item = scalesMap.get(Scales.DEFAULT_Y_AXIS_ID);
     // gets max
     int max= item.getMax();
}

Hmm, strangely the key that I had to use (without changing anything) for scalesMap had to be "y-axis-1" instead of the Default Y Axis ID ("y-axis-0").

Thank you for the answer!

stockiNail commented 5 years ago

Hmm, strangely the key that I had to use (without changing anything) for scalesMap had to be "y-axis-1" instead of the Default Y Axis ID ("y-axis-0").

I see that if you are using Scatter charts, CHART.JS is setting as default Axis id [x-axis-1, y-axis-1] Nevertheless you can set ID of axis, using own linear axis instance, like this (tested with a scatter chart):

CartesianLinearAxis axis = new CartesianLinearAxis(chart);
axis.setDisplay(true);
axis.setId(Scales.DEFAULT_Y_AXIS_ID);
chart.getOptions().getScales().setYAxes(axis);
elatoskinas commented 5 years ago

Hmm, strangely the key that I had to use (without changing anything) for scalesMap had to be "y-axis-1" instead of the Default Y Axis ID ("y-axis-0").

I see that if you are using Scatter charts, CHART.JS is setting as default Axis id [x-axis-1, y-axis-1] Nevertheless you can set ID of axis, using own linear axis instance, like this (tested with a scatter chart):

CartesianLinearAxis axis = new CartesianLinearAxis(chart);
axis.setDisplay(true);
axis.setId(Scales.DEFAULT_Y_AXIS_ID);
chart.getOptions().getScales().setYAxes(axis);

Ahh, I see. Thanks! That answers my problem.

stockiNail commented 5 years ago

@lightingft I was curious why the scatter charts are using y-axis-1 instead of 0.

I have seen that the default scales definitions (default set by CHART.JS) of Scatter chart have got x-axis-1 and y-axis-1 (only scatter chart has got this default).

In Charba you could see those defaults as following (that you can change them as well):

Defaults.get().getOptions(ChartType.SCATTER).getScales().getYAxes();