在JFreeChart堆叠3D柱状图中更改自定义颜色

3
我已经在网上进行了足够的搜索,但没有找到答案。
我创建了一个堆叠的3D条形图,但无法更改默认颜色。我尝试了所有提供的建议。
以下是我的代码片段。这是我的输入。
来自数据库的数据是:
A 0 2 B 15 53 C 0 2 D 0 2 E 0 1 F 1 0 G 0 1
不知何故,我将其转换为数据集需求,并添加了项目和模型。
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(item,models, data);
chart = ChartFactory.createStackedBarChart3D(chartDescription, X-axis, Y-axis, dataset,PlotOrientation.VERTICAL, true, true, true);
CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer r = plot.getRenderer();
r.setSeriesPaint(0, Color.GREEN);
r.setSeriesPaint(1, Color.GRAY);
 plot.setRenderer(new StackedBarRenderer3D() {

@Override
public Paint getItemPaint(int row, int col) {
    System.out.println("row:"+row);
    System.out.println("Col:"+col);
    return Color.getHSBColor(row / 42f, 1, 1);
}
});
CategoryAxis domainAxis = plot.getDomainAxis(); 
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

StackedBarRenderer3D renderer = (StackedBarRenderer3D)plot.getRenderer();
//renderer.setBarPainter(new StandardBarPainter());
renderer.setSeriesFillPaint(0,Color.BLACK);
renderer.setSeriesFillPaint(1,Color.GREEN);
renderer.setDrawBarOutline(false);
renderer.setShadowVisible(false);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER,TextAnchor.CENTER));
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setIgnoreZeroValues(true);
renderer.setMaximumBarWidth(.05);
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(
    ItemLabelAnchor.CENTER, TextAnchor.CENTER_RIGHT));
renderer.setNegativeItemLabelPositionFallback(new ItemLabelPosition(
ItemLabelAnchor.CENTER, TextAnchor.CENTER_RIGHT));
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.CENTER, TextAnchor.TOP_CENTER));
renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.CENTER, TextAnchor.TOP_CENTER));
LegendTitle legend = chart.getLegend(0); 
legend.setBackgroundPaint(Color.white);
legend.setFrame(new BlockBorder(Color.green));
StandardChartTheme theme = (StandardChartTheme)org.jfree.chart.StandardChartTheme.createJFreeTheme();
theme.setTitlePaint(Color.decode("#4572a7"));
theme.setExtraLargeFont(new Font("Arial",Font.BOLD, 16) ); 
theme.setLargeFont(new Font("Arial",Font.BOLD, 15)); 
theme.setRegularFont( new Font("Arial",Font.PLAIN, 11));
theme.setRangeGridlinePaint(Color.RED);
theme.setPlotBackgroundPaint( Color.white );
theme.setChartBackgroundPaint( Color.white );
theme.setItemLabelPaint(Color.YELLOW);
theme.setShadowVisible(true);
theme.setAxisLabelPaint( Color.decode("#666666")    );
theme.apply( chart );

我曾尝试使用setSeriespaint,但它并没有起作用。这与图表、绘图和渲染器的创建顺序有关吗?

当我打印行和列时,得到了以下结果:

行:1
列:0
行:0
列:1
行:1
列:1
行:1
列:2
行:1
列:3
行:1
列:4
行:0
列:5
行:1
列:6
1个回答

4
您可以重写渲染器的 getItemPaint(),如此示例所示:这里
plot.setRenderer(new StackedBarRenderer3D() {

    @Override
    public Paint getItemPaint(int row, int col) {
        return Color.getHSBColor(row / 42f, 1, 1);
    }
});

plot colors


1
使用 rowcol 来决定使用什么颜色。 - Catalina Island
1
@CatalinaIsland 的想法很正确:制作四个调色板,每个调色板都有一系列相关的颜色,例如绿色、黄色等;根据 col 选择调色板;这里有一个相关的示例;还可以参考这里引用的 DrawingSupplier 方法。 - trashgod
请编辑您的问题,包括一个完整示例,展示一小部分您的数据。 - trashgod
@trashgod:请在我编辑后提供任何意见。这是除数据部分以外的全部代码。 - user2228697
我不理解你的数据模型;请第三次编辑你的问题,包括一个短小、自包含、正确(可编译)的示例,其中包含你的图表和数据。 - trashgod
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接