This is a MSDN question asked by Mike Whalley, the source is Removing line chart datapoints where dependency property is null.
I have a line and bar chart showing the monthly movement in a bank account and the account balance (cashflow) as illustrated:
The account in this example was only opened in December. I want to have all previous datapoints removed (Mar to Nov) so that the line starts only at December (but for design consistency with other accounts I need to keep the whole 12 months range). I had thought that making the dependency value null would do this and accordingly set up my DataPointSeries dependency value as nullable:
private void GetCashLine()
{
List<KeyValuePair<string, decimal?>> balanceList = new List<KeyValuePair<string, decimal?>>();
decimal? balance = null;
for (int i = 0; i < 12; i++)
{
if (monthResult[i].Balance.HasValue)
{
balance = decimal.Parse(((decimal)monthResult[i].Balance).ToString("N2"));
}
else if (i == 0)// Necessary to prevent"No suitable axis is available for plotting the dependent value" error if first X value is null
{
balance = 0M;
}
KeyValuePair<string, decimal?> balancePoint = new KeyValuePair<string, decimal?>(months[startMonth - 1 + i < 0 ? 12 : startMonth - 1 + i], balance?? balance);
balanceList.Add(balancePoint);
}
(columnChart.Series[1] as DataPointSeries).ItemsSource = balanceList;
}
but the datapoints still show as above (note that the first dependency value datapoint cannot be null as this produces an "No suitable axis is available for plotting the dependent value" run-time error).
My XAML is:
<chartingToolkit:Chart Height="260" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="75,-240,0,0" x:Name="columnChart" Title="Cashflow" Width="580" Background="White" ScrollViewer.CanContentScroll="True" BorderBrush="{x:Null}">
<chartingToolkit:Chart.Axes>
<chartingToolkit:CategoryAxis x:Name="labelXAxis" MinWidth="20" VerticalAlignment="Bottom" Orientation="X" ShowGridLines="False"
Margin="2,0,0,-10" />
<chartingToolkit:CategoryAxis x:Name="labelYAxis" Title=" ◄ Overspend Saving ►" MinWidth="20" VerticalAlignment="Bottom" Orientation="Y"
ShowGridLines="False" />
</chartingToolkit:Chart.Axes>
<chartingToolkit:ColumnSeries x:Name="clnsExpenses" Title="Saving or deficit" DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" />
<chartingToolkit:LineSeries x:Name="clnsBalanceLineChart" Foreground="Red" Title="Account balance" DependentValuePath="Value"
IndependentValuePath="Key" ItemsSource="{Binding}">
</chartingToolkit:LineSeries>
</chartingToolkit:Chart>
I need to remove all datapoints where the value of the dependency property is null (but not zero) and would also prefer to overcome the need to set the first datapoint value to null as well.
Many solutions deal only with hiding or removing only the datapoint itself, or the whole series, but I want to delete an individual datapoint and the line joining it to the next datapoints if the it is null.
How can I achieve this?