Hi, I am attempting to add a custom line or custom grid line annotation to my RadChart. I need to make the line dashed, but do so completely in c#, not using any XAML styles. How would I do this?
Also, how would I add my annotation to my legend, completely in code. I saw the example:
http://www.telerik.com/help/wpf/radchart-how-to-add-annotations-in-chart-legend.html, but that uses XAML styles, and I can't do that.
How would I ensure that the annotation appears in front of the data series? I.E. the bars for the bar chart are behind the annotation.
And finally, how would I add labels to annotations, again, in c# code only.
Also, how would I add my annotation to my legend, completely in code. I saw the example:
http://www.telerik.com/help/wpf/radchart-how-to-add-annotations-in-chart-legend.html, but that uses XAML styles, and I can't do that.
How would I ensure that the annotation appears in front of the data series? I.E. the bars for the bar chart are behind the annotation.
And finally, how would I add labels to annotations, again, in c# code only.
4 Answers, 1 is accepted
0
Hello Mike,
Thanks for contacting us.
As you can see from the help topics here and here, you can add annotations in code-behind (C#) like this:
This is how you can convert a Style from XAML to C#:
XAML:
C#:
When it comes to using it:
XAML:
C#:
In theory anything written in XAML can be done in C#.
That said it is possible to rewrite the the Styles and templates from this article, however it would not be practical, will require a lot of effort and is out of the scope of Telerik Support.
The only thing you need to get it running is to copy-paste everything that start with <Style ... to the <Window.Resources> of your xaml file and put the chart itself ( <telerik:RadChart ... ) in the LayoutRoot (typically the <Grid x:Name="LayoutRoot" ... )
I hope this helps.
Regards,
Petar Kirov
the Telerik team
Thanks for contacting us.
As you can see from the help topics here and here, you can add annotations in code-behind (C#) like this:
Style dashedLineStyle =
new
Style(
typeof
(System.Windows.Shapes.Line));
dashedLineStyle.Setters.Add(
new
Setter(System.Windows.Shapes.Line.StrokeDashArrayProperty,
"1 2 3"
));
this
.RadChart1.DefaultView.ChartArea.Annotations.Add(
new
CustomGridLine
{
XIntercept = 7,
Stroke =
new
SolidColorBrush(Colors.Blue),
StrokeThickness = 2,
ElementStyle = dashedLineStyle
});
Style dashedLineStyle2 =
new
Style(
typeof
(System.Windows.Shapes.Line));
dashedLineStyle2.Setters.Add(
new
Setter(System.Windows.Shapes.Line.StrokeDashArrayProperty,
"1 2 4 8"
));
this
.RadChart1.DefaultView.ChartArea.Annotations.Add(
new
CustomLine
{
MinX = 2,
MinY = 2,
MaxX = 5,
MaxY = 10,
Slope = 2.66,
YIntercept = -3.32,
Stroke =
new
SolidColorBrush(Colors.Red),
StrokeThickness = 2,
ElementStyle = dashedLineStyle2
});
This is how you can convert a Style from XAML to C#:
XAML:
<
Style
x:Key
=
"customGridLineStyle"
TargetType
=
"Line"
>
<
Setter
Property
=
"StrokeDashArray"
Value
=
"1,1"
/>
</
Style
>
C#:
Style customGridLineStyle =
new
Style();
customGridLineStyle.TargetType =
typeof
(System.Windows.Shapes.Line);
customGridLineStyle.Setters.Add(
new
Setter(System.Windows.Shapes.Line.StrokeDashArrayProperty,
"1 1"
));
When it comes to using it:
XAML:
<Window.Resources>
<Style x:Key=
"customGridLineStyle"
TargetType=
"Line"
>
<Setter Property=
"StrokeDashArray"
Value=
"1,1"
/>
</Style>
</Window.Resources>
<Grid x:Name=
"LayoutRoot"
>
<Line Style=
"{StaticResource customGridLineStyle}"
/>
C#:
Style customGridLineStyle =
new
Style();
customGridLineStyle.TargetType =
typeof
(System.Windows.Shapes.Line);
customGridLineStyle.Setters.Add(
new
Setter(System.Windows.Shapes.Line.StrokeDashArrayProperty,
"1 1"
));
var line =
new
System.Windows.Shapes.Line();
line.Style = customGridLineStyle;
this
.LayoutRoot.Children.Add(line);
In theory anything written in XAML can be done in C#.
That said it is possible to rewrite the the Styles and templates from this article, however it would not be practical, will require a lot of effort and is out of the scope of Telerik Support.
The only thing you need to get it running is to copy-paste everything that start with <Style ... to the <Window.Resources> of your xaml file and put the chart itself ( <telerik:RadChart ... ) in the LayoutRoot (typically the <Grid x:Name="LayoutRoot" ... )
I hope this helps.
Regards,
Petar Kirov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
0
Mike
Top achievements
Rank 1
answered on 23 Nov 2012, 03:51 PM
Thank you for this. It makes sense. However, when I try to do this for a Telerik.Windows.Controls.Charting.CustomLine as follows:
I get an error saying:
'1 1' is not a valid value for the 'System.Windows.Shapes.Shape.StrokeDashArray' property on a Setter.
Any ideas?
Also, is there any way to bring the annotation to the front of the chart instead of behind the chart series?
Style customGridLineStyle =
new
Style();
customGridLineStyle.TargetType =
typeof
(System.Windows.Shapes.Line);
customGridLineStyle.Setters.Add(
new
Setter(System.Windows.Shapes.Line.StrokeDashArrayProperty,
"1 1"
));
customLine.ElementStyle = customGridLineStyle;
I get an error saying:
'1 1' is not a valid value for the 'System.Windows.Shapes.Shape.StrokeDashArray' property on a Setter.
Any ideas?
Also, is there any way to bring the annotation to the front of the chart instead of behind the chart series?
0
Mike
Top achievements
Rank 1
answered on 23 Nov 2012, 07:42 PM
I have figured out the issue above. I solved it by setting the value of the property to a DoubleCollection.
My only remaining issue is bringing the annotation to the front of the chart series. Is this possible?
DoubleCollection dc =
new
DoubleCollection();
dc.Add(1.0);
dc.Add(1.0);
Style customGridLineStyle =
new
Style();
customGridLineStyle.TargetType =
typeof
(System.Windows.Shapes.Line);
customGridLineStyle.Setters.Add(
new
Setter(System.Windows.Shapes.Line.StrokeDashArrayProperty, dc));
My only remaining issue is bringing the annotation to the front of the chart series. Is this possible?
0
Hi Mike,
It seams that the problem comes from the framework. I initially tested this in Silverlight and the code I provided worked as expect, but setting the StrokeDashArray property as DoubleCollection didn't.
By creating a sample SL and WPF projects containing only a Line and setting their style through code-behind you can reproduce this issue.
On second question - by default the layer that holds the annotations is behind the series. However you can change that by re-templating the ChartArea. This help topic describes exactly how.
Kind regards,
Petar Kirov
the Telerik team
It seams that the problem comes from the framework. I initially tested this in Silverlight and the code I provided worked as expect, but setting the StrokeDashArray property as DoubleCollection didn't.
By creating a sample SL and WPF projects containing only a Line and setting their style through code-behind you can reproduce this issue.
On second question - by default the layer that holds the annotations is behind the series. However you can change that by re-templating the ChartArea. This help topic describes exactly how.
Kind regards,
Petar Kirov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.