I'm using the DataVisualization charting within WPF and creating a BarSeries in code, but I can't get the numbers formatted on the X axis.
I don't want to do this in XAML because the type of chart can vary according to user selection and it seemed more flexible to do all of this in code rather than having different chart types. The number of data series also fluctuates.
The XAML is just
xmlns:chttk="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
<chttk:Chart Name="DataChart1" Margin="0" Title="{Binding AxisXTitle}" DataContext="{Binding GraphData1}" Style="{StaticResource ChartStyle1}" BorderBrush="Transparent"/>
The code behind is
Dim dataChart As System.Windows.Controls.DataVisualization.Charting.Chart = DataChart1
Dim s As New System.Windows.Controls.DataVisualization.Charting.BarSeries
s.Title = "My Chart"
s.DependentValuePath = "Value"
s.IndependentValuePath = "Key"
s.DataContext = New Binding("[0]")
s.ItemsSource = CType(itm, IEnumerable)
dataChart.Series.Add(s)
The DataContext is a DataSeries.Collection with the data in KeyValuePairs. The values are all Double values.
The graph is displaying, but the values on the axis are un-formatted, e.g. 120000. I have found a few examples pointing at different Axis types and LabelFormat and LabelStyle.Format but I can't find anything that works in the WPF environment in the code behind.
Thanks for the input - I finally managed to get it working with the following code:
Some of the gotchas that tripped me up were adding the axis. You need to add the right type of axis to the DependentRangeAxis or IndependentAxis. With a BarSeries the DependentRangeAxis is the X axis and can be a LinearAxis and the IndependentAxis is the Y axis and for me was a CategoryAxis. Also if you have multiple series then you need to apply the same axis to all series.
Hope this helps someone else.