如何为Live Chart Pies添加值?

时间:2017-11-20 09:03:16

标签: c# wpf charts livecharts

private void MapPie()
{
    List<string> allValues = new List<string>();
    var NameLables = new List<string>();

    CON.ConnectionString = ConfigurationManager.ConnectionStrings["conDB"].ConnectionString;
    CON.Open();
    CMD = new SqlCommand("select top (5) * from tblRecordDefects order by qty desc", CON);
    RDR = CMD.ExecuteReader();
    while (RDR.Read())
    {
        allValues.Add(Convert.ToString(RDR["qty"]));
        var day = RDR.GetString(RDR.GetOrdinal("dname"));
        NameLables.Add(day);
    }

    Func<ChartPoint, string> labelPoint = chartPoint =>
    string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);

    DataContext = this;
}

如何将RDR["qty"]RDR.GetString(RDR.GetOrdinal("dname"))转到此处string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);。我尝试了不同的方式,如toArray()和所有方法,但它没有用。

1 个答案:

答案 0 :(得分:0)

奇怪为什么你要将数据保存在两个单独的列表中,而不是两者的元组,但无论如何这里有两个版本的代码,一个将它保存在两个列表中,就像你现在这样,另一个没有。

private void MakeData()
    {
        List<string> allValues = new List<string>();
        var NameLables = new List<string>();

        //Simulate the SQL query by looping over X times
        for (int i = 0; i < 5; i++)
        {
            allValues.Add((i + 1).ToString());
            NameLables.Add(DateTime.Today.AddDays(i).ToString("dd-MM-yyyy"));
        }
        seriesCol = new SeriesCollection();

        seriesCol.AddRange(Enumerable.Range(0, allValues.Count).Select(x => new PieSeries { Title = NameLables[x], Values = new ChartValues<ObservableValue> { new ObservableValue(double.Parse(allValues[x])) } }));
    }

第二个版本

private void MakeData2()
    {
        var temp = new SeriesCollection();

        for (int i = 0; i < 5; i++)
        {
            var dname = DateTime.Today.AddDays(i).ToString("dd-MM-yyyy");
            var qty = i;
            temp.Add(new PieSeries { Title = dname, Values = new ChartValues<ObservableValue> { new ObservableValue(qty) } });
        }

        seriesCol = temp;

    }

在这两个示例中,视图模型还具有名为seriesCol

的属性
private SeriesCollection _seriesCol;
    public virtual SeriesCollection seriesCol
    {
        get { return _seriesCol; }
        set
        {
            Set(() => seriesCol, ref _seriesCol, value);
        }
    }

您查看然后绑定到

<lvc:PieChart Grid.Row="1" Series="{Binding Path=seriesCol}"/>