Reputation: 688
I'm trying to set from the Code Behind a color of some line in Chart. I have searched for 3 hours how to do it and I couldn't find anything. Is it possible do to that? If it can't be do, please recommend another chart library where I can do that.
Thanks!
XAML
<Charting:Chart Title="Name" x:Name="LineChart" HorizontalAlignment="Left" VerticalAlignment="Center" Width="510" Height="450">
<Charting:LineSeries Title="PB" IndependentValuePath="Name" DependentValuePath="Amount" IsSelectionEnabled="True"/>
<Charting:LineSeries Title="Oil" IndependentValuePath="Name" DependentValuePath="Amount" IsSelectionEnabled="True"/>
</Charting:Chart>
WPF
Random rand = new Random();
List<FinancialStuff> financialStuffList = new List<FinancialStuff>();
for (int i = 0; i < 30; i++ )
financialStuffList.Add(new FinancialStuff() { Name = Convert.ToString(i), Amount = rand.Next(0, 200) });
(LineChart.Series[0] as LineSeries).ItemsSource = financialStuffList;
for (int i = 0; i < 30; i++)
financialStuffList[i].Amount = rand.Next(0, 50);
(LineChart.Series[1] as LineSeries).ItemsSource = financialStuffList;
public class FinancialStuff
{
public string Name { get; set; }
public int Amount { get; set; }
}
Upvotes: 0
Views: 928
Reputation: 76
If anyone has problems, with Olaru Mircea solution in here
Style style = new Style(typeof(Control));
style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red)));
style.Setters.Add(new Setter(Control.HeightProperty, 5));
style.Setters.Add(new Setter(Control.WidthProperty, 5));
series.DataPointStyle = style;
just modify it like this:
Style style = new Style(typeof(Control));
style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red)));
style.Setters.Add(new Setter(Control.HeightProperty, 5.0));
style.Setters.Add(new Setter(Control.WidthProperty, 5.0));
series.DataPointStyle = style;
The first threw an exception for me, but after using double values, it worked fine.
Upvotes: 1
Reputation: 2620
Style style = new Style(typeof(Control));
style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red)));
style.Setters.Add(new Setter(Control.HeightProperty, 5));
style.Setters.Add(new Setter(Control.WidthProperty, 5));
series.DataPointStyle = style;
Have you tried this? Try to set it when you get the line here
(LineChart.Series[0] as LineSeries).DataPointStyle = style;
You may also try the following XAML:
<charting:LineSeries.DataPointStyle>
<Style TargetType="charting:LineDataPoint">
<Setter Property="Width" Value="17" />
<Setter Property="Height" Value="17" />
<Setter Property="Background" Value="Lime"/>
</Style>
</charting:LineSeries.DataPointStyle>
Upvotes: 1