I cannot seem to draw a smooth curve over a bar chart (in the same chart). I do not see any StackOverflow questions pertaining (but maybe missed).
Anyway, to do this, I was thinking of using a ColumnSeries for the bars with a LineSeries for the curve. Issues seem to be:
1) Points that are in the LineSeries but not in the ColumnSeries always appear at the end in the chart (even if the X values are in between those that exist in the ColumnSeries). I was thinking these in between values could be used to make the LineSeries curve smooth.
2) The LineSeries also controls the spacing between X values. All I want is the LineSeries to be used to draw the smooth curve between the bars. As it is, having many points in the LineSeries makes the bars very thin where they should be thick. In other words, the ColumnSeries ideally would control the tick marks and bar thickness (not the LineSeries).
The xaml is
<Grid>
<chartingToolkit:Chart x:Name="MainChart" HorizontalAlignment="Left" Margin="59,35,0,0" Title="Chart Title" VerticalAlignment="Top" Height="246" Width="405">
<chartingToolkit:ColumnSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding BarCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding LineCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</chartingToolkit:Chart>
</Grid>
The code behind is
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
// Add sample curve points
LineCollection.Add(new KeyValuePair<double, double>(1, 11));
LineCollection.Add(new KeyValuePair<double, double>(1.5, 18));
LineCollection.Add(new KeyValuePair<double, double>(2, 22));
LineCollection.Add(new KeyValuePair<double, double>(2.5, 35));
LineCollection.Add(new KeyValuePair<double, double>(3, 42));
// Primary bars being added
BarCollection.Add(new KeyValuePair<double, double>(1, 10));
BarCollection.Add(new KeyValuePair<double, double>(2, 20));
BarCollection.Add(new KeyValuePair<double, double>(3, 40));
}
/// <summary>
/// Core event handler for the view model.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Routes property changed events.
/// </summary>
/// <param name="propertyName"> Defines the property name upon which routing is determined. </param>
public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
ObservableCollection<KeyValuePair<double, double>> barCollection = new ObservableCollection<KeyValuePair<double, double>>();
public ObservableCollection<KeyValuePair<double, double>> BarCollection
{
get
{
return barCollection;
}
set
{
barCollection = value;
}
}
ObservableCollection<KeyValuePair<double, double>> lineCollection = new ObservableCollection<KeyValuePair<double, double>>();
public ObservableCollection<KeyValuePair<double, double>> LineCollection
{
get
{
return lineCollection;
}
set
{
lineCollection = value;
}
}
}
Thanks,
Buck