您的位置:首页 > 其它

(5)Jfreechart环形图

2016-03-04 15:05 204 查看

1、环形效果图



2、详解

通过createRingChart创建环形图

JFreeChart chart5 = ChartFactory.createRingChart(“环形分布图测试”,

getPieOrRingChartDataSet(), true, true, Locale.CHINESE);

设置样式

chart.setBackgroundPaint(Color.white);

RingPlot ringplot = (RingPlot) chart.getPlot();

ringplot.setLabelBackgroundPaint(Color.WHITE);

//设置环形图的每个扇区的样式

ringplot.setSectionPaint(0, new Color(128, 100, 162));

ringplot.setSectionPaint(1, new Color(155, 187, 89));

ringplot.setSectionPaint(2, new Color(192, 80, 77));

// ringplot.setLabelLinksVisible(false);

// 设置饼状图和环形图的显示数字。0代表显示文字说明,1代表显示数字,2代表显示数字以百分比的方式如果多个结合{0}:{1}

ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator(“{2}”));

ringplot.setSimpleLabels(true);

ringplot.setLabelBackgroundPaint(new Color(1.0F, 0.75F, 0.0F, 0.0F));

ringplot.setLabelFont(new Font(“sans-serif”, Font.PLAIN, 12));

ringplot.setLabelOutlinePaint(new Color(1.0F, 0.75F, 0.0F, 0.0F));

ringplot.setLabelPaint(Color.white);

ringplot.setLabelFont(new Font(“微软雅黑”, Font.PLAIN, 24));

ringplot.setLabelShadowPaint(new Color(1.0F, 0.75F, 0.0F, 0.0F));

TextTitle title = chart.getTitle();

title.setFont(new Font(“sans-serif”, Font.PLAIN, 24));

// 设置深度,即带颜色圆环的宽度

ringplot.setSectionDepth(0.5d);

// 设置环形图背景颜色

ringplot.setBackgroundPaint(Color.white);

设置数据集

private static PieDataset getPieOrRingChartDataSet() {

DefaultPieDataset dataset = new DefaultPieDataset();

dataset.setValue(“2.0分-3.0分”, new Double(10));

dataset.setValue(“3.0分-4.0分”, new Double(10));

dataset.setValue(“4.0分-5.0分”, new Double(80));

return dataset;

}

在main方法中执行

ChartFrame pieFrame = new ChartFrame(“水果销量分布图”, createRingChart2());

pieFrame.pack(); pieFrame.setVisible(true);

案例

package com.lizj_cdoc.jfreechartUtil;

import java.awt.Color;

import java.awt.Font;

import java.util.Locale;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartFrame;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.labels.StandardPieSectionLabelGenerator;

import org.jfree.chart.plot.RingPlot;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.general.DefaultPieDataset;

import org.jfree.data.general.PieDataset;

public class CreateRingChart {

public static void main(String[] args) {

final ChartFrame frame = new ChartFrame(“Interval Bar Chart Demo”, createRingChart());

frame.pack();

frame.setVisible(true);

}

public static JFreeChart createRingChart() {

JFreeChart chart5 = ChartFactory.createRingChart(“环形分布图测试”,

getPieOrRingChartDataSet(), true, true, Locale.CHINESE);

setRingChart(chart5);

return chart5;

}

public static void setRingChart(JFreeChart chart) {

chart.setBackgroundPaint(Color.white);

RingPlot ringplot = (RingPlot) chart.getPlot();

ringplot.setLabelBackgroundPaint(Color.WHITE);

ringplot.setSectionPaint(0, new Color(128, 100, 162));

ringplot.setSectionPaint(1, new Color(155, 187, 89));

ringplot.setSectionPaint(2, new Color(192, 80, 77));

// ringplot.setLabelLinksVisible(false);

// 设置饼状图和环形图的显示数字。0代表显示文字说明,1代表显示数字,2代表显示数字以百分比的方式如果多个结合{0}:{1}

ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator(“{2}”));

ringplot.setSimpleLabels(true);

ringplot.setLabelBackgroundPaint(new Color(1.0F, 0.75F, 0.0F, 0.0F));

ringplot.setLabelFont(new Font(“sans-serif”, Font.PLAIN, 12));

ringplot.setLabelOutlinePaint(new Color(1.0F, 0.75F, 0.0F, 0.0F));

ringplot.setLabelPaint(Color.white);

ringplot.setLabelFont(new Font(“微软雅黑”, Font.PLAIN, 24));

ringplot.setLabelShadowPaint(new Color(1.0F, 0.75F, 0.0F, 0.0F));

TextTitle title = chart.getTitle();

title.setFont(new Font(“sans-serif”, Font.PLAIN, 24));

// 设置深度,即带颜色圆环的宽度

ringplot.setSectionDepth(0.5d);

// 设置环形图背景颜色

ringplot.setBackgroundPaint(Color.white);

chart.getLegend().setItemFont(new Font("微软雅黑", Font.BOLD, 12));
}
private static PieDataset getPieOrRingChartDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
/* dataset.setValue("1.0分-2.0分", new Double(0)); */
dataset.setValue("2.0分-3.0分", new Double(10));
dataset.setValue("3.0分-4.0分", new Double(10));
dataset.setValue("4.0分-5.0分", new Double(80));

return dataset;
}


}

这里写代码片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息