gpt4 book ai didi

java - 在 3D BarChart 中设置多种颜色 - JFreeChart

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:19 25 4
gpt4 key购买 nike

我是 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

最佳答案

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

  • 调用setSeriesPaint(),如BarChartDemo1所示source .

  • 重写getItemPaint()来定义自定义配色方案,如图here .

关于java - 在 3D BarChart 中设置多种颜色 - JFreeChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13094063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com