Silentbob Silentbob -4 years ago 230
ASP.NET (C#) Question

Displaying a chart from a datatable

I am using VS 2015 and I want to display a data table I have created as a chart.

The data table looks like this.

Data table view

I havent really used charts before and I have tried a few examples/tutorials found online but I am not getting anywhere.

I would like a line chart with the x axis as timestamp, y as value and 3 lines (in this example) for each tag name.

I have added the control to the page using

<asp:Chart ID="Chart1" runat="server">
<asp:Series Name="Series1"></asp:Series>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>

This is what I have in my VB so far but I am aware it probably doesnt contain enough/correct information for the chart to display.

With Chart1.Series(0)

.Points.DataBind(trendTagData, "timestamp", "Value", Nothing)

End With

Any help greatly appreciated.

Answer Source

VB is really not my thing, but this is a one-liner:

    Chart1.DataBindCrossTable(dt.DefaultView, "tagname", "timestamp", "value", "");

enter image description here


You may also need to do this, AFTER you call DataBindCrossTable, in order to get a Line chart type:

        Chart1.Series["tag1"].ChartType = SeriesChartType.Line;
        Chart1.Series["tag2"].ChartType = SeriesChartType.Line;
        Chart1.Series["tag3"].ChartType = SeriesChartType.Line;


if you don't know how many tag names are in your table, do this:

        foreach (Series s in Chart1.Series)
            s.ChartType = SeriesChartType.Line;

Also, note you can remove Series1 from your ASPX markup.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download