JFreeChart 画饼状图 颜色 字体 样式的设置
2009-04-27 08:54
507 查看
一个画饼状图的方法:
/**
* 画饼状图
* @param dataset DefaultPieDataset
* @return JFreeChart
*/
public static JFreeChart getPieChart(DefaultPieDataset dataset,
String title) {
//创建JFreeChart,都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式
JFreeChart chart = ChartFactory.createPieChart(title,
dataset,
true,
false,
false);
//设定图片标题
chart.setTitle(new TextTitle(title, new Font("隶书", Font.BOLD, 16)));
//设定背景
chart.setBackgroundPaint(Color.white);
//饼图使用一个PiePlot
PiePlot pie = (PiePlot) chart.getPlot();
//设定百分比显示格式
pie.setLabelGenerator(new StandardPieItemLabelGenerator("{0}={1}({2})",
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//指定 section 轮廓线的颜色
pie.setBaseSectionOutlinePaint(new Color(0xF7, 0x79, 0xED));
//指定 section 轮廓线的厚度
pie.setSectionOutlineStroke(new BasicStroke(0));
//指定 section 的色彩
pie.setSectionPaint(0, new Color(0xF7, 0x79, 0xED));
pie.setNoDataMessage("没有数据!");
pie.setNoDataMessagePaint(Color.blue);
pie.setCircular(true);
pie.setLabelGap(0.01D); //间距
pie.setBackgroundPaint(Color.white);
pie.setLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));
//设定背景透明度(0-1.0之间)
pie.setBackgroundAlpha(0.6f);
//设定前景透明度(0-1.0之间)
pie.setForegroundAlpha(0.8f);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setWallPaint(Color.lightGray);
return chart;
}
开始生成饼状图:
JFreeChart chart = CategoryChart.getPieChart(dataset, "饼状图");
FileOutputStream jpg = null;
try {
jpg = new FileOutputStream(jpgname);
ChartUtilities.writeChartAsPNG(jpg, chart, 500, 400);
// ChartUtilities.writeChartAsJPEG(jpg, 100, chart, 500, 400, null);
其中ChartUtilities.writeChartAsJPEG(jpg, 100, chart, 500, 400, null);中 第二个参数指的是生成饼状图的品质,也就是图片的质量,数值越高颜色应该越清楚。
修改饼状图 指定 颜色和元素相对应:
比如,有一个问题,选择ABCD四个答案的比例分别是:10%,30%,20%,40%。希望饼图中A的颜色是red,B的颜色是green,C的颜色是yellow,D的颜色是blue。
可以这么做:
1.准备好dataset,这一步就不讲了,pieChart有其要求的dataset格式。
2.书写一个创建pieChart的方法:
public static JFreeChart createPieChart(DefaultPieDataset dataset, String mytitle) {
JFreeChart chart = ChartFactory.createPieChart(mytitle,dataset,true, true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setNoDataMessage("没有可供使用的数据!");
plot.setSectionPaint("A", Color.RED);
plot.setSectionPaint("B", Color.GREEN);
plot.setSectionPaint("C", Color.YELLOW);
plot.setSectionPaint("D", Color.BLUE);
//就是这个地方,实现了对各个key对应饼图区域的颜色设置
// PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setCircular(false);
plot.setLabelGap(0.02);
//do more things here
return chart;
}
注意到plot.setSectionPaint("A", Color.RED);这个方法了吧,该方法就是设置颜色的关键语句。如果没有此方法,则JFreeChart在其创建过程中,会调用默认的颜色来给各个区域进行颜色设置。
需要注意的是
plot.setSectionPaint("A", Color.RED);此类方法只有1.0.6版本支持,1.0.6以前的版本中
setSectionPaint方法的第一个参数是整形!如:plot.setSectionPaint(0, Color.RED);
摘抄于/article/4341828.html
/**
* 画饼状图
* @param dataset DefaultPieDataset
* @return JFreeChart
*/
public static JFreeChart getPieChart(DefaultPieDataset dataset,
String title) {
//创建JFreeChart,都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式
JFreeChart chart = ChartFactory.createPieChart(title,
dataset,
true,
false,
false);
//设定图片标题
chart.setTitle(new TextTitle(title, new Font("隶书", Font.BOLD, 16)));
//设定背景
chart.setBackgroundPaint(Color.white);
//饼图使用一个PiePlot
PiePlot pie = (PiePlot) chart.getPlot();
//设定百分比显示格式
pie.setLabelGenerator(new StandardPieItemLabelGenerator("{0}={1}({2})",
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//指定 section 轮廓线的颜色
pie.setBaseSectionOutlinePaint(new Color(0xF7, 0x79, 0xED));
//指定 section 轮廓线的厚度
pie.setSectionOutlineStroke(new BasicStroke(0));
//指定 section 的色彩
pie.setSectionPaint(0, new Color(0xF7, 0x79, 0xED));
pie.setNoDataMessage("没有数据!");
pie.setNoDataMessagePaint(Color.blue);
pie.setCircular(true);
pie.setLabelGap(0.01D); //间距
pie.setBackgroundPaint(Color.white);
pie.setLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));
//设定背景透明度(0-1.0之间)
pie.setBackgroundAlpha(0.6f);
//设定前景透明度(0-1.0之间)
pie.setForegroundAlpha(0.8f);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setWallPaint(Color.lightGray);
return chart;
}
开始生成饼状图:
JFreeChart chart = CategoryChart.getPieChart(dataset, "饼状图");
FileOutputStream jpg = null;
try {
jpg = new FileOutputStream(jpgname);
ChartUtilities.writeChartAsPNG(jpg, chart, 500, 400);
// ChartUtilities.writeChartAsJPEG(jpg, 100, chart, 500, 400, null);
其中ChartUtilities.writeChartAsJPEG(jpg, 100, chart, 500, 400, null);中 第二个参数指的是生成饼状图的品质,也就是图片的质量,数值越高颜色应该越清楚。
修改饼状图 指定 颜色和元素相对应:
比如,有一个问题,选择ABCD四个答案的比例分别是:10%,30%,20%,40%。希望饼图中A的颜色是red,B的颜色是green,C的颜色是yellow,D的颜色是blue。
可以这么做:
1.准备好dataset,这一步就不讲了,pieChart有其要求的dataset格式。
2.书写一个创建pieChart的方法:
public static JFreeChart createPieChart(DefaultPieDataset dataset, String mytitle) {
JFreeChart chart = ChartFactory.createPieChart(mytitle,dataset,true, true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setNoDataMessage("没有可供使用的数据!");
plot.setSectionPaint("A", Color.RED);
plot.setSectionPaint("B", Color.GREEN);
plot.setSectionPaint("C", Color.YELLOW);
plot.setSectionPaint("D", Color.BLUE);
//就是这个地方,实现了对各个key对应饼图区域的颜色设置
// PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
plot.setCircular(false);
plot.setLabelGap(0.02);
//do more things here
return chart;
}
注意到plot.setSectionPaint("A", Color.RED);这个方法了吧,该方法就是设置颜色的关键语句。如果没有此方法,则JFreeChart在其创建过程中,会调用默认的颜色来给各个区域进行颜色设置。
需要注意的是
plot.setSectionPaint("A", Color.RED);此类方法只有1.0.6版本支持,1.0.6以前的版本中
setSectionPaint方法的第一个参数是整形!如:plot.setSectionPaint(0, Color.RED);
摘抄于/article/4341828.html
相关文章推荐
- JFreeChart 画饼状图 颜色 字体 样式的设置
- PHPExcel-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- 设置Linux shell的输出效果[字体颜色,样式等]
- 同一文本设置不用颜色样式字体
- IOS7/8中设置状态栏字体颜色样式
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能
- C#里面设置ListView单列(某个单元格的样式:颜色和字体等)。
- 【C#.NET】设置dataGridView1样式颜色字体
- CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)
- iOS中字体样式的设置、颜色、空心、删除线、阴影、斜体、扁平化
- JTextPane例子,演示为文字设置字体、字号、样式、颜色、背景色和插入图片功能
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- C#里面设置ListView单列(某个单元格的样式:颜色和字体等)。
- TextView中各个部分的文本来设置字体,大小,颜色,样式
- CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)
- python—matplotlib数据可视化实例注解系列-----设置标注字体样式(matplotlib颜色库)
- Asp.NET中设置颜色、字体和边框样式
- Java中怎么设置JLabel的字体样式,大小,颜色?