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

Multiple Y axes with different color

5 Answers 167 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
lin
Top achievements
Rank 1
lin asked on 04 May 2011, 04:01 AM
Hi
   I have a problem about Multiple Y axes with different color. I use seriesMapping to bind data, and the DataSource is not static. I want to give each seriesMapping an  Y axes . I want to set Y axes color same with series color. For example, the second series color is red, I want to set the second  Y axes color red too.
  I have read the example or topic about Multiple Y axes.  In the topic , the colors have been set. I may have much more seriesMapping and Y axes , I can not set each of them by myself, so I want to get the color of series to give Y axes. How can I do that?  Here is my code.

                    SeriesMapping seriesMapping = new SeriesMapping();
                    seriesMapping.LegendLabel = "First";
                    seriesMapping.ItemMappings.Add(new ItemMapping("name1", DataPointMember.XCategory));
                    seriesMapping.ItemMappings.Add(new ItemMapping("value1", DataPointMember.YValue));
                    seriesMapping.SeriesDefinition = new BarSeriesDefinition();
                    radChart.SeriesMappings.Add(seriesMapping);

                    SeriesMapping seriesMapping = new SeriesMapping();
                    seriesMapping.LegendLabel = "Second";
                    seriesMapping.ItemMappings.Add(new ItemMapping("name2", DataPointMember.XCategory));
                    seriesMapping.ItemMappings.Add(new ItemMapping("value2", DataPointMember.YValue));
                    seriesMapping.SeriesDefinition = new BarSeriesDefinition();
                    AxisY axisY2 = new AxisY();
                    axisY2.AxisName = "Second";
                    radChart.DefaultView.ChartArea.AdditionalYAxes.Add(axisY2);
                     seriesMapping.SeriesDefinition.AxisName = "Second";
                    radChart.SeriesMappings.Add(seriesMapping);

                    SeriesMapping seriesMapping = new SeriesMapping();
                    seriesMapping.LegendLabel = "Third";
                    seriesMapping.ItemMappings.Add(new ItemMapping("name3", DataPointMember.XCategory));
                    seriesMapping.ItemMappings.Add(new ItemMapping("value3", DataPointMember.YValue));
                    seriesMapping.SeriesDefinition = new BarSeriesDefinition();
                    AxisY axisY3 = new AxisY();
                    axisY3.AxisName = "Third";
                    radChart.DefaultView.ChartArea.AdditionalYAxes.Add(axisY3);
                     seriesMapping.SeriesDefinition.AxisName = "Second";
                    radChart.SeriesMappings.Add(seriesMapping);

                     ........a lot of   seriesMapping and Y axes . But these Y axes have the same color(black), I want to show different colors, and the series and Y axes are the same color. 
            Thanks in advance. 
                    

                   

5 Answers, 1 is accepted

Sort by
0
Sia
Telerik team
answered on 09 May 2011, 03:25 PM
Hi lin,

Unfortunately there is no available programmatic approach for getting the default series' colors. Please let me know which theme you use and I will send them to you.

>> "I can not set each of them by myself, so I want to get the color of series to give Y axes."
Another possible solution is to have a custom set of as many colors as you need. You need to set them to all series using the PaletteBrushes functionality and to all y axes using the same approach as the one used in the example you mentioned. You can create the styles in code behind.
 
Best wishes,
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
0
Cameron
Top achievements
Rank 1
answered on 19 Jul 2011, 06:55 PM

Hi,

I'd like to set the color of the additional Y axes dynamically. How can I set axis line color programmatically in the code behind? (C#)

I have seen the Telerik's sample with additional Y axes colored differently http://demos.telerik.com/silverlight/#Chart/MultipleYAxes
But in that sample there is a fixed set of colors as defined in the xaml. In my scenario it can be any color.

Thanks for your help,

Cameron
0
Sia
Telerik team
answered on 20 Jul 2011, 02:08 PM
Hi Cameron,

Here is how you can achieve the same but in code behind so that you can set the color dynamically:
SolidColorBrush color = new SolidColorBrush(Colors.Red);
 
Style axisStyle = new Style {TargetType = typeof(System.Windows.Shapes.Line)};
axisStyle.Setters.Add(new Setter{ Property=System.Windows.Shapes.Line.StrokeProperty, Value=color});
Style majorTickLineStyle = new Style {TargetType = typeof(System.Windows.Shapes.Line)};
majorTickLineStyle.Setters.Add(new Setter { Property = System.Windows.Shapes.Line.StrokeProperty, Value = color});
Style minorTickLineStyle = new Style {TargetType = typeof(System.Windows.Shapes.Line)};
minorTickLineStyle.Setters.Add(new Setter {Property = System.Windows.Shapes.Line.StrokeProperty, Value = color});
Style customLabelStyle = new Style {TargetType = typeof(TextBlock)};
customLabelStyle.Setters.Add(new Setter {Property = TextBlock.ForegroundProperty, Value = color});
 
RadChart1.DefaultView.ChartArea.AdditionalYAxes[0].AxisStyles.AxisLineStyle = axisStyle;
RadChart1.DefaultView.ChartArea.AdditionalYAxes[0].AxisStyles.TickLineStyle = majorTickLineStyle;
RadChart1.DefaultView.ChartArea.AdditionalYAxes[0].AxisStyles.MinorTickLineStyle = minorTickLineStyle;
RadChart1.DefaultView.ChartArea.AdditionalYAxes[0].AxisStyles.ItemLabelStyle = customLabelStyle;


Regards,
Sia
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Cameron
Top achievements
Rank 1
answered on 20 Jul 2011, 03:23 PM

Thanks a lot Sia for your reply. It works.

I think my  mistake was to use Telerik.Windows.Controls.Charting.Line rather than System.Windows.Shapes.Line. When Telerik's Line is used to create the style assigned to axes it throws catastrophic exceptions.
 

Style style = new Style(typeof(Telerik.Windows.Controls.Charting.Line));

Thanks,

 

Cameron

 

0
Chris
Top achievements
Rank 1
answered on 19 Mar 2012, 09:14 AM
Great forum post - Please doc this! :)
Tags
Chart
Asked by
lin
Top achievements
Rank 1
Answers by
Sia
Telerik team
Cameron
Top achievements
Rank 1
Chris
Top achievements
Rank 1
Share this question
or