package com.leniao.highchartsUtils;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.TextAnchor;
/*
* 折线图
*/
public class LineChartsUtils extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public LineChartsUtils(String s) {
super(s);
setContentPane(createDemoLine());
}
private Container createDemoLine() {
return null;
}
// 生成图表主对象JFreeChart
public static JFreeChart createChart1(String chartTitle, String chartXdesc, String chartYdesc,
DefaultCategoryDataset linedataset) {
// 图表标题
// chartTitle,
// chartXdesc, // 横轴的显示标签
// chartYdesc, // 纵轴的显示标签
// dataset, // 数据集. 就是CategoryDataset类的实例对象
// PlotOrientation.VERTICAL, // 图表方向:水平、垂直 || 显示方式
// true, // 是否显示图例(标题)
// true, // 是否生成提示工具 tooltips || 是否启动热键
// false // 是否生成URL链接
JFreeChart chart = ChartFactory.createLineChart(chartTitle, chartXdesc, chartYdesc, linedataset,
PlotOrientation.VERTICAL, true, true, false);
// 设置标题
// ===============为了防止中文乱码:必须设置字体
chart.setTitle(new TextTitle(chartTitle, new Font("黑体", Font.ITALIC, 22)));
LegendTitle legend = chart.getLegend(); // 获取图例
legend.setItemFont(new Font("宋体", Font.BOLD, 12)); // 设置图例的字体,防止中文乱码
CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
plot.setBackgroundPaint(new Color(255, 255, 204));
plot.setForegroundAlpha(0.65F); // 设置前景色透明度
// 设置横虚线可见
plot.setRangeGridlinesVisible(true);
// 虚线色彩
plot.setRangeGridlinePaint(Color.gray);
CategoryAxis h = plot.getDomainAxis(); // 获取x轴
h.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示
h.setLabelFont(new Font("宋体", Font.BOLD, 12));// 设置字体,防止中文乱码
h.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// 轴数值
// h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 12)); // Y轴设置字体,防止中文乱码
// 折线上面显示数据
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
DecimalFormat decimalformat1 = new DecimalFormat("##.##");// 数据点显示数据值的格式
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
// 上面这句是设置数据项标签的生成器
renderer.setItemLabelsVisible(true);// 设置项标签显示
renderer.setBaseItemLabelsVisible(true);// 基本项标签显示
// 上面这几句就决定了数据点按照设定的格式显示数据值
renderer.setShapesFilled(Boolean.TRUE);// 在数据点显示实心的小图标
renderer.setShapesVisible(true);// 设置显示小图标
return chart;
}
/**
* 在本地生成图片的方法
*
* @param destPath
* @param chart
* @param width
* @param heigth
* @return
*/
private static boolean drawToOutputStream(String destPath, JFreeChart chart, int width, int heigth) {
FileOutputStream fos = null;
boolean returnValue = true;
try {
fos = new FileOutputStream(destPath);
ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流
chart, // 图表对象
width, // 宽
heigth, // 高
null); // ChartRenderingInfo信息
} catch (IOException e) {
e.printStackTrace();
returnValue = false;
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return returnValue;
}
/**
* 测试曲线图
*/
public static void demo() {
// 初始化数据
String chartTitle = "访问量统计图形";
String chartXdesc = "时间";
String chartYdesc = "访问量";
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
// 各曲线名称
String series1 = "冰箱";
String series2 = "彩电";
String series3 = "洗衣机"; // 横轴名称(列名称)
String type1 = "1月";
String type2 = "2月";
String type3 = "3月";
linedataset.addValue(0.0, series1, type1);
linedataset.addValue(4.2, series1, type2);
linedataset.addValue(3.9, series1, type3);
linedataset.addValue(1.0, series2, type1);
linedataset.addValue(5.2, series2, type2);
linedataset.addValue(7.9, series2, type3);
linedataset.addValue(2.0, series3, type1);
linedataset.addValue(9.2, series3, type2);
linedataset.addValue(8.9, series3, type3);
// 生成图表
JFreeChart chart = createChart1(chartTitle, chartXdesc, chartYdesc, linedataset);
// 生成图片
drawToOutputStream("C:\\Users\\Administrator\\Desktop\\折线图.JPG", chart, 640, 480);
}
public static void main(String[] args) {
demo();
}
}