Allow users to set chart colors

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
datavisualization charting 4.0
I'd like to build in an option for the end user to be able to select line colors of their choice. Is there something out there regarding that?

I did find this Manipulating the Chart Appearance

Visual Basic 2010 doesn't like this code. I looked in references for dlgChart in case that was the issue. Didn't find it or anything like it. I suspect the problem is my chart is a datavisualization chart and this references MSChart. I did fiddle with the code a bit but no luck.

VB.NET:
Private Sub MSChart1_SeriesActivated(ByVal Series As _
   Integer, ByVal MouseFlags As Integer, ByVal Cancel As Integer)
        ' The CommonDialog control is named dlgChart.
        Dim red, green, blue As Integer
        With dlgChart ' CommonDialog object
            .ShowColor()
            red = RedFromRGB(.Color)
            green = GreenFromRGB(.Color)
            blue = BlueFromRGB(.Color)
        End With


        ' NOTE: Only the 2D and 3D line charts use the
        ' Pen object. All other types use the Brush.


        If MSChart1.chartType <> VtChChartType2dLine Or _
        MSChart1.chartType <> VtChChartType3dLine Then
            MSChart1.Plot.SeriesCollection(Series). _
               DataPoints(-1).Brush.FillColor. _
               Set(red, green, blue)
        Else
            MSChart1.Plot.SeriesCollection(Series).Pen. _
               VtColor.Set(red, green, blue)
        End If
    End Sub
 
No, that is old pre-.Net charting, not for VB.Net.

How about Series.Color? Series Class (System.Windows.Forms.DataVisualization.Charting)
For user to select a color there is a standard dialog for that in Dialogs section of Toolbox in designer, ie the ColorDialog.

To get to know the .Net charting I recommend you create a new project to play with in designer, where you set up a basic chart and click into the various properties to get to know how it all connects and what all the options are. For any option you are uncertain of, just select it, and you can read the specific help topic by pressing F1.
For this you don't need to write a single line of code, even sample data can be added in designer. Anything you do in designer will generate code, and you can review the designer generated code if you wish.
Here is help for System.Windows.Forms.DataVisualization.Charting Namespace (), that is the namespace that contains all types related to charting, it can be useful to browse and click into specific topics.
 
No, that is old pre-.Net charting, not for VB.Net.

How about Series.Color? Series Class (System.Windows.Forms.DataVisualization.Charting)
For user to select a color there is a standard dialog for that in Dialogs section of Toolbox in designer, ie the ColorDialog.

To get to know the .Net charting I recommend you create a new project to play with in designer, where you set up a basic chart and click into the various properties to get to know how it all connects and what all the options are. For any option you are uncertain of, just select it, and you can read the specific help topic by pressing F1.
For this you don't need to write a single line of code, even sample data can be added in designer. Anything you do in designer will generate code, and you can review the designer generated code if you wish.
Here is help for System.Windows.Forms.DataVisualization.Charting Namespace (), that is the namespace that contains all types related to charting, it can be useful to browse and click into specific topics.

Thank you.. as you can tell I'm a novice at this. Just started working with VB2010 in the middle of July. Having some VBA experience has helped but a lot of what I run into are differences in the syntax differences that came along with VS2010. I'm constantly fiddling with good code examples from earlier VS versions to make VS10 happy.... :spiny:

Whoever posted that code up should be thanked immensely for their effort...That's simply awesome! I imagine it's bailed out a lot of people :anonymous:

Realizing that people can be color blind to certain colors, I need to implement a solution for that situation. I did manage to find and implement code to allow the user to select any of the available chart types and put that in a combobox. I'm hoping for a similar solution for the series colors. I did look at the link you posted before I posted my question but it's confusing to me at this point.... I'll keep at it...

Ultimately I need to implement series.color as a user selected option and apply it globally throughout the app because I have charts on more than one form.

I implemented this code to select chart type and it works fine.

VB.NET:
Private Sub CboChartType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboChartType.SelectedIndexChanged


        ' Dim value = DirectCast(Me.CboChartType.SelectedValue, DataVisualization.Charting.SeriesChartType)
        Chart1.Series(0).ChartType = System.Enum.Parse(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType), CboChartType.SelectedItem.ToString)
        Chart1.Series(1).ChartType = System.Enum.Parse(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType), CboChartType.SelectedItem.ToString)
        Chart1.Series(2).ChartType = System.Enum.Parse(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType), CboChartType.SelectedItem.ToString)
        Chart1.Series(3).ChartType = System.Enum.Parse(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType), CboChartType.SelectedItem.ToString)
        Chart1.Series(4).ChartType = System.Enum.Parse(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType), CboChartType.SelectedItem.ToString)
        Chart1.Series(5).ChartType = System.Enum.Parse(GetType(System.Windows.Forms.DataVisualization.Charting.SeriesChartType), CboChartType.SelectedItem.ToString)

