使用wpf工具箱动态创建图表

本文关键字:创建 动态 wpf 工具箱 使用 | 更新日期: 2023-09-27 17:50:39

我想用wpf工具包动态创建一个图表值,而不使用xml绑定依赖和独立的值;这段代码不能运行

            Chart myChart = new Chart();
            BarSeries colser = new BarSeries();
            myChart.Width = 250;
            myChart.Height = 250;
            myChart.Title = "chart";
            List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
            valueList.Insert(0, new KeyValuePair<string, int>("TOR", 10));
            valueList.Insert(1, new KeyValuePair<string, int>("SOC", 15));
            valueList.Insert(2, new KeyValuePair<string, int>("BOS", 18));
            valueList.Insert(3, new KeyValuePair<string, int>("NON", 11));
            colser.Title = "title";
            colser.IndependentValuePath = "Value";
            colser.DependentValuePath = "Key";
            colser.ItemsSource = valueList;
            myChart.Series.Add(colser);
            // added myChart to stackpanel as child

使用wpf工具箱动态创建图表

我找到解决问题的方法了

var barSeries = new BarSeries
              {
                            Title = "Fruit in Cupboard",
                            ItemsSource = new[]
                                              {
                                                  new KeyValuePair<string, int>("TOR", 10),
                                                  new KeyValuePair<string, int>("SOC", 15),
                                                  new KeyValuePair<string, int>("BOS", 18),
                                                  new KeyValuePair<string, int>("NON", 11)
                                              },
                            IndependentValueBinding = new Binding("Key"),
                            DependentValueBinding = new Binding("Value")
                        };
    myChart.Series.Add(barSeries);