栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中使用jfree图表制作动态折线图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中使用jfree图表制作动态折线图

是的,您可以做到。几天前我也遇到了类似的问题。

DynamicLineAndTimeSeriesChart.java

import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Timer;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.XYPlot;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.ui.Applicationframe;import org.jfree.ui.RefineryUtilities;public class DynamicLineAndTimeSeriesChart extends Applicationframe implements ActionListener {        private TimeSeries series;        private double lastValue = 100.0;        private Timer timer = new Timer(250, this);        public DynamicLineAndTimeSeriesChart(final String title) {        super(title);        this.series = new TimeSeries("Random Data", Millisecond.class);        final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);        final JFreeChart chart = createChart(dataset);        timer.setInitialDelay(1000);        //Sets background color of chart        chart.setBackgroundPaint(Color.LIGHT_GRAY);        //Created JPanel to show graph on screen        final JPanel content = new JPanel(new BorderLayout());        //Created Chartpanel for chart area        final ChartPanel chartPanel = new ChartPanel(chart);        //Added chartpanel to main panel        content.add(chartPanel);        //Sets the size of whole window (JPanel)        chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));        //Puts the whole content on a frame        setContentPane(content);        timer.start();    }        private JFreeChart createChart(final XYDataset dataset) {        final JFreeChart result = ChartFactory.createTimeSeriesChart( "Dynamic Line And TimeSeries Chart", "Time", "Value", dataset, true, true, false        );        final XYPlot plot = result.getXYPlot();        plot.setBackgroundPaint(new Color(0xffffe0));        plot.setDomainGridlinesVisible(true);        plot.setDomainGridlinePaint(Color.lightGray);        plot.setRangeGridlinesVisible(true);        plot.setRangeGridlinePaint(Color.lightGray);        ValueAxis xaxis = plot.getDomainAxis();        xaxis.setAutoRange(true);        //Domain axis would show data of 60 seconds for a time        xaxis.setFixedAutoRange(60000.0);  // 60 seconds        xaxis.setVerticalTickLabels(true);        ValueAxis yaxis = plot.getRangeAxis();        yaxis.setRange(0.0, 300.0);        return result;    }        public void actionPerformed(final ActionEvent e) {        final double factor = 0.9 + 0.2*Math.random();        this.lastValue = this.lastValue * factor;        final Millisecond now = new Millisecond();        this.series.add(new Millisecond(), this.lastValue);        System.out.println("Current Time in Milliseconds = " + now.toString()+", Current Value : "+this.lastValue);    }        public static void main(final String[] args) {        final DynamicLineAndTimeSeriesChart demo = new DynamicLineAndTimeSeriesChart("Dynamic Line And TimeSeries Chart");        demo.pack();        RefineryUtilities.centerframeonScreen(demo);        demo.setVisible(true);    }}

也请在这里检查:

http://blog.odoobiz.com/2012/07/how-to-draw-dynamic-line-or-
timeseries.html



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号