Search code examples
c#wpfxamlchartsdatavisualization.toolkit

Charting Toolkit Custom Horizontal Axis values


I am plotting a graph of frequency against loudness(decibels) using Charting toolkit. the horizontal axis frequency has values of 100, 250, 500,1000,2000,4000,6000, and 8000. When I plot the graph, the horizontal axis shows frequency values according to the 'Interval', which stays the same across x-axis. And my x-axis values are of different intervals. Update: I want to get a line graph. Here is the full code XAML is:

   <chartingToolkit:Chart Name="lineChart"
        Title="Results">
        <chartingToolkit:LineSeries Name="MyData"
            Title="Calculated data"  
            DependentValuePath="Value" 
            IndependentValuePath="Key"
            ItemsSource="{Binding [0]}"/>
        <chartingToolkit:Chart.Axes>
            <chartingToolkit:LinearAxis 
                Orientation="Y"
                Minimum="0" Maximum="80"
                Title="Loudness in dB"
                Interval="5" />
            <chartingToolkit:LinearAxis Name="xAxis"
                Orientation="X"
                Minimum="0" Maximum="8000"
                Title="Frequency in Hz"
                Interval="500" />
       </chartingToolkit:Chart.Axes>
   </chartingToolkit:Chart>

and c# code is:

    private void ShowLineChart(myChartResult) 
// myChartResult is a List of objects
// each object has a frequency and decibel field
    {
        var dataSourceList = new List<List<KeyValuePair<int, int>>>();

        for (int i = 0; i < myChartResult.Count; i++)
            dataSourceList.Add(new KeyValuePair<int, int>
               (myChartResult[i].frequency,
               myChartResult[i].decibels));
        lineChart.DataContext = dataSourceList;
}

In the graph, the decibel values against 100 and 250 are plotted but the horizontal axis doesn't show the frequency values of 100 and 250. Is there a way to customize (in XAML or c#) the x-axis labels so that I can show exactly the values I mentioned above?


Solution

  • Use a ColumnSeries:

        <chartingToolkit:Chart Margin="0" Title="Chart Title">
            <chartingToolkit:Chart.DataContext>
                <local:MyDataCollection/>
            </chartingToolkit:Chart.DataContext>
            <chartingToolkit:ColumnSeries DependentValuePath="Decibel" IndependentValuePath="Frequency" ItemsSource="{Binding}"/>
        </chartingToolkit:Chart>
    

    enter image description here