silverlight 动态chart 动态折线图

来源:互联网 发布:淘宝生e经怎么找不到 编辑:程序博客网 时间:2024/04/28 16:48

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Data.DataForm;
using System.Windows.Controls.DataVisualization.Charting;
using System.ComponentModel;
using System.Threading;
namespace altext
{
    public partial class MainPage : UserControl
    {

        private BackgroundWorker bw = new BackgroundWorker();
        private BackgroundWorker bw2 = new BackgroundWorker();
        public MainPage()
        {
          
            InitializeComponent();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;//
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw2.WorkerReportsProgress = true;
            bw2.WorkerSupportsCancellation = true;//
            bw2.DoWork += new DoWorkEventHandler(bw_DoWork2);
        }
   
        private void bw_DoWork2(object sender, DoWorkEventArgs e)
        {

            while (true)
            {

                int[] str = new int[5];
                Random rd = new Random();
                for (int i = 0; i < 5; i++)
                {
                    str[i] = rd.Next(0, 100);
                }
                int[] str2 = new int[5];
                Random rd2 = new Random();
                for (int i = 0; i < 5; i++)
                {
                    str2[i] = rd2.Next(100, 200);
                }

                ThreadPool.QueueUserWorkItem((threadState) =>
                {

                    Dispatcher.BeginInvoke(() =>
                   chart2.Series.Clear());

                    Dispatcher.BeginInvoke(() =>
                       lineSeries3.ItemsSource = new KeyValuePair<int, int>[] {
                  
                   new KeyValuePair<int, int>(1, str[1]),

                   new KeyValuePair<int , int>(2, str[2]),

                   new KeyValuePair<int , int>(3, str[3]),

                   new KeyValuePair<int , int>(4, str[4]),

                   new KeyValuePair<int , int>(5, str[0])
             

                    });
                    Dispatcher.BeginInvoke(() =>
                  lineSeries4.ItemsSource = new KeyValuePair<int, int>[] {
                  
                   new KeyValuePair<int, int>(1, str2[1]),

                   new KeyValuePair<int , int>(2, str2[2]),

                   new KeyValuePair<int , int>(3, str2[3]),

                   new KeyValuePair<int , int>(4, str2[4]),

                   new KeyValuePair<int , int>(5, str2[0])
             

                    });
                    Dispatcher.BeginInvoke(() =>
                   chart2.Series.Add(lineSeries3));
                    Dispatcher.BeginInvoke(() =>
                     chart2.Series.Add(lineSeries4));
              

 

 

                });

                Thread.Sleep(1500);
            }
        }
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
           
            while (true)
            {
                int[] str = new int[5];
                Random rd = new Random();
                for (int i = 0; i < 5; i++)
                {
                    str[i] = rd.Next(0, 100);
                }
                int[] str2 = new int[5];
                Random rd2 = new Random();
                for (int i = 0; i < 5; i++)
                {
                    str2[i] = rd2.Next(100, 200);
                }
              
                ThreadPool.QueueUserWorkItem((threadState) =>
                 {
               
                    Dispatcher.BeginInvoke(() =>
                   chart.Series.Clear());

                    Dispatcher.BeginInvoke(() =>
                       lineSeries.ItemsSource = new KeyValuePair<int, int>[] {
                  
                   new KeyValuePair<int, int>(1, str[1]),

                   new KeyValuePair<int , int>(2, str[2]),

                   new KeyValuePair<int , int>(3, str[3]),

                   new KeyValuePair<int , int>(4, str[4]),

                   new KeyValuePair<int , int>(5, str[0])
             

                    });
                    Dispatcher.BeginInvoke(() =>
                  lineSeries2.ItemsSource = new KeyValuePair<int, int>[] {
                  
                   new KeyValuePair<int, int>(1, str2[1]),

                   new KeyValuePair<int , int>(2, str2[2]),

                   new KeyValuePair<int , int>(3, str2[3]),

                   new KeyValuePair<int , int>(4, str2[4]),

                   new KeyValuePair<int , int>(5, str2[0])
             

                    });    Dispatcher.BeginInvoke(() =>
                chart.Series.Add(lineSeries));
                    Dispatcher.BeginInvoke(() =>
          chart.Series.Add(lineSeries2));

               
          

              
                 });
           
                Thread.Sleep(1500);
            }
        }
        Chart chart;
        Chart chart2;
        LineSeries lineSeries;
        LineSeries lineSeries2;
        LineSeries lineSeries3;
        LineSeries lineSeries4;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            chart = new Chart();
            chart.Width = 250;
            chart.Height = 240;

            // 设置图标的属性


            canvas1.Children.Add(chart);
            lineSeries = new LineSeries();

 

 


