如何生成随机值以在wpf中绘制条形图?
我正在使用从 http://wpf.codeplex.com/releases/view/ 下载的 WPF 图表控件40535
这里我试图绘制一个条形图,这是我的示例代码
public partial class Window1 : Window
{
List<Institute> list = new List<Institute> {
new Institute { Subject = "Computers", students = 122 },
new Institute { Subject = "Physics", students = 170 },
new Institute { Subject = "Maths", students = 210 },
new Institute { Subject = "Chemistry", students = 1840 },
new Institute { Subject = "Electronics", students = 140 },
new Institute { Subject = "Economics", students = 20 },
new Institute { Subject = "Science", students = 100 },
new Institute { Subject = "Scocial", students = 110 },
new Institute { Subject = "English", students = 120 },
new Institute { Subject = "Biology", students = 130 },
new Institute { Subject = "Zoology", students = 140 },
new Institute { Subject = "Hindi", students = 150 }};
public Window1()
{
InitializeComponent();
ColumnSeries bs = mcChart.Series[0] as ColumnSeries;
bs.ItemsSource = list;
}
}
public class Institute
{
public string Subject
{
get;
set;
}
public int students
{
get;
set;
}
}
XAML 代码是
<Window x:Class="net.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="Window1" Height="800" Width="800" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid>
<DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"
Width="800" Height="450" FontSize="12"
Background="DarkGray" Foreground="DarkRed">
<DVC:Chart.Series>
<DVC:ColumnSeries x:Name="Barchart" Title="Students of an institute"
ItemsSource="{Binding}"
IndependentValueBinding="{Binding Path=Subject}"
DependentValueBinding="{Binding Path=students}" >
<DVC:ColumnSeries.DataPointStyle>
<Style TargetType="DVC:ColumnDataPoint">
<Setter Property="Background" Value="#001100"/>
</Style>
</DVC:ColumnSeries.DataPointStyle>
</DVC:ColumnSeries>
</DVC:Chart.Series>
</DVC:Chart>
</Grid>
使用此代码我可以绘制图表,但我需要动态绘制它。
当我运行此代码时,我需要连续为每个科目生成随机数量的学生(使用随机),并且应根据新值绘制图表。 这意味着我想在 GUI 上看到该图表的动态变化,
这可能吗?
如果可以的话请回答这个问题。
提前致谢。
i'm using WPF chart controls downloaded from http://wpf.codeplex.com/releases/view/40535
Here i'm trying to plot a barchart and this is my sample code
public partial class Window1 : Window
{
List<Institute> list = new List<Institute> {
new Institute { Subject = "Computers", students = 122 },
new Institute { Subject = "Physics", students = 170 },
new Institute { Subject = "Maths", students = 210 },
new Institute { Subject = "Chemistry", students = 1840 },
new Institute { Subject = "Electronics", students = 140 },
new Institute { Subject = "Economics", students = 20 },
new Institute { Subject = "Science", students = 100 },
new Institute { Subject = "Scocial", students = 110 },
new Institute { Subject = "English", students = 120 },
new Institute { Subject = "Biology", students = 130 },
new Institute { Subject = "Zoology", students = 140 },
new Institute { Subject = "Hindi", students = 150 }};
public Window1()
{
InitializeComponent();
ColumnSeries bs = mcChart.Series[0] as ColumnSeries;
bs.ItemsSource = list;
}
}
public class Institute
{
public string Subject
{
get;
set;
}
public int students
{
get;
set;
}
}
XAML code is
<Window x:Class="net.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="Window1" Height="800" Width="800" xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid>
<DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"
Width="800" Height="450" FontSize="12"
Background="DarkGray" Foreground="DarkRed">
<DVC:Chart.Series>
<DVC:ColumnSeries x:Name="Barchart" Title="Students of an institute"
ItemsSource="{Binding}"
IndependentValueBinding="{Binding Path=Subject}"
DependentValueBinding="{Binding Path=students}" >
<DVC:ColumnSeries.DataPointStyle>
<Style TargetType="DVC:ColumnDataPoint">
<Setter Property="Background" Value="#001100"/>
</Style>
</DVC:ColumnSeries.DataPointStyle>
</DVC:ColumnSeries>
</DVC:Chart.Series>
</DVC:Chart>
</Grid>
With this code i can plot the chart but i need to plot it dynamically.
When ever i'm running this code i need generate random number of students(using Random) for each and every subject continuously and graph should be plotted based on the new values.
That means i want to see the dynamic changes in that graph on the GUI
Is it possible?
If it is possible please answer this.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ObservableCollection 来实现此目的。当您的集合被修改时,引发一个事件
CollectionChanged
,您的图表将重新绑定。从 MSDN 或 这个。希望这对你有用。
You can use ObservableCollection to this. When ever your collection is modified, raise an event
CollectionChanged
and your chart will get rebind. Check this example from MSDN or this.Hope this works for you.