3 Replies - 2526 Views - Last Post: 12 July 2017 - 11:23 PM Rate Topic: -----

#1 jduno   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 23-April 15

Problem Changing Data Point Color MSChart

Posted 25 November 2015 - 02:54 PM

I'm having trouble with what, according to a couple google searches, should be a pretty simple task. I'm attempting to change the color of data points if they meet a certain criteria.

Here is a pic of a simple example:
Attached Image

Basically anything above or below the tolerance (black) bands should be colored red instead of green.

Here are all the methods I've tried so far. None have worked.

NOTE: all charts are created dynamically during run time.

First I tried to change the color of the data point after it had already been added to the series.
eChartList(i).Series("Simulator").Points(eChartList(i).Series("Simulator").Points.Count - 1).Color = Color.Red



Then I tried creating a datapoint variable to change the color prior to adding it to the series
Dim dpoint As DataPoint = New DataPoint()
dpoint.XValue = xCoord
dpoint.YValues = {yCoord}
dpoint.Color = Color.Green

If dpoint.YValues(0) > UpperToleranceValue Or dpoint.YValues(0) < LowerToleranceValue Then

    dpoint.Color = Color.Red

End If

eChartList(i).Series("Simulator").Points.Add(dpoint)



Since neither of my above attempts worked I tried to see if I could get any change whatsoever. I added a button and attempted to loop through some data points and change their color.
For i = 0 To 99

    eChartList(0).Series("Simulator").Points.ElementAt(i).Color = Color.Red

    'ALSO TRIED

    eChartList(0).Series("Simulator").Points(i).Color = Color.Red

Next



Here was another attempt.
For Each dp As DataPoint In eChartList(0).Series("Simulator").Points

    dp.Color = Color.Red

Next



At this point I thought there might be an issue with having to redraw the chart so I added this to the end of all my attempts.
eChartList.Invalidate()



When invalidating the chart didn't work I thought maybe it had to do with my initialization of the series. So instead of setting the default color of the series to blue I left it blank, but the color just defaulted to a random color with the same issues.

I then attempted to change the color of the series to see if it would change instantly without having to redraw the chart.
eChartList(0).Series("Simulator").Color = Color.Red



This code worked as expected, changing all data points in the Simulator series to red.

I'm hoping someone here has had to deal with this issue before or has some other ideas I can try.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem Changing Data Point Color MSChart

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,808
  • Joined: 01-February 13

Re: Problem Changing Data Point Color MSChart

Posted 25 November 2015 - 07:25 PM

Well, without knowing how or when you are adding any of the DataPoints to the chart or how many there actually are per unit it would be hard for anyone to say how you could try to do this.

I am really not familiar with using Chart controls at all but, the only way i can figure is to compare the Y value of the DataPoint you are currently adding with the Y point of the upper and lower tolerance DataPoints at the same X position of the DataPoint you are currently adding.

Of coarse, this will require there to be a high number of points per x and y units. Like you have 0 to 4 seconds, it will need about 100 points per second just to get the line to change colors at a fairly precise location.

Anyways, here is a small example i threw together that you can try in a new form project with 1 Button added to the form. I add the chart dynamically and position the button too, so its location is not important when you add it. Maybe it will help or maybe not.

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1
    Private WithEvents Chart1 As New System.Windows.Forms.DataVisualization.Charting.Chart With {.Width = 550, .Height = 300}

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.ClientSize = New Size(550, 350)
        Me.Controls.Add(Chart1)

        Button1.Location = New Point(CInt(Me.ClientSize.Width / 2 - Button1.Width / 2), Me.ClientRectangle.Bottom - 30)

        With Chart1
            .ChartAreas.Add(New ChartArea)
            .Legends.Add(New Legend)
            .Legends(0).Enabled = False
            .AntiAliasing = AntiAliasingStyles.All
            .TextAntiAliasingQuality = TextAntiAliasingQuality.High
        End With

        With Chart1.ChartAreas(0)
            .AxisX.Title = "Time (sec)"
            .AxisX.MajorGrid.LineColor = Color.LightBlue
            .AxisX.Minimum = 0
            .AxisX.Maximum = 4
            .AxisY.Title = "Leg1"
            .AxisY.MajorGrid.LineColor = Color.LightGray
            .AxisY.Maximum = 4
            .AxisY.Minimum = 0
            .BackColor = Color.FloralWhite
            .BackSecondaryColor = Color.White
            .BackGradientStyle = GradientStyle.HorizontalCenter
            .BorderColor = Color.Blue
            .BorderDashStyle = ChartDashStyle.Solid
            .BorderWidth = 1
            .ShadowOffset = 2
        End With

        'adds the top tolerance line
        Chart1.Series.Add("Tolerance1")
        With Chart1.Series("Tolerance1")
            .ChartType = DataVisualization.Charting.SeriesChartType.Line
            .BorderWidth = 1
            .Color = Color.Black
            For x As Double = 0 To 4 Step 0.01
                .Points.AddXY(x, x + 0.5)
            Next
        End With

        'add the bottom tolerance line
        Chart1.Series.Add("Tolerance2")
        With Chart1.Series("Tolerance2")
            .ChartType = DataVisualization.Charting.SeriesChartType.Line
            .BorderWidth = 1
            .Color = Color.Black
            For x As Double = 0 To 4 Step 0.01
                .Points.AddXY(x, x - 0.5)
            Next
        End With

        'adds a series for the Data
        Chart1.Series.Add("Data")
        With Chart1.Series("Data")
            .ChartType = DataVisualization.Charting.SeriesChartType.Line
            .BorderWidth = 1
        End With

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        With Chart1.Series("Data")

            For YourRetrievedXValue As Double = 0 To 4 Step 0.01
                Dim YourRetrievedYValue As Double = 2.23

                'get the DataPoints of both tolerance series that have the same X value that you are adding
                Dim xx As Double = YourRetrievedXValue
                Dim t1 As DataPoint = (From p As DataPoint In Chart1.Series("Tolerance1").Points Where p.XValue = xx).FirstOrDefault
                Dim t2 As DataPoint = (From p As DataPoint In Chart1.Series("Tolerance2").Points Where p.XValue = xx).FirstOrDefault

                'create the new DataPoint from the X and Y values that you retrieved from wherever
                Dim dpnt As New DataPoint(YourRetrievedXValue, YourRetrievedYValue)

                'check if the Y value is between the 2 tolerance Y values and set its color accordingly
                If YourRetrievedYValue < t1.YValues(0) And YourRetrievedYValue > t2.YValues(0) Then
                    dpnt.Color = Color.Lime
                Else
                    dpnt.Color = Color.Red
                End If

                .Points.Add(dpnt) 'add the new point to the Data series
            Next

        End With

    End Sub
End Class



Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 jduno   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 23-April 15

Re: Problem Changing Data Point Color MSChart

Posted 25 November 2015 - 08:27 PM

Hey Razer thanks for the post. I figured out that it was the chart type that was inhibiting my ability to change the color of the data points. I was setting the chart types of my series to 'FastLine'. Changing the chart type to 'Line' allowed the data points to be recolored properly.
Was This Post Helpful? 1
  • +
  • -

#4 jik   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-July 17

Re: Problem Changing Data Point Color MSChart

Posted 12 July 2017 - 11:23 PM

Thank you so much, this was exactly my problem. It would not have occurred to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1