JFreeChart:带有StandardXYItemLabelGenerator的XYAreaRenderer2

时间:2012-07-13 20:11:13

标签: java charts jfreechart

我正在尝试将StandardXYItemLabelGeneratorXYAreaRenderer2渲染器一起使用,但没有任何内容出现。如果使用与XYLineAndShapeRenderer相同的代码,我会正确地看到图表上的标签。如何在XYAreaChart上看到这些标签?

 // create the chart...
        final JFreeChart chart = ChartFactory.createXYAreaChart(
                header_label, // chart title
                x_label, // x axis label
                y_label, // y axis lsabel
                dataset, // data
                PlotOrientation.VERTICAL,
                includeLegend, // include legend
                true, // tooltips
                false // urls
                );

        // NOW DO SOME OPTIONAL CUSTOMIZATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customization...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white); // X Axis is Domain
        plot.setRangeGridlinePaint(Color.white);  // Y Axis is Range

        // Allow lines and shapes to be visible
        renderer1 = new XYAreaRenderer();

        // Set the tooltip generator
        renderer1.setBaseToolTipGenerator(toolTipGenerator);

        NumberFormat.getNumberInstance().setMaximumFractionDigits(2);
//        renderer1.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("{0} {1} {2}", NumberFormat.getNumberInstance(), NumberFormat.getNumberInstance()));
        renderer1.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("test  {0} {1} {2}", NumberFormat.getNumberInstance(), NumberFormat.getNumberInstance()));

        renderer1.setBaseItemLabelsVisible(true);

        plot.setRenderer(renderer1);

1 个答案:

答案 0 :(得分:3)

XYLineAndShapeRenderer不同,XYAreaRendererXYAreaRenderer2都不会调用抽象父级drawItemLabel()。实际上,不支持标签,但工具提示如下所示。

XYAreaRenderer2 renderer = new XYAreaRenderer2(
    new StandardXYToolTipGenerator("test: {0} {1} {2}",
    NumberFormat.getNumberInstance(),
    NumberFormat.getNumberInstance()), null);
plot.setRenderer(renderer);