package com.hitangjun.jfreechart.demo;

/**
 * =============================================================
 * JFreeChart开发:利用JFreeChart开发实时曲线
 * =============================================================
 */
// 导入java2d包
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

public class TimeSeriesDemo1 extends JFrame implements Runnable, ActionListener {
 // 时序图数据集
// private TimeSeries timeseries;
 private TimeSeries timeseries;
  XYSeries s3 = new XYSeries("次序值");

 // Value坐标轴初始值
 private double lastValue;

 static Class class$org$jfree$data$time$Millisecond;

 static Thread thread1;
 
 private javax.swing.JPanel jPanel1;
 int step = 10;

 public static void main(String[] args) {
  TimeSeriesDemo1 TimeSeriesDemo1 = new TimeSeriesDemo1();
  TimeSeriesDemo1.pack();
  RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo1);
  TimeSeriesDemo1.setVisible(true);
  startThread();
 }

 public void run() {
  while (true) {
   try {
//    // 根据实际需要在此处加入需要执行的代码
    double d = 0.9D + 0.2D * Math.random();
//    lastValue = lastValue * d;
//    Millisecond millisecond = new Millisecond();
//    System.out.println("Now=" + millisecond.toString());
//    timeseries.add(millisecond, lastValue);
//    Thread.sleep(300);
    // 根据实际需要在此处加入需要执行的代码
//    double d = 0.9D + 0.2D * Math.random();
    lastValue = lastValue * d;
    System.out.println("step = "+step+",lastvalue = "+lastValue);
    s3.add(step, lastValue);
    step+=5;
    Thread.sleep(1000);
   } catch (InterruptedException e) {
   }
  }
 }

 private void initComponents(Component comp) {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("窗口demo");
        setLocationByPlatform(true);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("panel标题"));
        jPanel1.setToolTipText("串口设置");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 350, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );

//        jPanel1.add( comp );
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(comp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(472, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(comp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(117, Short.MAX_VALUE))
        );

        jPanel1.getAccessibleContext().setAccessibleName("饭店");
        pack();
    }
 public static void startThread() {
  thread1.start();
 }

 public void actionPerformed(ActionEvent e) {
  if (e.getActionCommand().equals("EXIT")) {
   thread1.interrupt();
   System.exit(0);
  }
 }

 public TimeSeriesDemo1() {
     
  // super(new BorderLayout());
  thread1 = new Thread(this);
  lastValue = 100D;
  // 创建时序图对象
  timeseries = new TimeSeries(
    "Random Data",
    TimeSeriesDemo1.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo1.class$org$jfree$data$time$Millisecond
      : (TimeSeriesDemo1.class$org$jfree$data$time$Millisecond = TimeSeriesDemo1
        .getClass("org.jfree.data.time.Millisecond")));
  TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(
    timeseries);
  
  XYSeriesCollection dataset = new XYSeriesCollection(s3);
        dataset.addSeries( s3 );
  // 创建图表面板
  ChartPanel chartpanel = new ChartPanel(
    createChart(dataset));
  chartpanel.setPreferredSize(new Dimension(500, 270));

//  JPanel jpanel = new JPanel();
//  jpanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));// 边距为4
//  JButton jbutton = new JButton("退出");
//  jbutton.setActionCommand("EXIT");
//  jbutton.addActionListener(this);
//  jpanel.add(jbutton);
//  getContentPane().add(chartpanel,"South");
//  getContentPane().add(jpanel, "South");
  initComponents(chartpanel);
 }

 private JFreeChart createChart(XYDataset xydataset) {
  JFreeChart jfreechart = ChartFactory.createXYLineChart("时序图例子",
    "时间", "温度值", xydataset, PlotOrientation.VERTICAL, true, true, false);
  XYPlot xyplot = jfreechart.getXYPlot();
  // 横纵坐标设定
  
  ValueAxis valueaxis = xyplot.getDomainAxis();
  valueaxis.setRange( 1, 300D );
  valueaxis.setAutoRange(true);
  valueaxis.setFixedAutoRange(300D);

  valueaxis = xyplot.getRangeAxis();
  valueaxis.setRange(0.0D, 200D);
  configFont(jfreechart);
  return jfreechart;
 }

 static Class getClass(String s) {
  Class cls = null;
  try {
   cls = Class.forName(s);
  } catch (ClassNotFoundException cnfe) {
   throw new NoClassDefFoundError(cnfe.getMessage());
  }
  return cls;
 }
 
 /**
     * 配置字体 
     * @param chart JFreeChart 对象
     */
    private void configFont(JFreeChart chart){
     // 配置字体
     Font xfont = new Font("宋体",Font.PLAIN,12) ;// X轴
     Font yfont = new Font("宋体",Font.PLAIN,12) ;// Y轴
     Font kfont = new Font("宋体",Font.PLAIN,12) ;// 底部
     Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题
     XYPlot plot = chart.getXYPlot();// 重生之大文豪www.dwhao.com图形的绘制结构对象
     
     // 图片标题
     chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
     
     // 底部
     chart.getLegend().setItemFont(kfont);
     
     // X 轴
     ValueAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLabelFont(xfont);// 轴标题
        domainAxis.setTickLabelFont(xfont);// 轴数值  
        domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色
//        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示 
        
     // Y 轴
     ValueAxis rangeAxis = plot.getRangeAxis();   
        rangeAxis.setLabelFont(yfont); 
        rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色
        rangeAxis.setTickLabelFont(yfont);  
        
    }

}