简体   繁体   English

如何在vb.net中使用单个数据表创建多条折线图

[英]how to create multi line chart with single datatable in vb.net

I am working on vb.net application where i have to create a multi line chart. 我正在vb.net应用程序上工作,我必须创建一个多折线图。 The table coming from the database using storing procedure is:- 使用存储过程来自数据库的表是:

TagName   On   Off   Trip
  P1      0     0     1
  P2      0     1     1
  P3      0     1     0
  Q1      0     1     0
  Q2      1     0     1
  Q3      2     2     2
  W1      4     2     1
  W2      2     0     1
  W3      1     1     0
  W4      0     1     1
  W5      2     1     1

And the code in vb.net i used to bind the chart named "chTrend" is:- 我用来绑定名为“ chTrend”的图表的vb.net中的代码是:

ds = ObjTags.GetTrendData()
If (ds.Tables(0).Rows.Count > 0) Then

    dt = ds.Tables(0)
    chTrend.DataSource = dt
    chTrend.Series(0).XValueMember = "TagName"
    chTrend.Series(0).YValueMembers = "On"

    chTrend.Series(1).XValueMember = "TagName"
    chTrend.Series(1).YValueMembers = "Off"

    chTrend.Series(1).XValueMember = "TagName"
    chTrend.Series(1).YValueMembers = "Trip"

    For i = 0 To 2
       chTrend.Series(i).ChartType = SeriesChartType.Line
       chTrend.Series(i).IsVisibleInLegend = True
       chTrend.Series(i).IsValueShownAsLabel = True
       chTrend.Series(i).ToolTip = "Data Point Y Value #VALY{G}"
       chTrend.Series(i).BorderWidth = 3
    Next
End If

When i run the program a Error is coming as following 当我运行程序时,错误如下

"Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index"

At the position 在该位置

chTrend.Series(0).XValueMember = "TagName"

how to solve this? 如何解决呢?

Assuming that your DataTable only has the four DataColumns {TagName, On. 假设您的DataTable仅具有四个DataColumns {TagName,On。 Off. Trip}, then replace everything in your shown code below the line: Trip},然后替换下面显示的代码中的所有内容:

chTrend.DataSource = dt

with this. 有了这个。

chTrend.Series.Clear() ' clears any existing series
Dim s As Series
For columnIndex As Int32 = 1 To dt.Columns.Count - 1
    Dim name As String = dt.Columns(columnIndex).ColumnName
    s = chTrend.Series.Add(name)
    s.XValueMember = dt.Columns(0).ColumnName
    s.YValueMembers = name
    s.ChartType = SeriesChartType.Line
    s.IsVisibleInLegend = True
    s.IsValueShownAsLabel = True
    s.ToolTip = "Data Point Y Value #VALY{G}"
    s.BorderWidth = 3
Next
chTrend.DataBind() ' loads the data from dt to the chart

The Chart databinding is a not a binding in the normal sense of binding. 在正常的绑定意义上,Chart数据绑定不是绑定。 You need to tell the chart to copy the data from the source so that it has something to plot. 您需要告诉图表从源复制数据,以便对其进行绘图。 That is what the DataBind method does. 这就是DataBind方法的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM