Search code examples
c#wpfdynamic-data-display

draw Broken line graph using dynamic data display?


Can i draw Broken line graph using dynamic data display ?

In my application i need to draw a real time voltage/time graph.My requirement is, i should draw graph lines only if the voltage is not a 0 .

How can i achieve that?

I tried the following :

public partial class MainWindow : Window
    {
        public ObservableDataSource<Point> source1 = null;
        public ObservableDataSource<Point> source2 = null;
        public ObservableDataSource<Point> source3 = null;
        public ObservableDataSource<Point> source4 = null;
        public ObservableDataSource<Point> source5 = null;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {        
            source1 = new ObservableDataSource<Point>();
            source2 = new ObservableDataSource<Point>();
            source3 = new ObservableDataSource<Point>();
            source4 = new ObservableDataSource<Point>();
            source5 = new ObservableDataSource<Point>();
            source1.SetXYMapping(p => p);

            testchart.AddLineGraph(source1, 1, "Data row");
            testchart.AddLineGraph(source2, Color.FromRgb(7, 10, 67), 1, "Data row");
            testchart.AddLineGraph(source3, Color.FromRgb(6, 5, 5), 1, "Data row");
            testchart.AddLineGraph(source4, Color.FromRgb(10, 6, 7), 1, "Data row");
            testchart.AddLineGraph(source5, Color.FromRgb(20, 8, 9), 1, "Data row");
            testchart.Viewport.FitToView();
            Thread simThread = new Thread(new ThreadStart(Simulation));
            simThread.IsBackground = true;
            simThread.Start();
        }

        private void Simulation()
        {
            int i = 0;
            while (true)
            {
                Point p1 = new Point(i * 10, i*10);
                if (i % 2 == 0)//here i tried to break first line in the graph
                {
                    source1.AppendAsync(Dispatcher, p1);
                }
                else
                {
                    Point p5 = new Point(Math.Sign(i*10),i*10);
                    source5.AppendAsync(Dispatcher, p5);
                }

                Point p2 = new Point(i *50, i*10);
                source2.AppendAsync(Dispatcher, p2);
                Point p3 = new Point(i * 1000, i);
                source3.AppendAsync(Dispatcher, p3);
                Point p4 = new Point(i * 1500, i);
                source4.AppendAsync(Dispatcher, p4);
                i++;
                Thread.Sleep(1000);

            }
        }

Solution

  • What you need is a class emmiting new ObservableDataSources (or adapters that you can stop emmiting new points) whenever the last datapoint has Y of >0. Whenever the Voltage is 0 you need to make the UI stop listening to that datasource. You emmit an event and Add another Linegraph to the chart when the Voltage is >0 and you are there..