XYItemRenderer :: setSeriesPaint()不设置图表线的颜色

时间:2013-03-18 17:48:10

标签: jfreechart

我正在尝试修改PriceVolumeDemo1.java中的JFreeChart示例代码供我自己使用。 (我确定没有人记住所有的JFreeChart演示,我只是为了完整性而提到它。)这是一个代表价格的线条和代表音量的条形图。演示中的线条为红色,条形为蓝色。我试图扭转这一点(蓝线和红线)。我试图用XYBarRenderer :: setSeriesPaint()和XYItemRenderer :: setSeriesPaint()这样做。请参阅下面的代码。

我在尝试更改颜色时修改过的行跟随/* */块中的注释。

XYBarRenderer :: setSeriesPaint()按预期工作,将条形设置为红色。但是XYItemRenderer :: setSeriesPaint()不会将该行设置为蓝色。有人能看出我做错了吗?

private static JFreeChart createChart() {

    XYDataset priceData = createPriceDataset();
    String title = "Eurodollar Futures Contract (MAR03)";
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        title,
        "Date",
        "Price",
        priceData,
        true,
        true,
        false
    );
    XYPlot plot = (XYPlot) chart.getPlot();
    NumberAxis rangeAxis1 = (NumberAxis) plot.getRangeAxis();
    rangeAxis1.setLowerMargin(0.40);  // to leave room for volume bars
    DecimalFormat format = new DecimalFormat("00.00");
    rangeAxis1.setNumberFormatOverride(format);

    XYItemRenderer renderer1 = plot.getRenderer();
    /* This does NOT set the line graph to blue. */
    renderer1.setSeriesPaint(0, Color.BLUE);

    renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
            new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));

    NumberAxis rangeAxis2 = new NumberAxis("Volume");
    rangeAxis2.setUpperMargin(1.00);  // to leave room for price line
    plot.setRangeAxis(1, rangeAxis2);
    plot.setDataset(1, createVolumeDataset());
    plot.setRangeAxis(1, rangeAxis2);
    plot.mapDatasetToRangeAxis(1, 1);
    XYBarRenderer renderer2 = new XYBarRenderer(0.20);
    renderer2.setBaseToolTipGenerator(
        new StandardXYToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
            new SimpleDateFormat("d-MMM-yyyy"),
            new DecimalFormat("0,000.00")));
    plot.setRenderer(1, renderer2);
    ChartUtilities.applyCurrentTheme(chart);
    renderer2.setBarPainter(new StandardXYBarPainter());
    renderer2.setShadowVisible(false);
    /* This sets the bar colors to red. */
    renderer2.setSeriesPaint(0, Color.RED);
    return chart;

}

1 个答案:

答案 0 :(得分:2)

在创建图表后立即应用ChartTheme,然后再拨打setSeriesPaint()

JFreeChart chart = ChartFactory.createTimeSeriesChart(...);
ChartUtilities.applyCurrentTheme(chart);
XYPlot plot = (XYPlot) chart.getPlot();