title: WPF Charting Toolkit: How to set a fixed X-axis range while adding real time data
body: <p>I'm doing an application real time data charting basing on WPF Charting Toolkit. I get the datas via serial port.
  The code of setting chart is below:</p>
  
  <pre><code>    &lt;chartingToolkit:Chart  Margin="10,10,10,0" ClipToBounds="True" x:Name="chart1" Title="Chart Title"&gt;
          &lt;chartingToolkit:LineSeries IndependentValueBinding="{Binding Value1}" DependentValueBinding="{Binding Value2}" ItemsSource="{Binding}" Background="Transparent" Cursor="No"&gt;
              &lt;chartingToolkit:LineSeries.DataPointStyle&gt;
                  &lt;Style TargetType="{x:Type chartingToolkit:LineDataPoint}"&gt;
                      &lt;Setter Property="Height" Value="0"/&gt;
                      &lt;Setter Property="Width" Value="0" /&gt;
                      &lt;Setter Property="Background" Value="Green"/&gt;
                  &lt;/Style&gt;
              &lt;/chartingToolkit:LineSeries.DataPointStyle&gt;
          &lt;/chartingToolkit:LineSeries&gt;
      &lt;/chartingToolkit:Chart&gt;
  </code></pre>
  
  <p>It works good but I still need to set maximum and minimum values of X axis.
  The X Values <strong>(Value1)</strong> are as number of received samples and the Y axis values <strong>(Value2)</strong> are  obviously as concrete values of the received samples.</p>
  
  <p>My question is about the X axis range.</p>
  
  <p>Currently, I'm getting minimum as 0 and maximinum as the highest number of sample which the serial port received in current moment.</p>
  
  <p>But I want to set a permanent range of X axis which I want to see.</p>
  
  <p>For example I want to see on the X axis range of 500 samples.</p>
  
  <p>It means that when the number of samples exceeds 500, the max should be as the highest sample number and the min should be <strong>max-500</strong>.</p>
  
  <p>The main difficulty is how to set it with real time data in WPF??</p>
  
  <p>Can anyone help me, please??</p>
  
  <p><strong><em>Updated question</em></strong></p>
  
  <p>I'm updating my question after @jstreet advise.</p>
  
  <p>I have this method which is running in separate thread within MainWindow class, likes below.</p>
  
  <pre><code> public partial class MainWindow : Window 
    {
   public SerialPort serialPort1 = new SerialPort();
      public string rx_str = "";
      public string rx_str_copy;
      public int a;
      public double x, y;
  
  
        ObservableCollection&lt;ChartData&gt; chartData;
      ChartData objChartData;
      Thread myThread;
  
       public MainWindow()
      {
          InitializeComponent();
          string[] port = SerialPort.GetPortNames();
          foreach (string a in port)
          {
              comboPorts.Items.Add(a);
          }
          Array.Sort(port);
          comboPorts.Text = port[0];
  
          objChartData = new ChartData();
          chartData.Add(objChartData);
          chart1.DataContext = chartData;
          myThread = new Thread(new ThreadStart(Run));
  
  
      }
  
     public void Run()
         {
          while (true)
          {
              serialPort1.Write("a");
              rx_str = serialPort1.ReadTo("b");
              rx_str_copy = rx_str;
              x = a;
              y = Double.Parse(rx_str_copy,     CultureInfo.InvariantCulture);                                               
  
              a++;
  
              Dispatcher.Invoke(new Action(delegate
              {
  
           chartData.Add(new ChartData() { Value1 = x,         
         Value2= y             });          
              }));
  
  
  
          }
      }  
  </code></pre>
  
  <p>This Run() method is responsible for receiving datas and adding it to the chart.</p>
  
  <p>In another class I have handle of reaction on comming datas and settings  properties Valeu1 and Value2:</p>
  
  <pre><code>  public class ChartData : INotifyPropertyChanged
     {
      double _Value1;
      double _Value2;
  
  
      public double Value1
      {
          get
          {
              return _Value1;
          }
          set
          {
              _Value1 = value;
              OnPropertyChanged("Value1");
          }
      }
  
      public double Value2
      {
          get
          {
              return _Value2;
          }
          set
          {
              _Value2 = value;
              OnPropertyChanged("Value2");
          }
      }
  
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new   
       PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>How can I adapt @jstreet's solution to my behind code example??</p>