2

我正在尝试修改 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;

}
4

1 回答 1

4

在创建图表后应用ChartTheme权利,然后再调用setSeriesPaint().

JFreeChart chart = ChartFactory.createTimeSeriesChart(...);
ChartUtilities.applyCurrentTheme(chart);
XYPlot plot = (XYPlot) chart.getPlot();
于 2013-03-18T19:11:55.173 回答