在JFreeChart堆叠的3D条形图中更改自定义颜色

时间:2013-09-18 07:58:09

标签: java jfreechart struts2-jfreechart-plugin

我已经在网上搜索过但没有运气。

我创建了一个堆叠的三维条形图,我无法更改默认颜色。我尝试了所有提供的建议。

在我的代码的一小部分下面。这是我的意见。

来自Db的我的数据是:

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 );

EvenI使用了setSeriespaint,但它没有用。 是否与图表,情节和渲染器中的创建顺序有关?

当我打印行和列时,我得到了这个:

row:1
Col:0
row:0
Col:1
row:1
Col:1
row:1
Col:2
row:1
Col:3
row:1
Col:4
row:0
Col:5
row:1
Col:6

1 个答案:

答案 0 :(得分:3)

您可以覆盖渲染器的getItemPaint(),如图所示here

plot.setRenderer(new StackedBarRenderer3D() {

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

plot colors