title: Dynamic JFreeChart for custom X-Axis values
body: <p>I make a <code>JFreeChart</code> for displaying values from serial port. It is working well, but I want to make X-axis movable from right to left. It means when plotting the graph, custom values <em>or</em> seconds move from right to left; now it work as 0 of x-axis remains same place, but I want to move from left right to left continuously. Also I tried <code>createTimeSeriesChart</code>, but it is start from 0 then 59 again start to 0, but I want continuous running of seconds 0 to onwards.</p>
  <pre><code>`import java.awt.BorderLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.Scanner;
  
  import javax.swing.JButton;
  import javax.swing.JComboBox;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  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.axis.ValueAxis;
  import org.jfree.chart.plot.XYPlot;
  import org.jfree.data.general.Series;
  import org.jfree.data.xy.XYSeries;
  import org.jfree.data.xy.XYSeriesCollection;
  
  //import org.jfree.chart.ChartFactory;
  //import org.jfree.chart.ChartPanel;
  //import org.jfree.chart.JFreeChart;
  //import org.jfree.data.xy.XYSeries;
  //import org.jfree.data.xy.XYSeriesCollection;
  
  import com.fazecast.jSerialComm.SerialPort;
  
  
  public class AccelerometerGraph {
      
      static SerialPort chosenPort;
      static int x = 0;
  
  public static void main(String[] args) {
          
          // create and configure the window
          JFrame window = new JFrame();
          window.setTitle(&quot;Vibration Analyzer Graph GUI&quot;);
          window.setSize(600, 400);
          window.setLayout(new BorderLayout());
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          // create a drop-down box and connect button, then place them at the top of the window
          JComboBox&lt;String&gt; portList = new JComboBox&lt;String&gt;();
          JButton connectButton = new JButton(&quot;Connect&quot;);
          JTextField textField = new JTextField(10);
          JLabel label = new JLabel();
          JPanel topPanel = new JPanel();
          JPanel bottomPanel = new JPanel();
          label.setText(&quot;Accel Values&quot;);
          textField.setFocusable(false);
          topPanel.add(portList);
          topPanel.add(connectButton);
          bottomPanel.add(label);
          bottomPanel.add(textField); 
          window.add(topPanel, BorderLayout.NORTH);
          window.add(bottomPanel, BorderLayout.SOUTH);
          
          // populate the drop-down box
          SerialPort[] portNames = SerialPort.getCommPorts();
          for(int i = 0; i &lt; portNames.length; i++)
              portList.addItem(portNames[i].getSystemPortName());
          
          
          // create the line graph
          XYSeries series = new XYSeries(&quot;&quot;);
          XYSeriesCollection dataset = new XYSeriesCollection(series);
          JFreeChart chart = ChartFactory.createXYLineChart(&quot;Accelerometer Readings&quot;, &quot;Time (seconds)&quot;, &quot;Intensity cm/s&quot;, dataset);
          window.add(new ChartPanel(chart), BorderLayout.CENTER);
          
          // configure the connect button and use another thread to listen for data
          connectButton.addActionListener(new ActionListener(){
              @Override public void actionPerformed(ActionEvent arg0) {
                  if(connectButton.getText().equals(&quot;Connect&quot;)) {
                      // attempt to connect to the serial port
                      chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
                      chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                      if(chosenPort.openPort()) {
                          connectButton.setText(&quot;Disconnect&quot;);
                          portList.setEnabled(false);
                      }
                      
                      
                      // create a new thread that listens for incoming text and populates the graph
                      Thread thread = new Thread(){
                          @Override public void run() {
                              Scanner scanner = new Scanner(chosenPort.getInputStream());
                          
                              while(scanner.hasNextLine()) {
                                  try {
                                      String line = scanner.nextLine();
                                      int number = Integer.parseInt(line);                                                                
                                      series.add(x++, 1023 - number);                                         
                                      textField.setText(String.valueOf(line));                                    
                                      window.repaint();
                                  } catch(Exception e) {}
                              }
                              scanner.close();
                          }
                      };
                      thread.start();
                  } else {
                      // disconnect from the serial port
                      chosenPort.closePort();
                      portList.setEnabled(true);
                      connectButton.setText(&quot;Connect&quot;);
                      textField.setText(null);
                      series.clear();
                      x = 0;
                  }
              }
          });
          
          // show the window
          window.setVisible(true);
      }
  
  }
  </code></pre>
  <p>`</p>