2

是否可以检索 BarChart 的所有 rangeAxis 值?我设法像这样绘制新的 GridLines(Markers):

在此处输入图像描述

但我需要知道值轴中的图表需要哪些值才能绘制所有线条。任何想法如何获得“价值”轴上的所有价值?(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);
    }
}
4

2 回答 2

0

看起来您能够以addRangeMarker()类似于此域示例的方式加以利用。匹配标记和刻度单位的一种方法是指定TickUnitSource包含您选择的标记的 a:

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

如果您需要自定义源,您可以在createIntegerTickUnits()或上对其进行建模createStandardTickUnits()

于 2013-05-24T16:10:33.410 回答
0

没有设法理解如何以“好”的方式做到这一点。制作了一个步骤方法来知道在哪里放置当前图表最大值的线条:

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;
}
于 2013-05-29T06:46:06.350 回答