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

Integers only for LinearAxis

3 Answers 308 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
BJans
Top achievements
Rank 1
BJans asked on 17 Sep 2013, 06:39 PM
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>

3 Answers, 1 is accepted

Sort by
0
Accepted
Petar Kirov
Telerik team
answered on 23 Sep 2013, 05:26 AM
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 >>
0
BJans
Top achievements
Rank 1
answered on 23 Sep 2013, 02:46 PM
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.
0
Petar Kirov
Telerik team
answered on 25 Sep 2013, 05:46 AM
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 >>
Tags
ChartView
Asked by
BJans
Top achievements
Rank 1
Answers by
Petar Kirov
Telerik team
BJans
Top achievements
Rank 1
Share this question
or