Multiple Y axes with different color

6 posts, 0 answers
  1. lin
    lin avatar
    1 posts
    Member since:
    May 2011

    Posted 03 May 2011 Link to this post

    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. 
                        

                       
  2. Sia
    Admin
    Sia avatar
    667 posts

    Posted 09 May 2011 Link to this post

    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
  3. DevCraft banner
  4. Cameron
    Cameron avatar
    17 posts
    Member since:
    Jan 2011

    Posted 19 Jul 2011 Link to this post


    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
  5. Sia
    Admin
    Sia avatar
    667 posts

    Posted 20 Jul 2011 Link to this post

    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!

  6. Cameron
    Cameron avatar
    17 posts
    Member since:
    Jan 2011

    Posted 20 Jul 2011 Link to this post


    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

     

  7. Chris
    Chris avatar
    16 posts
    Member since:
    Apr 2008

    Posted 19 Mar 2012 Link to this post

    Great forum post - Please doc this! :)
Back to Top
DevCraft banner