请教关于jfreechat生成柱状图的问题?
我想要生成的柱状图,每柱柱子是由不同的颜色叠加起来的。
package demo;
import java.awt.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LayeredBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.SortOrder;
public class LayeredBarChartDemo1 extends ApplicationFrame
{
public LayeredBarChartDemo1(String s)
{
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}
private static CategoryDataset createDataset()
{
String s = "First";
String s1 = "Second";
String s2 = "Third";
String s3 = "Category 1";
String s4 = "Category 2";
String s5 = "Category 3";
String s6 = "Category 4";
String s7 = "Category 5";
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, s, s3);
defaultcategorydataset.addValue(4D, s, s4);
defaultcategorydataset.addValue(3D, s, s5);
defaultcategorydataset.addValue(5D, s, s6);
defaultcategorydataset.addValue(5D, s, s7);
defaultcategorydataset.addValue(5D, s1, s3);
defaultcategorydataset.addValue(7D, s1, s4);
defaultcategorydataset.addValue(6D, s1, s5);
defaultcategorydataset.addValue(8D, s1, s6);
defaultcategorydataset.addValue(4D, s1, s7);
defaultcategorydataset.addValue(4D, s2, s3);
defaultcategorydataset.addValue(3D, s2, s4);
defaultcategorydataset.addValue(2D, s2, s5);
defaultcategorydataset.addValue(3D, s2, s6);
defaultcategorydataset.addValue(6D, s2, s7);
return defaultcategorydataset;
}
private static JFreeChart createChart(CategoryDataset categorydataset)
{
JFreeChart jfreechart = ChartFactory.createBarChart("Layered Bar Chart Demo 1", "Category", "Value", categorydataset, PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.white);
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
LayeredBarRenderer layeredbarrenderer = new LayeredBarRenderer();
layeredbarrenderer.setDrawBarOutline(false);
categoryplot.setRenderer(layeredbarrenderer);
categoryplot.setRowRenderingOrder(SortOrder.DESCENDING);
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0, 0, 64));
GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
layeredbarrenderer.setSeriesPaint(0, gradientpaint);
layeredbarrenderer.setSeriesPaint(1, gradientpaint1);
layeredbarrenderer.setSeriesPaint(2, gradientpaint2);
return jfreechart;
}
public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String args[])
{
LayeredBarChartDemo1 layeredbarchartdemo1 = new LayeredBarChartDemo1("Layered Bar Chart Demo 1");
layeredbarchartdemo1.pack();
RefineryUtilities.centerFrameOnScreen(layeredbarchartdemo1);
layeredbarchartdemo1.setVisible(true);
}
}import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class Test {
private static final String series1 = "series1";
private static final String series2 = "series2";
private static final String series3 = "series3";
private static final String category1 = "category1";
private static final String category2 = "category2";
private static final String category3 = "category3";
private static final String category4 = "category4";
private static final String category5 = "category5";
public static void main(String[] args) {
javax.swing.JFrame frame = new javax.swing.JFrame("stack bar");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setContentPane(new org.jfree.chart.ChartPanel(
createStackedBarChart()));
}
/**
* 建立一个JFreeChart对象
* @return
*/
public static JFreeChart createStackedBarChart() {
JFreeChart chart = ChartFactory.createStackedBarChart(
"StackedBarChart", "Categary", "Value",
createCategoryDataset(), PlotOrientation.HORIZONTAL, true,
true, false);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
setCategoryPlot(plot);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
return chart;
}
/**
* 建立数据集
* @return
*/
public static CategoryDataset createCategoryDataset() {
DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
categoryDataset.addValue(2.0, series1, category1);
categoryDataset.addValue(4.0, series1, category2);
categoryDataset.addValue(3.0, series1, category3);
categoryDataset.addValue(7.0, series1, category4);
categoryDataset.addValue(5.0, series1, category5);
categoryDataset.addValue(5.0, series2, category1);
categoryDataset.addValue(9.0, series2, category2);
categoryDataset.addValue(6.0, series2, category3);
categoryDataset.addValue(5.0, series2, category4);
categoryDataset.addValue(2.0, series2, category5);
categoryDataset.addValue(6.5, series3, category1);
categoryDataset.addValue(7.5, series3, category2);
categoryDataset.addValue(4.0, series3, category3);
categoryDataset.addValue(8.0, series3, category4);
categoryDataset.addValue(9.0, series3, category5);
return categoryDataset;
}
private static void setCategoryPlot(CategoryPlot plot) {
plot.getDomainAxis().setVisible(true);
plot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
plot.getDomainAxis().setLabelPaint(Color.BLACK);
plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
plot.getDomainAxis().setTickLabelPaint(Color.BLACK);
plot.getDomainAxis().setTickLabelsVisible(true);
plot.getRangeAxis().setVisible(true);
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
plot.getRangeAxis().setLabelPaint(Color.BLACK);
plot.getRangeAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
plot.getRangeAxis().setTickLabelPaint(Color.BLACK);
plot.getRangeAxis().setVerticalTickLabels(false);
plot.getRangeAxis().setLabelAngle(0.0D);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinesVisible(true);
}
}import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.RenderingHints;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeriesCollection;
/**
* <p> Title:根据数据集生成曲线图,饼图,柱状图,甘特图等</p>
* <p> Description:</p>
* <p> Copyright: Copyright (c) 2005 </p>
* <p> Company:rhui.co.,ltd </p>
* @author zhujw
* @version 1.0
*/
public class JFreeChartUtil {
/**
* 柱状图(一维,二维关系)
* @param title
* @param categoryAxisLabel
* @param valueAxisLabel
* @param dataset
* @return
*/
public static JFreeChart createHistogram(String title,
String categoryAxisLabel, String valueAxisLabel,
CategoryDataset dataset) {
//ChartFactory工厂类
JFreeChart chart = ChartFactory.createBarChart3D(title,
categoryAxisLabel, valueAxisLabel, dataset,
PlotOrientation.VERTICAL, true, false, false);
// PlotOrientation.VERTICAL:让平行柱垂直显示 PlotOrientation.HORIZONTAL 则让平行柱水平显示。
// 字体由模糊变清晰
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
// domainAxis.setVerticalCategoryLabels(false);
domainAxis.setVisible(true);
plot.setDomainAxis(domainAxis);
// plot图形设计:绘图集plot对象
ValueAxis rangeAxis = plot.getRangeAxis();
// 设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
// 设置最低的一个Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
// rangeAxis.set
plot.setRangeAxis(rangeAxis);
BarRenderer3D renderer = new BarRenderer3D();
// render 绘制工具
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置 Wall 的颜色
renderer.setWallPaint(Color.gray);
// 设置每种类型代表的柱的颜色
renderer.setSeriesPaint(0, Color.YELLOW);
renderer.setSeriesPaint(1, Color.GREEN);
renderer.setSeriesPaint(2, Color.RED);
renderer.setSeriesPaint(3, Color.CYAN);
renderer.setSeriesPaint(5, Color.ORANGE);
renderer.setSeriesPaint(4, Color.MAGENTA);
renderer.setSeriesPaint(6, Color.DARK_GRAY);
renderer.setSeriesPaint(7, Color.PINK);
renderer.setSeriesPaint(8, Color.black);
renderer.setSeriesPaint(9, new Color(0, 100, 255));
renderer.setSeriesPaint(10, new Color(0, 0, 255));
// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.25);// 为25%
// 显示每个柱的数值,并修改该数值的字体属性
renderer
.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);
// 设置柱的透明度
plot.setForegroundAlpha(0.6f);
// 设置地区、收入的显示位置
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
/*----------设置标题字体--------------------------*/
ValueAxis rAxis = plot.getRangeAxis();
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("sans-serif",Font.PLAIN,15));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,15));
/*------设置Y轴坐标上的文字-----------*/
rAxis.setTickLabelFont(new Font("sans-serif",Font.PLAIN,15));
/*------设置Y轴的标题文字------------*/
rAxis.setLabelFont(new Font("黑体",Font.PLAIN,15));
/*---------设置柱状体上的显示的字体---------*/
//renderer.setItemLabelGenerator(new LabelGenerator(0.0));
renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,12));
renderer.setItemLabelsVisible(true);
return chart;
}
/**
* 创建饼图
* @param dataset
* @param title
* @return
*/
public static JFreeChart createPieChart(String title,
DefaultPieDataset dataset) {
//ChartFactory工厂类
JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true,
true, true);
//设定饼图标题
chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
//定制子标题
//chart.addSubtitle(new TextTitle("2005质量技术监督局财务分析", new Font("隶书", Font.ITALIC, 12)));
//设定背景
chart.setBackgroundPaint(Color.white);
//饼图使用一个PiePlot
PiePlot pie = (PiePlot) chart.getPlot();
//设定显示格式(名称加百分比或数值)
//pie.setPercentFormatString("#,###0.0#%");
//设定百分比显示格式
pie.setBackgroundPaint(Color.white);
//pie.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));
//设定背景透明度(0-1.0之间)
pie.setBackgroundAlpha(0.6f);
//设定前景透明度(0-1.0之间)
pie.setForegroundAlpha(0.90f);
return chart;
}
/**
* 创建曲线图
* @param title
* @param subtitleStr
* @param domain
* @param range
* @param dataset
* @return
*/
public static JFreeChart JFreeChartSeriesChart(String title,
String subtitleStr, String domain, String range,
TimeSeriesCollection dataset) { //时间曲线元素
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
range, dataset, true, true, false);
TextTitle subtitle = new TextTitle(subtitleStr, new Font("黑体",
Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000,
Color.blue));
return chart;
}
/**
* 取得session中图像的地址
* @param chart
* @param session
* @param request
* @return
* @throws IOException
*/
public static String getGraphURL(JFreeChart chart, HttpSession session,
HttpServletRequest request, int width, int height)
throws IOException {
String filename = ServletUtilities.saveChartAsPNG(chart, width, height,
null, session);
String graphURL = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
return graphURL;
}
}