سؤال

I'm trying to show a 'customized' ToolTip on an MSChart on an asp.net page, using vb.net

The chart displays OK, but I'm trying to get it to show the 'YEAR' as part of the tooltip, as well as the XY values.

I can't figure out how to do it.

Here's the code that I'm using to build the chart:

    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()
هل كانت مفيدة؟

المحلول

Well, there may be a better answer, but I figured out a work-around anyhow ... I'm adding the YEAR to the axislabel. Then, in chart1_customize, changing the color of the bar, based on different axislabel. Seems to work.

    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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top