Search code examples
javajfreechart

How to get max Y value from StackedBarChart (jFreeChart)?


How to get the maximum axis value from a created chart?

Here is how it is created:

final JFreeChart chart = ChartFactory.createStackedBarChart("", "", symbol, dataSet,PlotOrientation.VERTICAL, false, false, false);

I probably have to get the dataset from the chart and then get the maximum axis value from it. The dataset is DefaultCategoryDataset.


Solution

  • Just iterate through the CategoryDataset

    CategoryDataset dataset = createDataset();
    for (int r = 0; r < dataset.getRowCount(); r++) {
        double max = Double.MIN_VALUE;
        for (int c = 0; c < dataset.getColumnCount(); c++) {
            Number number = dataset.getValue(r, c);
            double value = number == null ? Double.NaN : number.doubleValue();
            if (value > max) {
                max = value;
            }
        }
        System.out.println(dataset.getRowKey(r) + ": " + max);
    }
    

    Using the example dataset, produces the following output:

    First: 5.0
    Second: 8.0
    Third: 6.0