我正在使用从 http://wpf.codeplex.com/releases/view/40535 下载的 WPF 图表控件
我正在尝试绘制条形图,这是我的示例代码
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 上看到该图中的动态变化
这可能吗?
如果可能,请回答这个问题。
提前致谢。
最佳答案
您可以使用 ObservableCollection对此。每当您的集合被修改时,引发一个事件 CollectionChanged
并且您的图表将重新绑定(bind)。检查this来自 MSDN 或 this 的示例.
希望这对你有用。
关于c# - 如何生成随机值以在 wpf 中绘制条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986349/