Charting with VB and WPFToolkit

fatron

Member
Joined
May 13, 2010
Messages
18
Programming Experience
1-3
I've been searching google for the last couple of hours looking for a good example of how to put together a line chart using the wpf datavisualization controls, but haven't found anything useful. I've found a couple of C# examples, but haven't been able to translate them properly.

Basically, I'm just wanting to programmatically create a line chart and populate a data series with data I've loaded from a file. I can create the chart without problems, I just can't seem to figure out how to populate it with data.

Can anyone point me in the direction of a vb example?

Thanks
 
I finally figured this out, so I'm posting my results here for anyone who may search on how to do this. I'm sure there are better ways to do this, but this method worked for me.

These instructions assume you already have a chart in your project named MyChart. This code is just meant as a guide and won't work if you just copy and paste it. You'll need to modify it to suit your data.

The first thing you need to do is create a series for the chart. In this case, we're making a line chart. The series is what actually contains the data for drawing the line on the chart. You can create individual series, or an array of series depending on your needs.

VB.NET:
Dim MySeries as new charting.lineseries

The next thing you need to do is create a collection of keyvaluepairs. For my chart, the pair consists of DateTime on the X axis and Double on the Y axis. The KeyValuePair will be populated with your data, then added to the series.

VB.NET:
Dim MyData as Collection(Of KeyValuePair(Of DateTime, Double))

Now you need to populate the collection with your data. For this example, we'll assume there's an array of a structure called ExistingData with the elements SampleDate as data and SampleValue as double.

VB.NET:
Dim MyData as New Collection(of KeyValuePair(Of DateTime, Double))
For Counter= 0 to SampleData.Count-1
   Dim TempKVPair as new KeyValuePair(Of DateTime, Double)(ExistingData(counter).SampleDate, ExistingData(counter).SampleValue)
    Mydata.add(tempKVPair)
Next

Next, we add our data to the series

[CODE]
MySeries.DependentValuePath="Value"
MySeries.IndependentValuePath="Key"
MySeries.ItemsSource=MyData
MySeries.Title = "Demo data series"

Then finally, we add our series to our chart

VB.NET:
MyChart.Series.Add(MySeries)
 
Back
Top