我是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);
}
}
答案 0 :(得分:1)
所有条形都是相同的颜色,因为它们都属于同一系列,由rowKey
中的addValue()
参数标识。改变颜色的两种常用方法包括:
调用setSeriesPaint()
,如BarChartDemo1
source所示。
覆盖getItemPaint()
以定义自定义配色方案,如here所示。