-1

我正在尝试使用 vb.net 在 asp.net 页面上的 MSChart 上显示“自定义”工具提示

该图表显示正常,但我试图让它显示“YEAR”作为工具提示的一部分,以及 XY 值。

我不知道该怎么做。

这是我用来构建图表的代码:

    dt = New DataTable
    dt.Columns.Add("Topic")
    dt.Columns.Add("Value")
    dt.Columns.Add("Year")

    For i = 0 To t_YEARS.Count - 1
        Sql = "SELECT att_Topic, att_Value, att_Year from Att "
        Sql += " WHERE att_Year = '" & t_YEARS(i) & "' "
        conn.ConnectionString = strConnString
        conn.Open()
        cmd = New SqlCommand(Sql, conn)
        dr = cmd.ExecuteReader
        While dr.Read
            dt.Rows.Add(dr(0), dr(1), dr(2))
        End While
        dr.Close()
        cmd.Dispose()
        conn.Close()
    Next

    Chart1.DataSource = dt
    Chart1.Series("Series1").XValueMember = "Topic"
    Chart1.Series("Series1").YValueMembers = "Value"
    Chart1.Series("Series1").ToolTip = "#VALX - #VALY"
    Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True
    Chart1.DataBind()
4

1 回答 1

0

好吧,可能有更好的答案,但无论如何我想出了一个解决方法......我将 YEAR 添加到轴标签中。然后,在chart1_customize 中,根据不同的axislabel 更改条形的颜色。似乎工作。

    dt = New DataTable
    dt.Columns.Add("Topic")
    dt.Columns.Add("Value")
    dt.Columns.Add("Year")

    For i = 0 To t_YEARS.Count - 1
        showDATA = False
        Sql = "SELECT att_Topic, att_Value, att_Year, att_Data from BWS_Att "
        If (RBL_LIMIT.SelectedValue = 1) Then
            showDATA = True
            Sql += " WHERE att_Attrib = 'Location' "
            Sql += " AND att_Data IN ('" & String.Join("','", t_LOCS) & "')"
        ElseIf (RBL_LIMIT.SelectedValue = 2) Then
            showDATA = True
            Sql += " WHERE att_Attrib = 'Department' "
            Sql += " AND att_Data IN ('" & String.Join("','", t_DEPTS) & "')"
        Else
            Sql += " WHERE att_Attrib = 'Company' "
        End If
        Sql += " AND att_Year = '" & t_YEARS(i) & "' "
        Sql += " AND att_Topic IN ('" & String.Join("','", t_CATS) & "')"
        Sql += " Order By att_ind"
        conn.ConnectionString = strConnString
        conn.Open()
        cmd = New SqlCommand(Sql, conn)
        dr = cmd.ExecuteReader
        While dr.Read
            'dt.Rows.Add(dr(0), dr(1), dr(2))
            thisYR = dr(2).ToString
            If (lastYR <> thisYR) Then
                Chart1.Series("Series1").Points.Add(vbEmpty)
                Chart1.Series("Series1").Points.Add(vbEmpty)
                lastYR = thisYR
            End If
            If (showDATA = True) Then
                Chart1.Series("Series1").Points.AddXY(dr(2).ToString & "|" & dr(3).ToString & ":" & dr(0).ToString, dr(1))
            Else
                Chart1.Series("Series1").Points.AddXY(dr(2).ToString & ":" & dr(0).ToString, dr(1))
            End If
            Chart1.Series("Series1").ToolTip = " #AXISLABEL | #VALY"
        End While
        dr.Close()
        cmd.Dispose()
        conn.Close()
    Next

~~~~~~~~~~

    Private Sub Chart1_Customize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Chart1.Customize
    Dim C() As Drawing.Color = { _
        Drawing.Color.Khaki, _
        Drawing.Color.DarkSalmon, _
        Drawing.Color.Goldenrod, _
        Drawing.Color.MediumAquamarine, _
        Drawing.Color.Tan _
    }

    Dim CN As Int16 = 0
    Dim thisC As Int16 = 0
    Dim LAST As String = String.Empty

    For Each dp As System.Web.UI.DataVisualization.Charting.DataPoint In Chart1.Series("Series1").Points
        Dim x As Array = dp.AxisLabel.Split(":")
        If (x(0) <> "") Then
            Dim H As String = x(0)
            If (LAST <> H) Then
                CN += 1
                LAST = H
                thisC = (CN Mod 5)
            End If
            dp.Color = C(thisC)
        End If
    Next
End Sub
于 2013-10-11T15:01:14.170 回答