使用动态数据显示绘制折线图?

时间:2015-02-19 12:44:09

标签: c# wpf dynamic-data-display

我可以使用动态数据显示绘制折线图吗?

在我的应用程序中,我需要绘制实时voltage/time graph.My requirement,我应该只在电压不是0的情况下绘制图形线。

我如何实现这一目标?

我尝试了以下内容:

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);

            }
        }

1 个答案:

答案 0 :(得分:0)

当最后一个数据点的Y大于0时,你需要的是一个发出新的ObservableDataSources(或者你可以停止发出新点的适配器)的类。每当Voltage为0时,您需要让UI停止侦听该数据源。当电压> 0并且你在那里时,你发出一个事件并将另一个Linegraph添加到图表中。