Bjørn
Bjørn

Reputation: 1148

ASP:Chart from datatable with many series

This is what the graph is currently looking like:

Example graph

This is some of the datatable i am getting data from:

CountElem   userid  AddedDate
1433        1   2012-10-08
1327        1   2012-10-09
1514        1   2012-10-10
793         1   2012-10-11
219         6   2012-08-12
249         6   2012-08-13
289         6   2012-08-14
319         6   2012-08-15
233         6   2012-08-16

What happens is that it draws all CountElem and AddedDate for all userids in one series, instead of drawing for each CountElem and AddedDate for each userid in it's own series.

This is my code:

        mainChart.DataSource = dataTable;
        foreach (DataRow dataRow in users.Rows)
        {
            Series s = new Series("Userid_" + dataRow["userid"]);
            s.ChartType = SeriesChartType.Spline;

            if (colors.Length == i)
                i = 0;
            s.Color = colors[i];

            s.XValueMember = "AddedDate";
            s.YValueMembers = "CountElements";

            mainChart.Series.Add(s);
            i++;
        }

The problem i that i cant figure out how to "split" the series. It draws all data in each series, which then produces the lines between each start and stop of the series.

Is there a way to define that each series only draws for one userid?

I feel like providing one datatable per series. But i can not find out how this would be done, and i do not believe this the way it is designed to work...

Upvotes: 1

Views: 1487

Answers (1)

jbl
jbl

Reputation: 15413

You may try binding your series with filtered DataRow sets, something like this (not tested, sorry for any syntax error) :

    foreach (DataRow dataRow in users.Rows)
    {
        Series s = new Series("Userid_" + dataRow["userid"]);
        s.ChartType = SeriesChartType.Spline;

        s.Color = colors[i % colors.Count()];

        var userRows = dataTable.Rows.Cast<DataRow>().Select(r=>r["userid"]==dataRow["userid"]).ToArray();

        s.Points.DataBindXY(userRows,"AddedDate",userRows,"CountElements");

        mainChart.Series.Add(s);
        i++;
    }

Anyway, be aware that, unless all your series are aligned (ie : share exactly the same set of x values), the chart engine will completely mess-up with missing x values. You may have have to fill missing x values with empty points.

Hope this will help

Upvotes: 1

Related Questions