1

我是 JFreeChart 库的新手。我目前正在使用 3D 条形图,我想知道是否可以让每个生成的 3D 条具有不同的颜色?它们目前都是黄色的,如下所示。

有什么我可以覆盖来改变颜色的吗?

谢谢。

代码:

import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class jfree extends ApplicationFrame
{
    {
        // set a theme using the new shadow generator feature available in
        // 1.0.14 - for backwards compatibility it is not enabled by default
        ChartFactory.setChartTheme(StandardChartTheme.createDarknessTheme());
    }

    public jfree(String s)
    {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    private static CategoryDataset createDataset()
    {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
                    defaultcategorydataset.addValue(25D, "Series 1", "Pipe Stricking");
                    defaultcategorydataset.addValue(17D, "Series 1", "Lost Circulation");
                    defaultcategorydataset.addValue(34D, "Series 1", "Hole Deviation");
                    defaultcategorydataset.addValue(12D, "Series 1", "Kick Blowout");
                    defaultcategorydataset.addValue(54D, "Series 1", "Formation Change");
                    defaultcategorydataset.addValue(10D, "Series 1", "Borehole Instability");
                    defaultcategorydataset.addValue(19D, "Series 1", "Cavings/Pack-offs");
        return defaultcategorydataset;
    }

    private static JFreeChart createChart(CategoryDataset categorydataset)
    {
        JFreeChart jfreechart = ChartFactory.createBarChart3D("IPS", "", "Value", categorydataset, PlotOrientation.VERTICAL, false, false, false);
                    CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
                    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
                    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.2D));
                    CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
                    categoryitemrenderer.setBaseItemLabelsVisible(true);
                    BarRenderer barrenderer = (BarRenderer)categoryitemrenderer;
                    barrenderer.setItemMargin(200D);
        return jfreechart;
    }

    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart(createDataset());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[])
    {
        jfree jfree = new jfree("3D Bar Chart Demo 3");
        jfree.pack();
        RefineryUtilities.centerFrameOnScreen(jfree);
        jfree.setVisible(true);
    }
}

JFree3DBarChart

4

1 回答 1

1

所有的条都是相同的颜色,因为它们都属于同一个系列,由 中的rowKey参数标识addValue()。改变颜色的两种常见方法包括:

  • 调用setSeriesPaint(),如源码所示。BarChartDemo1

  • 覆盖以定义自定义配色方案,如此getItemPaint()所示。

于 2012-10-27T01:25:36.450 回答