2017-07-28 77 views
1

在此代碼中,我創建了2個TimeSeries並將它們添加到同一個圖中,但axis.setAutoRange(true)僅適用於第二個系列。JFreeChart AutoRange不能在同一地塊上的多個系列上工作

有沒有辦法使兩個TimeSeries上的AutoRange工作?

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.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 

import javax.swing.*; 
import java.awt.*; 

public class Graph extends ApplicationFrame { 

    private TimeSeries seriesA; 
    private TimeSeries seriesB; 

    public Graph(final String windowTitle, int width, int height, String xTitle, String yTitle, String headerTitle, String graphTitle) { 
     super(windowTitle); 
     final TimeSeriesCollection dataset = new TimeSeriesCollection(); 
     this.seriesA = new TimeSeries(graphTitle); 
     this.seriesB = new TimeSeries(graphTitle); 
     dataset.addSeries(this.seriesA); 
     dataset.addSeries(this.seriesB); 

     final JFreeChart chart = ChartFactory.createTimeSeriesChart(
       headerTitle,//set title 
       xTitle,//set x title 
       yTitle,//set y title 
       dataset, 
       false, 
       false, 
       false 
     ); 

     final XYPlot plot = chart.getXYPlot(); 
     ValueAxis axis = plot.getDomainAxis(); 
     axis.setFixedAutoRange(60000.0); 

     axis = plot.getRangeAxis(); 
     axis.setAutoRange(true); 

     final ChartPanel chartPanel = new ChartPanel(chart); 
     final JPanel content = new JPanel(new BorderLayout()); 
     content.add(chartPanel); 

     chartPanel.setPreferredSize(new java.awt.Dimension(width, height)); 

     setContentPane(content); 
    } 

    public void addPointA(double y) { 
     this.seriesA.add(new Millisecond(), y); 
    } 

    public void addPointB(double y) { 
     this.seriesB.add(new Millisecond(), y); 
    } 


    public static void main(final String[] args) throws InterruptedException { 
     final Graph demo = new Graph("Demo",500,500,"Time","Value", 
       "Header1","graph1");//window title 
     demo.pack();//doesnt matter 
     RefineryUtilities.positionFrameOnScreen(demo,0.2,0.7);//manually choose window position % 
     demo.setVisible(true);//show window 

     double lastValue=80;//randomize input 
     while (true){ 
      demo.addPointA(lastValue); 
      demo.addPointB(lastValue-100); 
      //randomize input 
      lastValue*=Math.random()*0.2-0.1+1.001; 
      lastValue+=Math.random()*2-1; 

      //limit input rate 
      Thread.sleep(100); 
     } 
    } 
} 

在該圖片中的axis.setAutoRange(true)作品只對紅色圖形(seriesB) enter image description here

+0

「通常,設置在軸範圍手動,呼叫者期望範圍內仍然有效。」你需要第二個軸嗎?請修改您的問題以包含[mcve],其中包含代表性數據並展示您描述的問題。 – trashgod

+0

對不起,但我看不出你的評論與我的問題有什麼關係。就像上圖中的圖片一樣,'setAutoRange'在紅色圖表上工作。我想知道它將如何在兩個圖表上工作(以便整個範圍都可見)。 –

+0

自動範圍在'org.jfree.chart.demo.TimeSeriesChartDemo1'中正常工作。你爲什麼改變它? – trashgod

回答

2

幾個問題值得關注:

  • 每個TimeSeries包括TimeSeriesCollection用作的名稱Comparable索引;這些名稱應該是唯一可靠的自動量程;使圖表工廠的圖例能夠看到效果。

  • 如分配中包含的org.jfree.chart.demo.TimeSeriesChartDemo1所示,自動量程通常不需要特殊設置。

  • 僅在event dispatch thread上構建和操作Swing GUI對象。

  • 請勿事件調度線程上的sleep();使用javax.swing.Timer來調整更新。

  • 請勿不必要地擴展頂級容器。

  • 不要無謂地嵌套容器。

image

import java.awt.EventQueue; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.time.Millisecond; 
import org.jfree.data.time.TimeSeries; 
import org.jfree.data.time.TimeSeriesCollection; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 
import javax.swing.*; 
import java.awt.event.*; 

public class Graph extends ApplicationFrame { 

    private final TimeSeries seriesA = new TimeSeries("A"); 
    private final TimeSeries seriesB = new TimeSeries("B"); 

    public Graph(final String windowTitle, int width, int height, 
     String xTitle, String yTitle, String headerTitle, String graphTitle) { 
     super(windowTitle); 
     final TimeSeriesCollection dataset = new TimeSeriesCollection(); 
     dataset.addSeries(this.seriesA); 
     dataset.addSeries(this.seriesB); 
     final JFreeChart chart = ChartFactory.createTimeSeriesChart(
      headerTitle, xTitle, yTitle, dataset, true, true, false 
     ); 
     ChartPanel chartPanel = new ChartPanel(chart); 
     chartPanel.setPreferredSize(new java.awt.Dimension(width, height)); 
     add(chartPanel); 
    } 

    public void addPointA(double y) { 
     this.seriesA.add(new Millisecond(), y); 
    } 

    public void addPointB(double y) { 
     this.seriesB.add(new Millisecond(), y); 
    } 

    public static void main(final String[] args) { 
     EventQueue.invokeLater(() -> { 
      Graph demo = new Graph("Demo", 640, 480, 
       "Time", "Value", "Header", "Graph"); 
      demo.pack();//matters a great deal 
      RefineryUtilities.centerFrameOnScreen(demo); 
      demo.setVisible(true); 

      new Timer(100, (new ActionListener() { 
       double lastValue = 80; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        demo.addPointA(lastValue); 
        demo.addPointB(lastValue - 100); 
        lastValue *= Math.random() * 0.2 - 0.1 + 1.001; 
        lastValue += Math.random() * 2 - 1; 
       } 
      })).start(); 
     }); 
    } 
} 
+1

非常感謝!當我給每個圖表一個獨特的名字時,它完美的工作! –

相關問題