Luminous
Luminous

Reputation: 1909

Data bind an array to a chart series in vb.net

How do you data bind a Series to an array of numbers?

Following this answer I would've hoped it'd be that easy, but it's not.

My basic code example:

dim xarray = {2, 30, 40, 50}
dim yarray = {1, 20, 40, 10}

dim newSeries = new DataVisualization.Charting.Series()
newSeries.ChartType = DataVisualization.Charting.SeriesChartType.Line

newSeries.points.DataBindXY(xarray, yarray)

xarray = {1,2,3,4}
myChart.series.add(newSeries)

Throwing this into a debugger and stepping through you see the two arrays are successfully apart of the series. After the 6th line of code, going back to see if the series was updated shows it wasn't. In my code I have to go digging for these two arrays which reside in a BindingList, but the concept is the same since I'm still working with arrays.

Upvotes: 1

Views: 7150

Answers (1)

Sifu
Sifu

Reputation: 1082

vb.net is a line by line programming language. Sadly, your

newSeries.points.DataBindXY(xarray, yarray) 

actually codes:

newSeries.points.DataBindXY({2, 30, 40, 50} , {1, 20, 40, 10}).

So, modifying later on xarray won't modify the chart. Everytime you modify xarrayor yarray, you have the call DatabindXY.

So, to work, it would have to be :

dim xarray = {2, 30, 40, 50}
dim yarray = {1, 20, 40, 10}

dim newSeries = new DataVisualization.Charting.Series()
newSeries.ChartType = DataVisualization.Charting.SeriesChartType.Line

newSeries.points.DataBindXY(xarray, yarray)

xarray = {1,2,3,4}
newSeries.points.DataBindXY(xarray, yarray)

myChart.series.add(newSeries)

Upvotes: 1

Related Questions