WPF:图表没有绘制图表

时间:2014-09-04 07:53:55

标签: c# wpf xaml charts

我正在开发WPF应用程序中的图表。但它没有显示图表。 Chart Plot区域是空白的,而我可以根据我在代码中执行的valueList.add的数量来查看图例的数量。

XAML

<chartingToolkit:Chart HorizontalAlignment="Stretch"
                       x:Name="MyPie"
                       Margin="10"
                       Title="Total Voting Percentage"
                       VerticalAlignment="Stretch">
    <chartingToolkit:PieSeries ItemsSource="{Binding}"
                               DependentValuePath="Total Participants"
                               IndependentValuePath="Total Voted" />
</chartingToolkit:Chart>    

代码背后

public partial class TotalVoting : Window
{
    int TotalParticipant;
    int TotalVoted;
    public TotalVoting()
    {
        InitializeComponent();
        TotalParticipant = int.Parse(da[0].ToString()); //data reaches here
        TotalVoted = int.Parse(da1[0].ToString()) / 2; //data reaches here
        ObservableCollection<Writer> Team = new ObservableCollection<Writer> 
        { 
             new Writer("Total Participants", TotalParticipant), 
             new Writer("Participants Voted", TotalVoted),
             new Writer("Total Participants", TotalParticipant), 
             new Writer("Participants Voted", TotalVoted)
        };
        MyPie.DataContext = Team;
    }
}
public class Writer
{
    public Writer() { }
    public Writer(string writerName, int numOfTypes)
    {
        Name = writerName;
        Types = numOfTypes;
    }
    public string Name { get; set; }
    public int Types { get; set; }
}

当我运行代码时,我可以看到数字1,2,3的图例数...所以当前代码显示从1到4.但是我的图表控件上没有显示任何图表。任何线索,以便我可以完成它。


编辑:已对代码进行了更改..但仍然是相同的结果

public void GenerateChart()
{
  List<KeyValuePair<string, double>> valueList = new List<KeyValuePair<string, double>>();
  valueList.Add(new KeyValuePair<string, double>("Apple", 101));
  valueList.Add(new KeyValuePair<string, double>("Banana", 201));
  valueList.Add(new KeyValuePair<string, double>("Cake", 20));
  valueList.Add(new KeyValuePair<string, double>("Others", 200));

  pieChart.Title = "Top Products";
  pieChart.DataContext = valueList;
}

添加时间valueList.Add。我可以在图表上使用相同数量的图例但没有图表:(

希望有人可以帮助我!!!

1 个答案:

答案 0 :(得分:2)

以下是完整图表源代码的链接。 https://www.dropbox.com/s/helas5e8qdr1xhs/WPF.zip?dl=0