15832144755
2021-08-27 3516f02277035cdc7ff137422709b16efe97ca4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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("导出成功!");
    }
 
}