This is a migrated thread and some comments may be shown as answers.

How to apply color for AdditionalYAxes through coding?

1 Answer 99 Views
Chart
This is a migrated thread and some comments may be shown as answers.
satish
Top achievements
Rank 1
satish asked on 23 Aug 2010, 09:46 AM
Hi All,

I am using the RadChart for WPF in my project for displaying the Chart as LineSeries, my requirment is like this:
 1) I need to add dataPoints dynamically by reading the output file for each Dataseries
  2) I need to add AdditionalYAxes for each Dataseries
  3) I need to Apply ForeGround color for each DataSeries
   4)And the same DataSeires color i need to apply color for related AdditionalYAxes
 
I have done upto the Above Three Steps and i am able to add AdditionalYAxes. But i am not able to Apply the color Style for the AdditionYAxes line and also the AdditionalYAxes is not start from X-axes, for now it is starting from X-axes one Step above.

I need to apply the color for AdditionaYAxes for the relating DataSeries and The Line should Start from X-Axes position.

i have attached my codeSnippet below:
.XAML file

<

 

 

UserControl x:Class="TabControl.MainWindow"

 

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

 

xmlns:telerikChart="http://schemas.telerik.com/2008/xaml/presentation"

 

 

 

xmlns:telerikCharting="clr-namespace:Telerik.Windows.Controls.Charting;assembly=Telerik.Windows.Controls.Charting"

 

 

 

Height="Auto" Width="Auto">

 

 

 

 

<Grid>

 

 

 

 

<Grid.Resources>

 

 

 

 

<Style x:Name="CustomAxisLineStyle" TargetType="Line">

 

 

 

 

<Setter Property="Stroke" Value="Red" />

 

 

 

 

<Setter Property="StrokeThickness" Value="5" />

 

 

 

 

</Style>

 

 

 

 

</Grid.Resources>

 

 

 

 

<telerikChart:RadChart x:Name="_myChart">

 

 

 

 

</telerikChart:RadChart>

 

 

 

 

</Grid>

 

</

 

 

UserControl>

 


.Xaml.cs file

 

 

private void GenerateSeries(string target, ref DateTime timeStamp, double avgVal, string counterName)

 

{

 

 

int _minsOffSet=0;

 

 

 

DateTime minDate = DateTime.MaxValue;

 

 

 

DateTime maxDate = DateTime.MinValue;

 

 

 

double minVal = double.MaxValue;

 

 

 

double maxVal = double.MinValue;

 

 

 

DataSeries counterSeries = new DataSeries();

 

counterSeries.Definition =

 

new LineSeriesDefinition() { ShowItemLabels = false, ShowItemToolTips = true };

 

counterSeries.Definition.ItemToolTipFormat =

 

"#SERIESLABEL\n#Y";

 

counterSeries.Definition.AxisName = counterName;

 

 

AxisY yAxis = new AxisY();

 

yAxis.AxisName = counterName;

 

 

string legendName = string.Format("{0}", counterName);

 

SetLegendDetails(legendName,

 

ref counterSeries, ref yAxis);

 

 

 

DateTime time = timeStamp.AddMinutes(_minsOffSet);

 

 

minVal =

 

Math.Min(minVal, avgVal);

 

maxVal =

 

Math.Max(maxVal, avgVal);

 

 

 

DataPoint dp = new DataPoint(time.ToOADate(), avgVal);

 

dp.IsDateTime =

 

true;

 

counterSeries.Add(dp);

 

 

if (time > maxDate)

 

maxDate = time;

 

 

if (time < minDate)

 

minDate = time;

 

 

if (avgVal > maxVal)

 

maxVal = avgVal;

 

 

if (avgVal > minVal)

 

minVal = avgVal;

_myChart.DefaultView.ChartArea.AxisX.MinValue = minDate.ToOADate();

_myChart.DefaultView.ChartArea.AxisX.MaxValue = maxDate.ToOADate();

_myChart.DefaultView.ChartArea.AxisY.MinValue = minVal;

_myChart.DefaultView.ChartArea.AxisY.MaxValue = maxVal;

_myChart.DefaultView.ChartArea.DataSeries.Add(counterSeries);

_myChart.DefaultView.ChartArea.AdditionalYAxes.Add(yAxis);

 

 

double graphVariation = maxVal - minVal;

 

 

 

if ((graphVariation > 2) & (graphVariation <= 9))

 

{

_myChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat =

 

"n1";

 

}

 

 

else if (graphVariation <= 2)

 

{

_myChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat =

 

"n3";

 

}

 

 

else

 

{

_myChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat =

 

"n0";

 

_myChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat =

 

"0";

 

}

}

 

 

private void SetLegendDetails(string legendName, ref DataSeries dataSeries, ref AxisY yAxis)

 

{

_myChart.DefaultView.ChartLegend.Header =

 

"";

 

_myChart.DefaultView.ChartLegend.UseAutoGeneratedItems =

 

true;

 

dataSeries.LegendLabel = legendName;

dataSeries.Definition.Appearance.Foreground =

 

new SolidColorBrush(Colors.Green);

 

 

 

Style customAxisYStyle = this.Resources["CustomAxisYStyle"] as Style;

 

_myChart.DefaultView.ChartArea.AxisY.AxisStyles.AxisLineStyle = customAxisYStyle;

 

}

I have attaching the Chart ScreenShot what i implemented.
Please see in Attachments
i am  calling the GenerateSeries Method every time for loading the X-Axis value and Y-Axes value.
Please any one help me how to apply color and starting position of AdditionaYAxes.
This is very urgent Requirment.

Thanks and Regards
Satish

1 Answer, 1 is accepted

Sort by
0
Sia
Telerik team
answered on 26 Aug 2010, 08:14 AM
Hi Satish,

As I understand from your post you need to set different stroke color for every additional y axis. I noticed something wrong in your code - please use the following style for your axes:

<Style x:Key="CustomAxis" TargetType="Line">
      <Setter Property="Stroke" Value="Green" />
      <Setter Property="StrokeThickness" Value="5" />
</Style>

The difference is that you need to use x:Key instead of x:Name. Having separate style for every y axis should fix your problem.

Regarding your second question (about the start point of the additional y axes), I have tried to reproduce it with the provided code, but unfortunately with no success. A possible hint is that may be it is caused by the data you set.

Could you please provide us with more information? A sample project would be the best option.

Thank you,
Sia
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Chart
Asked by
satish
Top achievements
Rank 1
Answers by
Sia
Telerik team
Share this question
or