package com.hxzkoa.util;
|
|
import java.awt.Font;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import javax.swing.filechooser.FileSystemView;
|
|
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.axis.ValueAxis;
|
import org.jfree.chart.plot.CategoryPlot;
|
import org.jfree.chart.plot.PlotOrientation;
|
import org.jfree.chart.title.LegendTitle;
|
import org.jfree.chart.title.TextTitle;
|
import org.jfree.data.DefaultKeyedValues;
|
import org.jfree.data.category.CategoryDataset;
|
import org.jfree.data.general.DatasetUtilities;
|
|
import com.hxzkoa.json.tb_history_power;
|
|
/**
|
* XY折线图
|
*
|
*/
|
public class LineXy {
|
static JFreeChart chart = null;
|
|
/**
|
* 生成JFreeChart
|
*
|
* @throws IOException
|
*/
|
public static void getJFreeChart_hp(String name, List<tb_history_power> powerList,String sysPath) throws IOException {
|
CategoryDataset dataset = null;
|
DefaultKeyedValues keyedValues = new DefaultKeyedValues();
|
int size = powerList.size();
|
if (size != 0) {
|
for (int i = 0; i < size; i++) {
|
tb_history_power pow = powerList.get(i);
|
int power = Integer.parseInt(pow.getPower());
|
keyedValues.addValue(pow.getTime(), power);
|
}
|
dataset = DatasetUtilities.createCategoryDataset(name + "电量", keyedValues);
|
chart = ChartFactory.createLineChart("", // 图表标题
|
"日期", // x轴标签
|
"电量(%)", // y轴标签
|
dataset, // 数据集
|
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
|
true, // 是否显示图例
|
false, // 是否生成工具
|
false // 是否生成URL链接
|
);
|
// 修改字体
|
updateFont(chart);
|
}
|
chart.setTitle(new TextTitle(name + "电量分析", new Font("宋体", Font.BOLD + Font.ITALIC, 20)));
|
LegendTitle legend = chart.getLegend(0);// 设置Legend
|
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
|
//File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
|
//String path = desktopDir.getAbsolutePath()+ "\\" + name + "电量分析.jpeg";
|
// String filePath = "C:/powerAnalysis/";
|
String filePath = sysPath+"hxzk/image/powerAnalysis/";
|
File dir = new File(filePath);
|
if(!dir.isDirectory()){
|
dir.mkdirs();
|
}
|
String fileName = filePath + name + "电量分析.jpeg";
|
OutputStream os = new FileOutputStream(fileName);// 图片是文件格式的,故要用到FileOutputStream用来输出。
|
ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
|
// 使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。
|
os.close();// 关闭输出流
|
}
|
|
/**
|
* 修改字体
|
*
|
* @param chart
|
*/
|
private static void updateFont(JFreeChart chart) {
|
|
// 标题
|
TextTitle textTitle = chart.getTitle();
|
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
|
LegendTitle legendTitle = chart.getLegend();
|
legendTitle.setItemFont(new Font("宋体", Font.PLAIN, 14));
|
// 图表
|
CategoryPlot categoryPlot = chart.getCategoryPlot();
|
|
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
|
// X轴字体
|
categoryAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 14));
|
// X轴标签字体
|
categoryAxis.setLabelFont(new Font("宋体", Font.PLAIN, 14));
|
|
ValueAxis valueAxis = categoryPlot.getRangeAxis();
|
// y轴字体
|
valueAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 14));
|
// y轴标签字体
|
valueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 14));
|
}
|
|
public static void main(String[] args) throws Exception {
|
// String tagid = "4004";
|
// List<tb_history_power> powerList = new ArrayList();
|
// tb_history_power p1 = new tb_history_power();
|
// tb_history_power p2 = new tb_history_power();
|
// p1.setPower("37");
|
// p1.setTime("03-28");
|
// p2.setPower("35");
|
// p2.setTime("03-19");
|
// powerList.add(p1);
|
// powerList.add(p2);
|
//// getJFreeChart_hp(tagid, powerList,);
|
// System.out.println("导出成功!");
|
}
|
|
}
|