【问题标题】:Change StackedBarChart Colors based on key values根据键值更改 StackedBarChart 颜色
【发布时间】:2014-04-15 09:36:27
【问题描述】:

我想使用键值修改stackedBarChart 的颜色。我知道如何对饼图执行此操作,但无法对stackedBarCharts 执行相同操作。

对于饼图,基本上我的方法类似于here的答案

需要注意的代码行是:

PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionPaint("J+1", Color.black);
plot.setSectionPaint("J-1", new Color(120, 0, 120));

但是对于 StackedBarChart,我不确定该怎么做,基本上我必须修改下面现有的 jfreechart 代码:

 public static JFreeChart createStackedBarChart(final String title,
                        final CategoryDataset dataset) {

                JFreeChart sectorChart = ChartFactory.createStackedBarChart(title, "",
                                "", dataset, PlotOrientation.VERTICAL, true, false, false);

                CategoryPlot plot = (CategoryPlot) sectorChart.getPlot();
                formatCategoryPlot(plot);
                sectorChart.getLegend().setBorder(0, 0, 0, 0);
                sectorChart.setBorderVisible(false);
                sectorChart.setBorderPaint(Color.white);
                plot.setOutlineVisible(false);
                StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();


                return Chart;

        }

所以我的问题真的是,有没有相当于

PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionPaint("J+1", Color.black);

堆叠条形图?如果是,我该如何使用它?

我可以从网络资源中看到关于 setSeriesPaint 的一些内容,但这似乎是根据索引更改颜色。我想根据标签更改颜色,例如“J+1”。

【问题讨论】:

    标签: colors jfreechart pie-chart stackedbarseries


    【解决方案1】:

    你可以覆盖渲染器的getItemPaint()方法,如图here

    【讨论】:

    • 谢谢。该代码没有显示如何根据部分标签定义颜色...您能帮忙详细说明一下吗?
    • 您还可以查看自定义DrawingSupplier,提到here
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2015-09-02
    • 2017-07-28
    相关资源
    最近更新 更多