Topic: 请问一个jFreeChart曲线图的问题???急!!!

  Print this page

1.请问一个jFreeChart曲线图的问题???急!!! Copy to clipboard
Posted by: wh_44448
Posted on: 2006-09-12 15:44

我想做一个时间曲线图,x坐标用季度,不知那位高手可以指点一下,感谢!!!

2.Re:请问一个jFreeChart曲线图的问题???急!!! [Re: wh_44448] Copy to clipboard
Posted by: jackchengen
Posted on: 2006-09-16 14:29

public class ChartPanel extends javax.swing.JPanel{
//...................
public void paint(Graphics g){
//用g来画图
}

//..................
}

3.Re:请问一个jFreeChart曲线图的问题???急!!! [Re: wh_44448] Copy to clipboard
Posted by: jackchengen
Posted on: 2006-09-16 14:30

package monitor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;

import javax.swing.BorderFactory;

import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
import javax.swing.JFrame;

/**
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
* Builder, which is free for non-commercial use. If Jigloo is being used
* commercially (ie, by a corporation, company or business for any purpose
* whatever) then you should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details. Use of Jigloo implies
* acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
* PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
* ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class HistoryPanel extends javax.swing.JPanel {
  private double grid = 12;

  private double baseX = grid;

  private int speedFactor = 6;

  private LinkedList list;

  private int total = 255;
  private MainFrame mf;

  /**
   * @param mf
   */
  public HistoryPanel(MainFrame mf) {
    this();
    this.mf = mf;
  }

  /**
   * Auto-generated main method to display this JPanel inside a new JFrame.
   */
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new HistoryPanel());
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    for (int i = 0; i < 100; i++) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      frame.repaint();
    }
  }

  public HistoryPanel() {
    super();
    initGUI();
    list=new LinkedList();
  }

  /*
   * (non-Javadoc)
   *
   * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
   */
  protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponentPresent;
    Graphics2D g2 = (Graphics2D) g;
    double leftX = 0;
    double topY = 0;
    double width = this.getWidth();
    double height = this.getHeight();
    int x_num = (int) ((width - baseX) / (int) grid);
    int y_num = (int) (height / (int) grid);

    g2.setColor(new Color(0, 150, 0));
    // draw y lines
    for (int i = 0; i <= x_num; i++) {
      Line2D line = new Line2D.Double(baseX + i * (width / x_num), topY,
          baseX + i * (width / x_num), height);
      g2.draw(line);
    }

    // draw x lines
    for (int i = 0; i <= y_num; i++) {
      Line2D line = new Line2D.Double(leftX, topY + i * (height / y_num),
          width, topY + i * (height / y_num));
      g2.draw(line);
    }

    g2.setColor(new Color(100, 255, 0));
    // draw history curvs
    if (list.size()>= 2) {
      double historybaseX = width - (list.size() - 1) * this.speedFactor;
      Object[] array = list.toArray();
      for (int i = 0; i < array.length-1; i++) {
        int data = ((Integer) array[i]).intValue();
        int nextdata = ((Integer) array[i+1]).intValue();
        Line2D line = new Line2D.Double(historybaseX, (total - data)
            * height / total, historybaseX + this.speedFactor,
            (total - nextdata) * height / total);
        g2.draw(line);
        historybaseX += this.speedFactor;
      }
    }

    baseX = baseX - grid / speedFactor;
    if (baseX == 0)
      baseX = grid;
  }

  private void initGUI() {
    try {
      setPreferredSize(new Dimension(400, 300));
      this.setBackground(new java.awt.Color(0, 0, 0));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void addData(int data){
    int historyNum=(int)(this.getWidth()/this.speedFactor)+1;
    if(list.size()==historyNum)
      list.removeFirst();
    list.add(new Integer(data));
  }
  /**
   * @param baseX
   * the baseX to set
   */
  public void setBaseX(double baseX) {
    this.baseX = baseX;
  }

  /**
   * @param grid
   * the grid to set
   */
  public void setGrid(double grid) {
    this.grid = grid;
  }

  /**
   * @return the baseX
   */
  public double getBaseX() {
    return baseX;
  }

  /**
   * @return the grid
   */
  public double getGrid() {
    return grid;
  }

  /**
   * @return the list
   */
  public LinkedList getList() {
    return list;
  }

}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923