【问题标题】:Change the series color indicator at the bottom of the chart更改图表底部的系列颜色指示器
【发布时间】:2013-12-25 12:28:52
【问题描述】:

我正在使用 jfreechart 绘制一些图表,我希望图表条具有自定义颜色,因此我遵循了 thisthis 链接中提到的教程和示例代码。

条形颜色问题已解决,但现在我无法更改图表底部的系列颜色指示器,如下图所示。

有人可以指导我如何根据条形改变颜色吗?

编辑:

class CustomRenderer extends BarRenderer
{
public Paint[] colors;

public CustomRenderer()
{
    this.colors = new Paint[] { Color.green, Color.red };
    /*
     * { Color.red, Color.blue, Color.green, Color.yellow, Color.orange,
     * Color.cyan, Color.magenta, Color.blue };
     */
}

public Paint getItemPaint(final int row, final int column)
{
    // returns color for each column
    return ((column % 2 == 0) ? colors[0] : colors[1]);
}
}
//Code for creating the chart in main() method
DefaultCategoryDataset objDataset = new DefaultCategoryDataset();
    objDataset.setValue(10, "POS", "one");
    objDataset.setValue(11, "NEG", "two");
    objDataset.setValue(8, "POS", "three");
    objDataset.setValue(12, "NEG", "four");


    final JFreeChart objChart = ChartFactory.createBarChart(
            "Top Ten Features", // Chart title
            "Features", // Domain axis label
            "POS and NEG value", // Range axis label
            objDataset, // Chart Data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend?
            true, // include tooltips?
            false // include URLs?
            );


    final CategoryPlot categoryPlot = objChart.getCategoryPlot();
    CategoryItemRenderer renderere = new CustomRenderer();
    categoryPlot.setRenderer(renderere);
    final BarRenderer br = (BarRenderer) categoryPlot.getRenderer();
    br.setItemMargin(-1);

    final ChartFrame frame = new ChartFrame("Demo", objChart);
    frame.pack();
    frame.setVisible(true);

【问题讨论】:

  • 请编辑您的问题以包含sscce,以显示您如何获得显示的结果。
  • @trashgod 代码已添加。

标签: java charts jfreechart


【解决方案1】:

您的自定义BarRenderer 会覆盖getItemPaint(),但它会保持原始getLegendItem() 不变。这两种方法需要保持一致。您可以覆盖 lookupSeriesPaint(),或者更一般地说,提供备用 DrawingSupplier,如引用 here 的文章中所示。

【讨论】:

  • 非常感谢,DrawingSupplier 解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多