在swing中插入图表

在swing中插入图表


参考代码: 
Java code
package swing;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.util.*;

import javax.swing.*;
import javax.swing.Timer;

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.*;

public class DynaChartPanel extends JPanel implements ActionListener {
    
    private Vector<Integer> v1 = new Vector<Integer>(); 
    private Vector<Integer> v2 = new Vector<Integer>(); 
    private Vector<Integer> times = new Vector<Integer>();
    private String[] rowKeys = { "Team1", "Team2" };
    private Timer timer;
    private int second = 0;
    private int s1 = 0, s2 = 0;
    private Random rnd = new Random();
    private int width = 90;
    private final int height = 610;
    
    public DynaChartPanel()
    {
        times.add(second);
        v1.add(s1);
        v2.add(s2);
        //
        setPreferredSize(new Dimension(width, height));
    }
    
    public void start()
    {
        timer = new Timer(3000, this);    //测试代码,3秒一次,可改为30000(秒)
        timer.start();
    }
    
    public void stop()
    {
        if(timer!=null&&timer.isRunning())
            timer.stop();
    }
    
    public CategoryDataset getCategoryDataset()
    {
        Integer[] t = new Integer[0];
        Number[][] data = new Integer[][]{
            v1.toArray(t),
            v2.toArray(t)
        };
        DefaultCategoryDataset result = new DefaultCategoryDataset();
        for (int r = 0; r < data.length; r++) {
            for (int c = 0; c < data[r].length; c++) {
                result.addValue(data[r][c], rowKeys[r], times.get(c));
            }
        }
        return result;
    }
    
    public void paintComponent(Graphics g)
    {
        Dimension size = new Dimension( width, height);
        setPreferredSize(size);
        //
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        //
        CategoryDataset dataset = getCategoryDataset();
        //以下10来行代码亦可由ChartFactory.createLineChart()完成
        CategoryAxis categoryAxis = new CategoryAxis("时间(秒)");
        ValueAxis valueAxis = new NumberAxis("比分");
        LineAndShapeRenderer renderer = new LineAndShapeRenderer(true, false);
            renderer.setBaseToolTipGenerator(
                    new StandardCategoryToolTipGenerator());
        CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, 
                renderer);
        plot.setOrientation(PlotOrientation.VERTICAL);
        
        JFreeChart chart = new JFreeChart("折线图", JFreeChart.DEFAULT_TITLE_FONT,
                plot, true);
        //
        Rectangle2D area1 = new Rectangle(0, 0, width, 300);
        chart.draw(g2, area1);
        //
        chart = ChartFactory.createBarChart("直方图", "时间(秒)", "比分", dataset, PlotOrientation.VERTICAL, true, true, false);
        Rectangle2D area2 = new Rectangle(0, 300, width, 300);
        chart.draw(g2, area2);
    }

    public void actionPerformed(ActionEvent e) {
        String message = "输入格式为“v1,v2”,比如“11,12”:";
        while(true)
        {
            //每次加1到3,方便演示之用
            s1 += rnd.nextInt(3) + 1; s2+= rnd.nextInt(3) + 1;
            //使用注释部分代码可以达到手动输入两队比分的目的
            String input = s1 + "," + s2;//JOptionPane.showInputDialog( message, s1 + "," + s2);
            
            try
            {
                String[] score = input.split(",");
                s1 = Integer.parseInt(score[0]);
                s2 = Integer.parseInt(score[1]);
                second += 30;
                v1.add(s1);
                v2.add(s2);
                times.add(second);
                //
                width += 30;
                repaint();
                //
                break;
            }catch(Exception ex)
            {
                message = "输入无效!请重输!\n输入格式为“v1,v2”,比如“11,12”:";
            }
        }
    }
    
    public static void main(String[] args) {
        JFrame frm = new JFrame();
        frm.setTitle("动态的折线图与直方图");
        frm.setSize(600, 650);
        DynaChartPanel pnlMain = new DynaChartPanel();        
        frm.getContentPane().add(new JScrollPane(pnlMain));
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
        pnlMain.start();        
    }
}


运行效果: 

你可能感兴趣的:(C++,c,swing,jfreechart,C#)