Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello , basically I want to refresh the x axe with new values coming.

Chart.xaml:

XML
<UserControl             
             x:Class="----"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
             xmlns:tk="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">

<Grid>
  <chartingToolkit:Chart  Width="350" Height="250" Background="#BDBDBD" Name="AreaChart"  Foreground="DarkRed"
      LegendTitle="Ranges" Title="UKC" Margin="10" HorizontalAlignment="Left"  >
    <chartingToolkit:AreaSeries DependentValuePath="Value"  Foreground="Yellow"  IndependentValuePath="Key" ItemsSource="{Binding}"
              IsSelectionEnabled="True">
   </chartingToolkit:AreaSeries>
  </chartingToolkit:Chart>
  </Grid>
<UserControl>


Chart.cs:


C#
 public partial class Graph 
    {
        static Timer timer; // TEST variable to read sentence periodically 
       
        List<KeyValuePair<int, int>> listofvalues = new List<KeyValuePair<int, int>>();
        public Graph()
        {
            InitializeComponent();
            LoadAreaChartData();
            ////////TEMPORALY TESTING
             
            //TEST TO "SEND" values preiodically
            timer = new Timer(2200);//take into account delay           
            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(RefreshChart);
            /////////////////////////////////////////////////////////////////////
           
        }

///LoadInitial values on the chart
        private void LoadAreaChartData()
        {
            
           listofvalues.Add( new KeyValuePair<int, int>(1, 90));
           listofvalues.Add( new KeyValuePair<int, int>(5, 100));
           listofvalues.Add( new KeyValuePair<int, int>(4, 110));
           listofvalues.Add( new KeyValuePair<int, int>(8, 95));
           listofvalues.Add(new KeyValuePair<int, int>(8, 100));
           listofvalues.Add(new KeyValuePair<int, int>(12, 102));
           

            ((AreaSeries)AreaChart.Series[0]).ItemsSource = listofvalues;
        }
///refreshvalues
        private void RefreshChart(object sender, ElapsedEventArgs e){
          this.Dispatcher.Invoke(new Action(delegate()
              {
                  int time = DateTime.Now.Second;
                  listofvalues.RemoveAt(0);
                  listofvalues.Add(new KeyValuePair<int, int>(time, time + 30));
                  ((AreaSeries)AreaChart.Series[0]).ItemsSource = listofvalues;
                  InitializeComponent();
              }));
            
 }

    }
}

What I am missing?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900