            System.Windows.Data.Binding keyBinding = new System.Windows.Data.Binding();
            keyBinding.Path = new PropertyPath("Key");
            lineSeries.IndependentValueBinding = keyBinding;

            System.Windows.Data.Binding valueBinding = new System.Windows.Data.Binding();
            valueBinding.Path = new PropertyPath("Value");
            lineSeries.DependentValueBinding = valueBinding;

            lineSeries.ItemsSource = new KeyValuePair<int, int>[] {

                   new KeyValuePair<int, int>(1, 9),

                   new KeyValuePair<int, int>(2, 8),

                   new KeyValuePair<int, int>(3, 6),

                   new KeyValuePair<int,int>(4, 9),

                   new KeyValuePair<int, int>(5, 8)

                    };

            lineSeries2 = new LineSeries();
            lineSeries2.Title = "1346";

            System.Windows.Data.Binding keyBinding1 = new System.Windows.Data.Binding();
            keyBinding1.Path = new PropertyPath("Key");
            lineSeries2.IndependentValueBinding = keyBinding1;

            System.Windows.Data.Binding valueBinding1 = new System.Windows.Data.Binding();
            valueBinding1.Path = new PropertyPath("Value");
            lineSeries2.DependentValueBinding = valueBinding1;

            lineSeries2.ItemsSource = new KeyValuePair<int, int>[] {

                   new KeyValuePair<int, int>(1, 1),

                   new KeyValuePair<int, int>(2,2),

                   new KeyValuePair<int, int>(3, 4),

                   new KeyValuePair<int,int>(4, 9),

                   new KeyValuePair<int, int>(5, 8)

                    };
            chart.Series.Add(lineSeries2);
            chart.Series.Add(lineSeries);
            if (bw.IsBusy != true)
            {
                bw.RunWorkerAsync();
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            chart2 = new Chart();
            chart2.Width = 250;
            chart2.Height = 240;

            // 设置图标的属性


            canvas2.Children.Add(chart2);
            lineSeries3 = new LineSeries();

            System.Windows.Data.Binding keyBinding = new System.Windows.Data.Binding();
            keyBinding.Path = new PropertyPath("Key");
            lineSeries3.IndependentValueBinding = keyBinding;

            System.Windows.Data.Binding valueBinding = new System.Windows.Data.Binding();
            valueBinding.Path = new PropertyPath("Value");
            lineSeries3.DependentValueBinding = valueBinding;

            lineSeries3.ItemsSource = new KeyValuePair<int, int>[] {

                   new KeyValuePair<int, int>(1, 9),

                   new KeyValuePair<int, int>(2, 8),

                   new KeyValuePair<int, int>(3, 6),

                   new KeyValuePair<int,int>(4, 9),

                   new KeyValuePair<int, int>(5, 8)

                    };

            lineSeries4 = new LineSeries();
            lineSeries4.Title = "1346";
            System.Windows.Data.Binding keyBinding1 = new System.Windows.Data.Binding();
            keyBinding1.Path = new PropertyPath("Key");
            lineSeries4.IndependentValueBinding = keyBinding1;

            System.Windows.Data.Binding valueBinding1 = new System.Windows.Data.Binding();
            valueBinding1.Path = new PropertyPath("Value");
            lineSeries4.DependentValueBinding = valueBinding1;

            lineSeries4.ItemsSource = new KeyValuePair<int, int>[] {

                   new KeyValuePair<int, int>(1, 1),

                   new KeyValuePair<int, int>(2,2),

                   new KeyValuePair<int, int>(3, 4),

                   new KeyValuePair<int,int>(4, 9),

                   new KeyValuePair<int, int>(5, 8)

                    };
            chart2.Series.Add(lineSeries3);
            chart2.Series.Add(lineSeries4);
            if (bw2.IsBusy != true)
            {
                bw2.RunWorkerAsync();
            }

        }
     

    }
}
xaml代码:

<UserControl x:Class="altext.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="648" d:DesignWidth="905" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

    <Grid x:Name="LayoutRoot" Background="White" Height="597" Width="855">
        <Button Content="1" Height="23" HorizontalAlignment="Right" Margin="0,498,490,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Canvas Height="297" HorizontalAlignment="Left" Margin="15,14,0,0" Name="canvas1" VerticalAlignment="Top" Width="233"></Canvas>
        <Button Content="2" Height="23" Name="button2" Width="75" Margin="303,313,0,0" HorizontalAlignment="Left" Click="button2_Click" />
        <Canvas Name="canvas2" Margin="266,32,306,286"></Canvas>
    </Grid>
</UserControl>

原创粉丝点击