Java JFreeChart生成折线图保存

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class CreateJFreeChartXYLine {

    /**
     * 创建JFreeChart LineXY Chart(折线图)
     */
    public static void main(String[] args) {
        //步骤1:创建XYDataset对象(准备数据)
        XYDataset dataset = createXYDataset();
        //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置
        JFreeChart freeChart = createChart(dataset);
        //步骤3:将JFreeChart对象输出到文件,Servlet输出流等
        saveAsFile(freeChart, "template/lineXY.png", 600, 400);
    }


    // 保存为文件
    public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {
        FileOutputStream out = null;
        try {
            File outFile = new File(outputPath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(outputPath);
            // 保存为PNG
            ChartUtilities.writeChartAsPNG(out, chart, weight, height);
            // 保存为JPEG
            // ChartUtilities.writeChartAsJPEG(out, chart, weight, height);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }

    // 根据XYDataset创建JFreeChart对象
    public static JFreeChart createChart(XYDataset dataset) {
        // 创建JFreeChart对象:ChartFactory.createXYLineChart
        JFreeChart jfreechart = ChartFactory.createXYLineChart("XYLine Chart Demo", // 标题
                "年分", // categoryAxisLabel (category轴,横轴,X轴标签)
                "数量", // valueAxisLabel(value轴,纵轴,Y轴的标签)
                dataset, // dataset
                PlotOrientation.VERTICAL,
                true, // legend
                false, // tooltips
                false); // URLs
        // 使用CategoryPlot设置各种参数。以下设置可以省略。
        XYPlot plot = (XYPlot) jfreechart.getPlot();
        // 背景色 透明度
        plot.setBackgroundAlpha(0.5f);
        // 前景色 透明度
        plot.setForegroundAlpha(0.5f);
        // 其它设置可以参考XYPlot类
        return jfreechart;
    }

    /**
     * 创建XYDataset对象
     */

    private static XYDataset createXYDataset() {
        XYSeries xyseries1 = new XYSeries("One");
        xyseries1.add(1987, 50);
        xyseries1.add(1997, 20);
        xyseries1.add(2007, 30);
        XYSeries xyseries2 = new XYSeries("Two");
        xyseries2.add(1987, 20);
        xyseries2.add(1997, 10D);
        xyseries2.add(2007, 40D);
        XYSeries xyseries3 = new XYSeries("Three");
        xyseries3.add(1987, 40);
        xyseries3.add(1997, 30.0008);
        xyseries3.add(2007, 38.24);
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        xySeriesCollection.addSeries(xyseries1);
        xySeriesCollection.addSeries(xyseries2);
        xySeriesCollection.addSeries(xyseries3);
        return xySeriesCollection;
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李小雷一直在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值