package org.com.testJfreechart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class TestJfreeChart3 {
//生成折线图的宽高
private static final int width = 900;
private static final int height = 400;
//折线图标题、横坐标、纵坐标
private static final String title="利益演示图";
private static final String xString="被保险人年龄";
private static final String yString="个人账户价值(万元)";
private static final String Z_String="部分领取(元)";
//折线图不同颜色线条所代表的含义 个人账户价值低中高 、部分领取
private static final String funderbalLow = "个人账户价值(低)";
private static final String funderbalMid = "个人账户价值(中)";
private static final String funderbalHig = "个人账户价值(高)";
private static final String getPart = "部分领取";
//字体颜色
private static final String blackBold ="黑体";
private static final String luShu ="隶书";
//输出图片png名称及路径
private static final String outFilePath ="D:/out_picture.png";
//折线图 JfreeChart
private static JFreeChart creatChart(XYDataset dataset) {
//创建XYLineChart
JFreeChart chart=ChartFactory.createXYLineChart(title, xString, //X轴标签
yString, //Y轴标签
dataset, PlotOrientation.VERTICAL, true, false, false);
//使用XYPlot设置各种参数
XYPlot xyPlot=chart.getXYPlot();
//设置背景色透明度
xyPlot.setBackgroundAlpha(0.5f);
//设置前景色透明度
xyPlot.setForegroundAlpha(0.5f);
// 曲线图
XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer)xyPlot.getRenderer();
//设置线条为数据点
xyLineAndShapeRenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1,1.0F, new float[] { 1F, 6F }, 0.0F));
xyLineAndShapeRenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1,1.0F, new float[] { 1F, 6F }, 0.0F));
xyLineAndShapeRenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1,1.0F, new float[] { 1F, 6F }, 0.0F));
// 设置线颜色
// “个人账户价值”曲线的颜色: 曲线颜色 高 紫红色RGB(156,0,88), 中 红色 RGB(211,17,69), 低 蓝色 RGB( 0, 115, 174) 以符合公司VI3.0
xyLineAndShapeRenderer.setSeriesPaint(0, new Color(0, 115, 174));
xyLineAndShapeRenderer.setSeriesPaint(1, new Color(211, 17, 69));
xyLineAndShapeRenderer.setSeriesPaint(2, new Color(156, 0, 88));
xyLineAndShapeRenderer.setSeriesPaint(3, new Color(30, 0, 88));//部分领取颜色
TextTitle textTitle=new TextTitle("",new Font(blackBold,Font.BOLD,12));
chart.addSubtitle(textTitle);
chart.setTitle(new TextTitle(title, new Font(luShu, Font.ITALIC, 15)));
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0,1000,Color.BLUE));
//解决中文乱码问题 ValueAxis为横坐标 NumberAxis为纵坐标
Font font = new Font("SimSun", 10, 20);
NumberAxis rangeAxis = (NumberAxis) xyPlot.getRangeAxis();
rangeAxis.setLabelFont(font);
ValueAxis domainAxis = xyPlot.getDomainAxis();
domainAxis.setLabelFont(font);
//解决折线图最下方的中文乱码
chart.getLegend().setItemFont(font);
// 输出文件到指定目录
try {
ChartUtilities.saveChartAsPNG(new File(outFilePath), chart, width, height);
} catch (IOException exz) {
System.out.print("....Cant’t Create image File");
}
return chart;
}
private static XYDataset creatXYDataset(Object[] datas) {
//创建时间数据源
XYSeriesCollection xySeriesCollection=new XYSeriesCollection();
XYSeries xyLineSeries1 = new XYSeries(funderbalLow);
XYSeries xyLineSeries2 = new XYSeries(funderbalMid);
XYSeries xyLineSeries3 = new XYSeries(funderbalHig);
XYSeries xyBarSeries = new XYSeries(getPart);
Map<Integer,Double> map = new HashMap<Integer, Double>();
int mapLength = 0;
if(datas != null && datas.length>0){
map=(HashMap<Integer, Double>) datas[0];
if(map != null && map.size()>0){
mapLength = map.size();
for(int i=0,j=0;i<mapLength && j<datas.length;i++){
Map<Integer,Double> data = new HashMap<Integer, Double>();
data=(Map<Integer, Double>) datas[j];
if(j==0){
xyLineSeries1.add(i, data.get(i));//设置横纵坐标值
}else if(j==1){
xyLineSeries2.add(i, data.get(i));
}else if(j==2){
xyLineSeries3.add(i,data.get(i));
}else{
xyBarSeries.add(i, data.get(i));
}
if(i==mapLength-1){
j++;
i=-1;
}
}
}
}
xySeriesCollection.addSeries(xyLineSeries1);
xySeriesCollection.addSeries(xyLineSeries2);
xySeriesCollection.addSeries(xyLineSeries3);
xySeriesCollection.addSeries(xyBarSeries);
return xySeriesCollection;
}
//模拟填充数据 总共4中数据 key:横坐标 value:纵坐标
public static Object[] getDatas(){
Object[] objects=new Object[4];
for(int i = 0;i<objects.length;i++){
Map<Integer,Double> map = new HashMap<Integer, Double>();
for(int j=0;j<15;j++){
map.put(j,Math.random());
}
objects[i]=map;
}
return objects;
}
public static void main(String[] args) {
//第一步,创建XYDataSet对象(接口) 准备数据
XYDataset dataset=creatXYDataset(getDatas());
//第二部,根据sataSet对象生成JfreeChart,并做相应设置
creatChart(dataset);
}
}
----------注:需要导入jar包。