That code was easy because it encompasses ever series in the chart and sets it up in one click. I need to address each specific series for color and not have it be complicated for the end user. Some options form perhaps and have those settings become global for all charts. YES...it's a significant challenge for me at this point... need assistance with example code...
 
Last edited:
I'd suggest adding a user setting to hold and retain the selected color. In project settings add it in Settings tab (give it a name and select type Color).
To use dialog to select and store value:
        Using dialog As New ColorDialog
            If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                My.Settings.SeriesColor = dialog.Color
            End If
        End Using

To assign Series.Color from setting:
Chart1.Series(0).Color = My.Settings.SeriesColor


Two quick comments about your ChartType code, (1) don't use strings and (2) don't repeat code :) Check into your "SeriesChartType" thread here again if you're uncertain what this means.
 
I'd suggest adding a user setting to hold and retain the selected color. In project settings add it in Settings tab (give it a name and select type Color).
To use dialog to select and store value:
        Using dialog As New ColorDialog
            If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                My.Settings.SeriesColor = dialog.Color
            End If
        End Using

To assign Series.Color from setting:
Chart1.Series(0).Color = My.Settings.SeriesColor


Two quick comments about your ChartType code, (1) don't use strings and (2) don't repeat code :) Check into your "SeriesChartType" thread here again if you're uncertain what this means.

Many than yous for that....I'll give that a whirl! I didn't even know about the project settings..... This is GREAT!
 
I'm looking at settings.settings....really not sure what to do to get this right.
me said:
give it a name and select type Color
If you want to synchronize the setting name with the code sample you use name "SeriesColor".
bones said:
I didn't even know about the project settings
Since you mention the cryptic "settings.settings" file, then apparently you also don't know about project property pages that I directed you to. Project menu > Projectname Properties... Or use context menu on project item in Solution Explorer and select Properties. So much fun in there :)
 
This is what I've done so far. Here's a screen shot. I grabbed the majority of the chart color pallets and added them....How do I get this to the end user as select options?

settings.jpg
 
If you want to synchronize the setting name with the code sample you use name "SeriesColor".

Since you mention the cryptic "settings.settings" file, then apparently you also don't know about project property pages that I directed you to. Project menu > Projectname Properties... Or use context menu on project item in Solution Explorer and select Properties. So much fun in there :)


I followed this How to: Create Property Grids for User Settings in Visual Basic

I have the property grid on the form. The selections do in fact show. What they don't do is change the chart colors....what am I missing.

NOTE: I put the code lines shown in that link in the Form Load event.... I'm missing something to tie it together or I put the last code section in the wrong location....or something else that I don't YET know....
 
I'd have to guess at what your screenshot post 8 is not displaying, and I don't know how you could possibly have managed to do that, but I think you have set type to System.Windows.Media.Color, when it should be System.Drawing.Color.

Further, the setting names and their current values don't really make any sense, you'd have to explain that if it has any meaning.
What they don't do is change the chart colors....what am I missing.
See code sample post 4. It's a simple value assignment (a structure is a value type), if the setting changes you have to set it again.
 
I'd have to guess at what your screenshot post 8 is not displaying, and I don't know how you could possibly have managed to do that, but I think you have set type to System.Windows.Media.Color, when it should be System.Drawing.Color.

Further, the setting names and their current values don't really make any sense, you'd have to explain that if it has any meaning.

See code sample post 4. It's a simple value assignment (a structure is a value type), if the setting changes you have to set it again.


I think I understand now what happened after reading this Configure the PropertyGrid Control - TechExams.net IT Certification Blogs

In the project properties where I setup the user options, under TYPE I browsed and found system.windows.form.datavisualization.chartcolorpallet.

I selected several of those, thinking it would change the colors... I believe I was in error thinking that.

After reading that good article on the property grid I have a clearer understanding of it's use. For one thing I didn't have it pointing to chart1 in the selected object property.... I think I have a handle on what to do now.... It works just like the GUI properties grid...I just have to load it with the correct info and point it to the control correctly....will holler back with results....thanks for the assistance...
 
I see, the ChartColorPalette was new to me. You're thinking to set Chart.Palette rather than a specific Series.Color, but then you also only need one setting, where value can be one of available ChartColorPalette values.
 
I see, the ChartColorPalette was new to me. You're thinking to set Chart.Palette rather than a specific Series.Color, but then you also only need one setting, where value can be one of available ChartColorPalette values.

Exactly.... It looked like the least complicated approach.

For the software I'm developing, the one thing guys don't want is overkill on bells and whistles. It needs to do it's job and be very user intuitive in functions. But since it's very chart dependent they will need a method to tweak that to their preference.

What would be excellent is if they could click on the line in the chart legend and be presented with color choices. Also with the chart background. Not sure I can pull that off though..
 

Latest posts

Back
Top