2012-12-27 333 views
12

我有一個JFree XY折線圖,它總是開始在x = 0。然後根據用戶定義的設置從一個屬性文件基於該號碼應用增量(這代表以分鐘爲單位的時間)JFreeChart的x軸刻度

例如x = 0開始用戶定義的設置爲5 - 因此刻度變爲0,5,10,15,20 ....或者用戶設置爲3,因此變爲0,3,6,9,12 ......

很簡單。

我遇到的問題是圖開始的方式。如果我從0開始,那麼0在圖形的中間而不是在-0.0000005,-0.000004,-0.000003 ..... 0.000000,0.000001,0.000002 ...... 0.000005

我怎樣才能在底部手動添加比例尺,即定義它應該是2的增量,然後它保持它?

感謝您的時間

回答

13

您應該使用NumberAxis,裏面包含了很多的方法來定義圖表的規模。

實施例:

// Create an XY Line chart 
XYSeries series = new XYSeries("Random Data"); 
series.add(1.0, 500.2); 
series.add(10.0, 694.1); 
XYSeriesCollection data = new XYSeriesCollection(series); 
JFreeChart chart = ChartFactory.createXYLineChart("XY Series Demo", "X", "Y", data, 
                PlotOrientation.VERTICAL, 
                true, true, false); 

// Create an NumberAxis 
NumberAxis xAxis = new NumberAxis(); 
xAxis.setTickUnit(new NumberTickUnit(2)); 

// Assign it to the chart 
XYPlot plot = (XYPlot) chart.getPlot(); 
plot.setDomainAxis(xAxis); 
+1

1爲'setTickUnit()'。 – trashgod

3

在此基礎上example,這裏是一個使用setTickUnit()動態調整域軸的刻度部,從值5起動sscce

image

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.util.Random; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSpinner; 
import javax.swing.SpinnerNumberModel; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.axis.NumberTickUnit; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 

/** @see https://stackoverflow.com/a/14167983/230513 */ 
public class SSCCE { 

    private static final int COUNT = 100; 
    private static final int UNITS = 5; 
    private static final Random r = new Random(); 

    public static void main(String[] args) { 
     XYSeries series = new XYSeries("Data"); 
     for (int i = 0; i < COUNT; i++) { 
      series.add(i, r.nextGaussian()); 
     } 
     XYSeriesCollection data = new XYSeriesCollection(series); 
     final JFreeChart chart = ChartFactory.createXYLineChart("TickUnits", 
      "X", "Y", data, PlotOrientation.VERTICAL, true, true, false); 
     XYPlot plot = (XYPlot) chart.getPlot(); 
     final NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); 
     xAxis.setTickUnit(new NumberTickUnit(UNITS)); 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame f = new JFrame("TickUnitDemo"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.add(new ChartPanel(chart)); 
       final JSpinner spinner = new JSpinner(
        new SpinnerNumberModel(UNITS, 1, COUNT, 1)); 
       spinner.addChangeListener(new ChangeListener() { 

        @Override 
        public void stateChanged(ChangeEvent e) { 
         JSpinner s = (JSpinner) e.getSource(); 
         Number n = (Number) s.getValue(); 
         xAxis.setTickUnit(new NumberTickUnit(n.intValue())); 
        } 
       }); 
       JPanel p = new JPanel(); 
       p.add(new JLabel(chart.getTitle().getText())); 
       p.add(spinner); 
       f.add(p, BorderLayout.SOUTH); 
       f.pack(); 
       f.setLocationRelativeTo(null); 
       f.setVisible(true); 
      } 
     }); 

    } 
} 
+0

如果這不完全是你所追求的,它可能有助於澄清你的問題。 – trashgod