Chart Type as a selected option

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I have a chart on a user form that can contain from 0 to 6 series. It's defined as a SPLINE chart.

I would like to put a combobox on the form to allow the users to select the type of chart they prefer.

1 - How can I pass that as a variable into this code?
2 - Can it be applied globally to all series in the chart from one combox?

VB.NET:
Case RBtnPort1.Checked = True

                    Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Spline
                    Chart1.Series(0).Points.Clear()
                    For Count As Integer = 0 To DataGridView1.Rows.Count - 2
                    Chart1.Series(0).Points.AddXY(DataGridView1.Item(0, Count).Value, DataGridView1.Item(2, Count).Value)
                    Next
 
For example like this, add values:
Me.ComboBoxChartType.DataSource = System.Enum.GetValues(GetType(DataVisualization.Charting.SeriesChartType))

Get selected value:
Dim value = DirectCast(Me.ComboBoxChartType.SelectedValue, DataVisualization.Charting.SeriesChartType)
 
For example like this, add values:
Me.ComboBoxChartType.DataSource = System.Enum.GetValues(GetType(DataVisualization.Charting.SeriesChartType))

Get selected value:
Dim value = DirectCast(Me.ComboBoxChartType.SelectedValue, DataVisualization.Charting.SeriesChartType)

Thank you for the help. Here's what I ran into.

I found some code while I was waiting for a response and applied it. It works but there's a glitch I can't figure out.

Form Load
VB.NET:
CboChartType.Items.AddRange({"Line", "spline", "fastline"})
        Dim rndm As New Random
        For i As Integer = 1 To 10
            Chart1.Series(0).Points.AddY(random.Next(1, 21))
        Next

Combobox Code

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




        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)


    End Sub

If, in the form load code I use Line & Column, the code will change the chart type to line or column. That's not what I'm after. In VB2010 chart properties you can specifically select each series type as line, spline, fastline, etc.

I have the chart properties set to spline at the moment and want to switch between spline, line or fastline. When I use those values in the form load code it blows up with the message "requested value not found"

So my question is twofold...

Should I NOT use the charts properties to establish the series type if I'm also trying to use code? I would like to because I want SPLINE to be the default and allow the user to select the other two options if they wish to change that?

How do I define the types I want? Since they are actual property values I assumed that was what I should put in as chart type?
 
it blows up with the message "requested value not found"
I guess that is because of your string handling. Instead use the proper values:
Me.ComboBoxChartType.DataSource = {SeriesChartType.Line, SeriesChartType.Spline, SeriesChartType.FastLine}

Getting the value same as previous example.
 
I guess that is because of your string handling. Instead use the proper values:
Me.ComboBoxChartType.DataSource = {SeriesChartType.Line, SeriesChartType.Spline, SeriesChartType.FastLine}

Getting the value same as previous example.

Should I use that line of code in the combobox code or in the my other sub?
 
Use that to add the values to the control.
 

Latest posts

Back
Top