码迷,mamicode.com
首页 > 其他好文 > 详细

JFreeChart

时间:2016-08-03 20:23:23      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

花了四个小时给同学写的。还行吧,原来都没有用过到处找资料写的。

package DrawLine;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;

import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Created by chaoqun.jiang on 8/3/2016.
 */
public class DrawLine {

    private JFreeChart freeChart;

    private JFreeChart rChart = null;     //图表对象
    public String chartTitle = "title";        //图表标题
    public String chartXdesc = "X";        //X轴标题
    public String chartYdesc = "Y";        //Y轴标题
    public String chartSeriesDesc = "desc";   //曲线说明
    public int graphWidth = 600;          //默认宽度
    public int graphHigh = 400;           //默认高度
    public String timeFormat = "yyyy-MM-dd"; //yyyy-MM-dd-HH-mm:ss:ms       年yyyy 月MM 日dd 时(大写为24进制,小写为12进制) 分mm 秒ss 微妙ms

    //定义生成图形样式
    public JFreeChart createNewChart(XYDataset xyDataset){
        //定义
        JFreeChart freeChart = ChartFactory.createTimeSeriesChart(chartTitle, chartXdesc, chartYdesc, xyDataset, true, true, false);
        //背景色
        freeChart.setBackgroundPaint(Color.WHITE);

        //设置(x,y)的性质
        XYPlot xyPlot = freeChart.getXYPlot();
        xyPlot.setOrientation(PlotOrientation.VERTICAL);

        xyPlot.setBackgroundPaint(Color.lightGray);
        xyPlot.setDomainGridlinePaint(Color.white);
        xyPlot.setRangeGridlinePaint(Color.white);

        xyPlot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));//图片区与坐标轴的距离
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateAxis axis = (DateAxis) xyPlot.getDomainAxis();
        axis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1, dateFormat));

        return freeChart;
    }

}
package Import;

import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chaoqun.jiang on 8/3/2016.
 */
public class ImportData {

    public XYDataset readFromDat(Date start, Date end) throws IOException, ParseException {

        TimeSeriesCollection dataset = new TimeSeriesCollection();

        BufferedReader bufferedReader;
        Map<String, String> dataMap = new HashMap<String, String>();
        bufferedReader = new BufferedReader(new FileReader("E:\\baozhen_2011.dat"));

        //按行处理字符串
        String valueString = null;

        TimeSeries series = new TimeSeries("we are null");

        while ((valueString = bufferedReader.readLine()) != null) {
            String temp = valueString.substring(0, 10);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date dateTemp = dateFormat.parse(temp);

            //如果时间符合要求,就把时间加进去
            if (dateTemp.getTime() >= start.getTime() && dateTemp.getTime() <= end.getTime()) {
                //转换(x, y)
                DateFormat deepDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String deepTemp = valueString.substring(0, 20);
                Date newPointTime = deepDateFormat.parse(deepTemp);
                Double date = Double.parseDouble(valueString.substring(37, 44));
                System.out.println("date:" + newPointTime.toString() + "     number:" + valueString.substring(37, 44));
                series.addOrUpdate(new Second(newPointTime), new Double(date));
            }
        }

        dataset.addSeries(series);
        return dataset;

    }

}
package Main;

import DrawLine.DrawLine;
import Import.ImportData;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYDataset;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
 * Created by chaoqun.jiang on 8/3/2016.
 */
public class main {

    public static void main(String[] arg) throws IOException, ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Scanner scanner = new Scanner(System.in);// 创建输入流扫描器

        System.out.println("请输入起始时间:");
        String begin = scanner.nextLine();
        Date small = dateFormat.parse(begin);

        System.out.println("请输入结束时间:");
        String end = scanner.nextLine();
        Date big = dateFormat.parse(end);

        ImportData importData = new ImportData();
        //获得了坐标点
        XYDataset xyPoint = importData.readFromDat(small, big);

        //获得JChart对象
        DrawLine drawLine = new DrawLine();
        JFreeChart jFreeChart = drawLine.createNewChart(xyPoint);

        //放到frame里面,显示
        ChartFrame frame = new ChartFrame("TestChart", jFreeChart);
        frame.pack();
        frame.setVisible(true);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>data</groupId>
    <artifactId>data</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.19</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jfree/jcommon -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.23</version>
        </dependency>

    </dependencies>


</project>

不解释了,好累,我去歇息一下。

 

JFreeChart

标签:

原文地址:http://www.cnblogs.com/friday92/p/5734135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!