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]),int>(3,str[3]),int>(4,str[4]),int>(5,str[0])
});
dispatcher.BeginInvoke(() =>
lineseries4.ItemsSource = new keyvaluePair<int,str2[1]),str2[2]),str2[3]),str2[4]),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,200);
}
ThreadPool.QueueUserWorkItem((threadState) =>
{
dispatcher.BeginInvoke(() =>
chart.Series.Clear());
dispatcher.BeginInvoke(() =>
lineseries.ItemsSource = new keyvaluePair<int,str[0])
});
dispatcher.BeginInvoke(() =>
lineseries2.ItemsSource = new keyvaluePair<int,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,9),8),6),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,1),2),4),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,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,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" Name="canvas1" VerticalAlignment="Top" Width="233"></Canvas> <Button Content="2" Height="23" Name="button2" Width="75" Margin="303,313,0" HorizontalAlignment="Left" Click="button2_Click" /> <Canvas Name="canvas2" Margin="266,32,306,286"></Canvas> </Grid> </UserControl>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。