Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

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()
        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 String() = (From p In dt.AsEnumerable()
                                 Where p.Field(Of String)("Department") = department
                                 Order By p.Field(Of String)("year")
                                 Select p.Field(Of String)("year")).ToArray

            Dim y As Decimal() = (From p In dt.AsEnumerable()
                                  Where p.Field(Of String)("Department") = department
                                  Order By p.Field(Of String)("year")
                                  Select p.Field(Of Decimal)("Total")).ToArray()

            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
        Next
        Chart1.Legends(0).Enabled = True

    End Sub


What I have tried:

I tried googeling but all the results that i got is how to change the colors in properties.

Any help is appricitated.
Bests E
Posted
Updated 23-Jul-20 20:45pm

1 solution

If you want to modify the Behaviour of any Control it works only with the Properties of the Control. Chart is also a Control. But Properties not only belong to the Designer and the Designtime - they could (as you allready do it) also be changed within the code (for example inside a loop).
Why don't you set the LineColor you want to have for your Series inside the Loop which creates the different Series ?
 
Share this answer
 
Comments
Member 13410460 24-Jul-20 10:25am    
HEllo , Thank you for help, I looked in google how to change the colors in the loop but I could not find anything so specific. Could you help me somehow on that topic please.

Bests
E
Ralf Meier 24-Jul-20 16:50pm    
in your case ;
Chart1.Series(department).Color = Green // for example
Member 13410460 24-Jul-20 18:07pm    
This Changes all 5 spline lines to green.
Ralf Meier 25-Jul-20 13:18pm    
Yes ... of course ... you should now set each new Series to a new (different) color ...
That means : inside your loop you have to do this - for eample with a pre-defined array where the 1st element is Green, the 2nd Red, the 3rd Blue and so on ...
Member 13410460 25-Jul-20 13:40pm    
Thank you for your response, Do you have any link that I can take as an example for the loop?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900