【问题标题】:How to generate random values to plot a barchart in wpf?如何生成随机值以在 wpf 中绘制条形图?
【发布时间】:2012-01-24 11:51:53
【问题描述】:

我正在使用从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 上看到该图的动态变化

有可能吗?

如果可以,请回答。

提前致谢。

【问题讨论】:

    标签: c# wpf charts wpf-controls wpftoolkit


    【解决方案1】:

    您可以对此使用ObservableCollection。当您的收藏被修改时,请提出一个事件CollectionChanged,您的图表将重新绑定。检查来自 MSDN 或 thisthis 示例。

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2023-04-02
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多