Custom values on Y-Axis

2 posts, 0 answers
  1. siva sankar
    siva sankar avatar
    26 posts
    Member since:
    Mar 2011

    Posted 02 Nov 2012 Link to this post

    Hai,
     
                 I want  custom value (like 33,128, 134,138,148,158) on Y-axis of wpf chart, instead of 20,30,40,50,60,70......170.
    Iam attaching my chart here.


    Thanks
  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 07 Nov 2012 Link to this post

    Hello,

    The RadChart does not support such functionality, however you can manually iterate through the labels of the axis and hide all the labels, except the ones which represent a data point present in the chart. Here is an example :
    this.RadChart1.DefaultView.ChartArea.AxisY.Step = 1;
    RadChart1.LayoutUpdated += this.RadChart1_LayoutUpdated;
     
    private void RadChart1_LayoutUpdated(object sender, System.EventArgs e)
    {
        HideUnneededLabels();
    }
             
    void HideUnneededLabels()
    {
        UIElement axisY =
            RadChart1.DefaultView.ChartArea.
            ChildrenOfType<VerticalAxisLabels2D>().FirstOrDefault();
     
        var labels = from axisLabel2D in axisY.ChildrenOfType<AxisLabel2D>()
            select axisLabel2D.ChildrenOfType<TextBlock>().FirstOrDefault();
     
        var yValues = GetUniqueYValues();
     
        foreach (var label in labels)
        {
            if (!yValues.Contains(double.Parse(label.Text)))
                label.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
     
    private HashSet<double> GetUniqueYValues()
    {
        var result = new HashSet<double>();
     
        foreach (var series in this.RadChart1.DefaultView.ChartArea.DataSeries)
        {
            foreach (var dataPoint in series)
            {
                result.Add(dataPoint.YValue);
            }
        }
     
        return result;
    }

    Don't forget to set the "resolution" of the axis by changing the AxisY.Step property to 1 (for example), because otherwise at 137, there won't be a label to hide. I have attached an image of the result that I get.

    Kind regards,
    Petar Kirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top