2

Is it possible to retrieve all rangeAxis-values for a BarChart? I have manage to draw new GridLines(Markers) like this:

enter image description here

But I need to know which values that are pressent for the chart in the value-axis to be able to draw all lines. Any ideas how to get all value on the "Value"-axis?(RangeAxis)

public class BarChartDemo extends ApplicationFrame {

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public BarChartDemo(final String title) {
        super(title);
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Returns a sample dataset.
     * 
     * @return The dataset.
     */
    private CategoryDataset createDataset() {

        // row keys...
        final String series1 = "First";

        // column keys...
        final String category1 = "Category 1";
        final String category2 = "Category 2";
        final String category3 = "Category 3";

        // create the dataset...
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(3.5, series1, category1);
        dataset.addValue(4.0, series1, category2);
        dataset.addValue(3.0, series1, category3);
        return dataset;
    }

    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return The chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createBarChart(
            "Bar Chart Demo",         // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            false,                     // include legend
            false,                     // tooltips?
            false                     // URLs?
        );
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getDomainAxis().setCategoryMargin(.01);

        plot.setRangeGridlinesVisible(false);
        for(int i=1; i<=4; i++){
            Marker marker = new ValueMarker(i);
            marker.setStroke(new BasicStroke(
                    1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                    1.0f, new float[] {3.0f, 8.0f}, 0.0f
                ));
            marker.setPaint(new Color(224,224,224));
            plot.addRangeMarker(marker);
        }

        return chart;
    }

    public static void main(final String[] args) {
        final BarChartDemo demo = new BarChartDemo("Bar Chart Demo");
        demo.pack();
        demo.setVisible(true);
    }
}
Grains
  • 950
  • 3
  • 16
  • 35
  • Have you tried enabling trace, shown [here](http://stackoverflow.com/a/5522583/230513)? – trashgod May 20 '13 at 10:02
  • Tried but it seems like its based on "ChartPanel" which seems to be a JPanel. But I want to draw it directly to pdf with iText. – Grains May 20 '13 at 11:33
  • Please edit your question to clarify this usage and include an [sscce](http://sscce.org/) that shows what `Marker` you tried; see the demo for examples. – trashgod May 20 '13 at 15:02
  • 1
    Ah, I was thinking you could use `addRangeMarker()` and `setDrawAsLine`, similar to [this](http://stackoverflow.com/a/11391403/230513) domain example. You could look at how `CategoryPlot@draw()` calls `draw*Gridlines`. – trashgod May 21 '13 at 16:55
  • Thanks! Managed to draw lines now with `addRangeMarker()`. Edited question for the remainder part of the problem. – Grains May 24 '13 at 11:45

2 Answers2

0

It looks like you were able to leverage addRangeMarker() in a way similar to this domain example. One way to match markers and tick units would be to specify a TickUnitSource that includes your chosen markers:

NumberAxis range = (NumberAxis) plot.getRangeAxis();
range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

If you need a custom source, you can model it on createIntegerTickUnits() or createStandardTickUnits().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Did not manage to understand how to do it in a "nice" way. Made a step method to know where to put the lines for the current charts max value:

private static double getGridStep(double max){
        double step = 0;
        if(max > 86){
            step = 10;
        }
        else if(max >= 30){
            step = 5;
        }
        else if(max > 17){
            step = 2.5;
        }
        else if(max > 7){
            step = 1;
        }
        else if(max > 2){
            step = 0.5;
        }
        else if(max > 1){
            step = 0.25;
        }
        else
            step = 0.1;
        return step;
}
Grains
  • 950
  • 3
  • 16
  • 35