Change color of each line in Spline Chart using C# and VB.Net in Windows Application

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Hello Everyone,

How do I put different color for each spline line in my data chart

I am doing a project in my vb.net and I have a chart that shows me 5 different lines for my data.

Here is the code that I am using for my Project :

    Private Sub DataCharts()
        Dim dt As DataTable = GetData("Chart_Monthly1")
        Dim departments As List(Of String) = (From p In dt.AsEnumerable()
                                              Select p.Field(Of String)("Department")).Distinct().ToList()

        Dim years As List(Of String) = (From p In dt.AsEnumerable()
                                        Order By p.Field(Of String)("year")
                                        Select p.Field(Of String)("year")).Distinct().ToList()

        If Chart1.Series.Count() = 1 Then
            Chart1.Series.Remove(Chart1.Series(0))
        End If
        Chart1.Series.Clear()

        For Each department As String In departments
            Dim x As List(Of String) = New List(Of String)()
            Dim y As List(Of Decimal) = New List(Of Decimal)()
            For Each year As String In years
                x.Add(year)
                y.Add((From p In dt.AsEnumerable()
                       Where p.Field(Of String)("Department") = department And p.Field(Of String)("year") = year
                       Order By p.Field(Of String)("year")
                       Select p.Field(Of Decimal)("Total")).FirstOrDefault())
            Next
            Chart1.Series.Add(New Series(department))
            Chart1.Series(department).IsValueShownAsLabel = True
            Chart1.Series(department).BorderWidth = 2
            Chart1.Series(department).ChartType = SeriesChartType.Spline
            Chart1.Series(department).Points.DataBindXY(x, y)
            Chart1.Series(department).LabelForeColor = Color.MediumSeaGreen
            Chart1.Series(department).EmptyPointStyle.Color = Color.Red
            Chart1.Series(department).EmptyPointStyle.AxisLabel = "Empty"
        Next
        Chart1.Legends(0).Enabled = True
        Dim MyData As Single() = New Single(Chart1.Series.Count - 1) {}
        For i = 0 To Chart1.Series.Count - 1
            MyData(i) = Convert.ToSingle(5 * Math.Sin(i / CDbl(2)))
            Dim withBlock = Chart1.Series(i)
            withBlock.BorderWidth = 2
            withBlock.MarkerStyle = MarkerStyle.Circle
            withBlock.MarkerSize = 8
            Chart1.Annotations.Add(New CalloutAnnotation())
        Next

    End Sub

What I have tried: I tried googling but all the results that I got are how to change the colors in properties which when I do it changes all the color of the spline line. 

Any help is appreciated.

You are viewing reply posted by: dharmendr 7 months ago.
Posted 7 months ago

Use this.

Private Sub DataCharts()
    Dim dt As DataTable = GetData("Chart_Monthly1")
    Dim departments As List(Of String) = (From p In dt.AsEnumerable()
                                            Select p.Field(Of String)("Department")).Distinct().ToList()

    Dim years As List(Of String) = (From p In dt.AsEnumerable()
                                    Order By p.Field(Of String)("year")
                                    Select p.Field(Of String)("year")).Distinct().ToList()

    If Chart1.Series.Count() = 1 Then
        Chart1.Series.Remove(Chart1.Series(0))
    End If
    Chart1.Series.Clear()
    Dim rnd As Random = New Random()
    For Each department As String In departments
        Dim x As List(Of String) = New List(Of String)()
        Dim y As List(Of Decimal) = New List(Of Decimal)()
        For Each year As String In years
            x.Add(year)
            y.Add((From p In dt.AsEnumerable()
                    Where p.Field(Of String)("Department") = department And p.Field(Of String)("year") = year
                    Order By p.Field(Of String)("year")
                    Select p.Field(Of Decimal)("Total")).FirstOrDefault())
        Next
        Chart1.Series.Add(New Series(department))
        Chart1.Series(department).IsValueShownAsLabel = True
        Chart1.Series(department).BorderWidth = 2
        Chart1.Series(department).ChartType = SeriesChartType.Spline
        Chart1.Series(department).Points.DataBindXY(x, y)
        Chart1.Series(department).LabelForeColor = Color.MediumSeaGreen
        Chart1.Series(department).EmptyPointStyle.Color = Color.Red
        Chart1.Series(department).EmptyPointStyle.AxisLabel = "Empty"
        'Chart1.Series(department).Color = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256))
    Next
    Chart1.Series(0).Color = Color.Red
    Chart1.Series(1).Color = Color.Green
    Chart1.Series(2).Color = Color.Yellow
    Chart1.Series(3).Color = Color.Violet
    Chart1.Series(4).Color = Color.Orange
    Chart1.Legends(0).Enabled = True
    Dim MyData As Single() = New Single(Chart1.Series.Count - 1) {}
    For i = 0 To Chart1.Series.Count - 1
        MyData(i) = Convert.ToSingle(5 * Math.Sin(i / CDbl(2)))
        Dim withBlock = Chart1.Series(i)
        withBlock.BorderWidth = 2
        withBlock.MarkerStyle = MarkerStyle.Circle
        withBlock.MarkerSize = 8
    Next
End Sub