Integers only for LinearAxis

4 posts, 1 answers
  1. BJans
    BJans avatar
    3 posts
    Member since:
    Sep 2013

    Posted 17 Sep 2013 Link to this post

    Hello,

    I've run into some issues with the RadCartesianChart where I want to display only integer values along the LinearAxis. It does not make sense to measure the item I'm plotting along this axis in decimals. Another thing I'd like is to have a DesiredTickCount of 5. I've tried setting the label format to not show decimals but then this leads to weirdness when the Maximum value calculated by the control is very low (ex. Maximum of 1 shows me 5 ticks with the labels of (blank), (blank), 1, 1, 2). I also tried to set the Major Step to 1 but that caused issues with the DesiredTickCount of 5 when the Maximum was greater than 5.

    Is there some way to force the chart to only calculate in Integers? Or set the, for lack of better words, Minimum Maximum to just 5 so it calculates the ticks as integers?

    Here is the code for my VerticalAxis:
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis DesiredTickCount="5" Minimum="0">
            <telerik:LinearAxis.LabelTemplate>
                <DataTemplate>
                    <TextBlock MinWidth="40" Margin="0,0,3,0" Text="{Binding}" TextAlignment="Right" />
                </DataTemplate>
            </telerik:LinearAxis.LabelTemplate>
        </telerik:LinearAxis>
    </telerik:RadCartesianChart.VerticalAxis>
  2. Answer
    Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 23 Sep 2013 Link to this post

    Hi BJans,

    In order to make the axis labels calculation integer based, you will need to set an integer MajorStep. Note that the MajorStep and DesiredTickCount properties are mutually exclusive. This means that DesiredTickCount is respected only when MajorStep is not set.
    One thing you can try is to bind the Minimum, Maximum, MajorStep and/or LabelStep properties to your ViewModel and recalculate them every time  your data changes. Here's an example:

    void CalculateRange()
    {
        int min = 0; //or this.Data.Min(p => p.Value)
        int max = this.Data.Max(p => p.Value);
       
        int desiredTickCount = 5;
        double step = 0;
       
        if (max - min > 5)
        {
            step = (max - min) / desiredTickCount;
            this.MajorStep = (int)Math.Round(step);
            //or this.LabelStep = (int)Math.Round(step);
        }
        else
            this.MajorStep = 1; // or this.LabelStep = 1;
    }
    I hope this helps.

    Regards,
    Petar Kirov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. BJans
    BJans avatar
    3 posts
    Member since:
    Sep 2013

    Posted 23 Sep 2013 Link to this post

    Thanks Petar, It worked great.

    Just a P.S.- I originally had a radiobutton that changed the chart to also show sales data which was currency, but was unable to use the same code that you posted by changing the int to a decimal. It would create a large amount of steps for some reason, so many it was just a big black block because they were printing over each other. I ended up just using 2 charts and flipping the visibility of them on/off which worked. I just didn't understand why the code wouldn't work for a decimal since it seemed to calculate the MajorStep correctly when I stepped through it.
  5. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 25 Sep 2013 Link to this post

    Hi BJans,

    I am not sure why the same code does not work for both of your scenarios. It would be helpful if you could open a support ticket and attach a project that reproduces the issue, so we can examine it and help you solve the issue (hopefully in a more elegant way).

    On a side note, if you want re-enable the automatic axis range calculations, you can un-bind the properties that you are using and clear their values:
    BindingOperations.ClearBinding(this.chart.VerticalAxis,
        LinearAxis.MajorStepProperty);
     
    BindingOperations.ClearBinding(this.chart.VerticalAxis,
        LinearAxis.MinimumProperty);
     
    BindingOperations.ClearBinding(this.chart.VerticalAxis,
        LinearAxis.MaximumProperty);
     
    (this.chart.VerticalAxis as LinearAxis).ClearValue(LinearAxis.MajorStepProperty);
    (this.chart.VerticalAxis as LinearAxis).ClearValue(LinearAxis.MinimumProperty);
    (this.chart.VerticalAxis as LinearAxis).ClearValue(LinearAxis.MaximumProperty);

     

    Regards,
    Petar Kirov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top