Is it possible to set a chart datasource from a generic list?

前端 未结 1 1454
不知归路
不知归路 2020-12-12 01:44

I have a list created from a linq query that contains 2 columns of data.

var result = root.Descendants().Elements(\"sensor\")
                 .Where(el =&         


        
相关标签:
1条回答
  • 2020-12-12 01:59

    Yes it is possible, but not by binding the list to the Chart itself.

    There are several quite different methods to do Chart Databinding, all with different sets of pros and cons..

    I suggest using the Series.Points.DataBindXY or Series.Points.DataBind methods.

    Here is an example:

    // create a list with test data:
    List<PointF> points = new List<PointF>();
    for (int i = 0; i < 100; i++) points.Add(new PointF(i, 1f * i / 2f * R.Next(8)));
    

    Now create a generic list from it:

    var al = points.Select(x => new { t1 = x.X, v1 = x.Y }).ToList();
    

    Now this works:

    someSeries.Points.DataBindXY(al, "t1", al, "v1");
    

    or also this:

    someSeries.DataBind(al, "t1", "v1", "" );
    

    In your case you would write maybe this:

    chart1.Series[0].Points.DataBind(result, "t1", "v1");
    

    Note that a Chart typically can only display pairs of values whereas a DGV can create as many columns as the DataSource has. So Chart needs a little help in finding the x- and y-values..

    0 讨论(0)
提交回复
热议问题