locked
splinearea colors not change RRS feed

  • Question

  • In this example:
    Imports System.Math
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim n As Integer
            Dim f As Single
    
            Chart1.ChartAreas(0).AxisX.Minimum = 0
            Chart1.ChartAreas(0).AxisX.Maximum = 100
            Chart1.ChartAreas(0).AxisY.Minimum = -100
            Chart1.ChartAreas(0).AxisY.Maximum = 100
            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.SplineArea
    
            For n = 0 To 100
                f = 20 * Sin(0.1 * n)
                If f > 0 Then
                    Me.Chart1.Series(0).Color = Color.Red
                Else
                    Me.Chart1.Series(0).Color = Color.Blue
                End If
                Me.Chart1.Series(0).Points.AddXY(n, f)
            Next
    
        End Sub
    End Class

    why upper part is not red?
    Thursday, February 13, 2020 8:07 PM

Answers

  • Thanks tommy2train.

    I managed to do do this using DrawPolygon, but this is far better. It uses the chart.

    Regards

    Saturday, February 15, 2020 8:12 PM

All replies

  • Hi Vergassivellaunus,

    Thank you for posting here.

    According to your question, you can refer to the following code to correctly change splinearea colors:

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim n As Integer
            Dim f As Double
            Chart1.ChartAreas(0).AxisX.Minimum = 0
            Chart1.ChartAreas(0).AxisX.Maximum = 100
            Chart1.ChartAreas(0).AxisY.Minimum = -100
            Chart1.ChartAreas(0).AxisY.Maximum = 100
            Chart1.Series(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea
            Dim lst1 As List(Of Integer) = New List(Of Integer)()
            Dim lst2 As List(Of Integer) = New List(Of Integer)()
    
            For n = 0 To 100
                f = 20 * Math.Sin(0.1 * n)
                Dim index As Integer = Me.Chart1.Series(0).Points.AddXY(n, f)
                If f > 0 Then
                    lst1.Add(index)
                Else
                    lst2.Add(index)
                End If
            Next
    
            For Each index As Integer In lst1
                Chart1.Series(0).Points(index).Color = Color.Red
            Next
    
            For Each index As Integer In lst2
                Chart1.Series(0).Points(index).Color = Color.Blue
            Next
        End Sub

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by tommytwotrain Friday, February 14, 2020 10:15 AM
    Friday, February 14, 2020 2:44 AM
  • Very good Xing!

    Where did you find how to do that?

    PS Appears the list is not required?   :)

     

        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim n As Integer
            Dim f As Double
            Chart1.ChartAreas(0).AxisX.Minimum = 0
            Chart1.ChartAreas(0).AxisX.Maximum = 100
            Chart1.ChartAreas(0).AxisY.Minimum = -100
            Chart1.ChartAreas(0).AxisY.Maximum = 100
            Chart1.Series(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea
    
            For n = 0 To 100
                f = 20 * Math.Sin(0.1 * n)
                Dim index As Integer = Me.Chart1.Series(0).Points.AddXY(n, f)
                If f > 0 Then
                    Chart1.Series(0).Points(index).Color = Color.Red
                Else
                    Chart1.Series(0).Points(index).Color = Color.Blue
                End If
            Next
    
        End Sub


    Friday, February 14, 2020 10:15 AM
  • Hy Xingyu Zhao,

    thanks for your solution.

    My way of reasoning it is not liked by Visual Studio.

    Enzo

    Friday, February 14, 2020 11:48 AM
  • Hy Xingyu Zhao.

    I have a problem when drawing in vertical. First i have to say that chart must be set: Charting.SeriesChartType.Area, if you set: Charting.SeriesChartType.SplineArea

    all becomes very slow.

    Anyawy, if I draw a vertical graph:

    With its code:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim n, x_max As Integer
            Dim f As Double
            x_max = 256    
            Chart1.ChartAreas(0).AxisX.Minimum = -100
            Chart1.ChartAreas(0).AxisX.Maximum = 100
            Chart1.ChartAreas(0).AxisY.Minimum = 0
            Chart1.ChartAreas(0).AxisY.Maximum = x_max
            Chart1.Series(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line
            Me.Chart1.Series(0).BorderWidth = 1
            Dim lst1 As List(Of Integer) = New List(Of Integer)()
            Dim lst2 As List(Of Integer) = New List(Of Integer)()
    
            For n = 0 To x_max
                f = 80 * Math.Sin(6 * 3.141 * n / x_max)
                Dim index As Integer = Me.Chart1.Series(0).Points.AddXY(f, n)           
    
                If f > 0 Then
                    lst1.Add(index)
                Else
                    lst2.Add(index)
                End If
            Next
    
            For Each index As Integer In lst1
                Chart1.Series(0).Points(index).Color = Color.Red
            Next
    
            For Each index As Integer In lst2
                Chart1.Series(0).Points(index).Color = Color.Blue
            Next
        End Sub

    and then set Chart1.Series(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Area , to fill, i get:

    When it fills it fills only in vertical mode. Is there a way to change it?

    Regards





    Saturday, February 15, 2020 1:19 PM
  • I'm tryng with DrawPolygon(.......)

    If i succede let you know.

    Saturday, February 15, 2020 5:17 PM
  • You can do it this way. It might be just as easy to draw your own chart all the way instead of the chart control?

    You use chart control in vb6?

    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form8
    
        Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
            Dim ptsRed As New List(Of PointF)
            Dim ptsGreen As New List(Of PointF)
            Dim x1, x, y As Single
    
            Dim x0 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(
                Chart1.ChartAreas(0).Name, AxisName.X, Chart1.Series(0).Points(0).XValue))
    
    
            For i = 0 To Chart1.Series(0).Points.Count - 1
                x1 = Chart1.Series(0).Points(i).XValue
                x = CSng(e.ChartGraphics.GetPositionFromAxis(
                              Chart1.ChartAreas(0).Name, AxisName.X, x1))
                y = CSng(e.ChartGraphics.GetPositionFromAxis(
                              Chart1.ChartAreas(0).Name, AxisName.Y, Chart1.Series(0).Points(i).YValues(0)))
    
                If x1 > 0 Then
                    ptsRed.Add(e.ChartGraphics.GetAbsolutePoint(New PointF(x, y)))
                    ptsGreen.Add(e.ChartGraphics.GetAbsolutePoint(New PointF(x0, y)))
                Else
                    ptsRed.Add(e.ChartGraphics.GetAbsolutePoint(New PointF(x0, y)))
                    ptsGreen.Add(e.ChartGraphics.GetAbsolutePoint(New PointF(x, y)))
                End If
            Next
    
            ptsRed.Add(New PointF(ptsRed(0).X, ptsRed(ptsRed.Count - 1).Y))
            ptsGreen.Add(New PointF(ptsGreen(0).X, ptsGreen(ptsGreen.Count - 1).Y))
    
            With e.ChartGraphics.Graphics
                .FillPolygon(Brushes.Red, ptsRed.ToArray)
                .FillPolygon(Brushes.Green, ptsGreen.ToArray)
            End With
    
        End Sub
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim n As Integer
            Dim f As Double
            Chart1.ChartAreas(0).AxisX.Minimum = -50
            Chart1.ChartAreas(0).AxisX.Maximum = 50
            Chart1.ChartAreas(0).AxisY.Minimum = 0
            Chart1.ChartAreas(0).AxisY.Maximum = 100
    
            Chart1.Series(0).Name = "Chart1"
            Chart1.Series(0).ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine
            Chart1.Series(0).Color = Color.Black
            Chart1.Series(0).IsVisibleInLegend = False
    
            For n = 0 To 100
                f = 20 * Math.Sin(0.1 * n)
                Chart1.Series(0).Points.AddXY(f, n)
            Next
    
        End Sub
    End Class


    Saturday, February 15, 2020 5:19 PM
  • Thanks tommy2train.

    I managed to do do this using DrawPolygon, but this is far better. It uses the chart.

    Regards

    Saturday, February 15, 2020 8:12 PM