LinearAxis without decimal numbers

I want to avoid decimal numbers in my axis, how can I do this?

enter image description here

XAML:

<Charting:Chart VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <Charting:Chart.Axes> <Charting:LinearAxis Orientation="Y" Minimum="0" Title="" Location="Left" /> </Charting:Chart.Axes> <Charting:Chart.Series> <Charting:ColumnSeries ItemsSource="{Binding Persons}" DependentValueBinding="{Binding Count}" IndependentValueBinding="{Binding Category}"> </Charting:ColumnSeries> </Charting:Chart.Series> </Charting:Chart> 
+4
source share
5 answers

If you are still struggling with this, or if someone else is interested: the solution is what Baalazamon wrote. It's just that {0: 0. ##} will display two decimal digits if they exist (which means ". ##"). So what you have to write

 <Style x:Key="EmptyStyle" TargetType="charting:NumericAxisLabel"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="StringFormat" Value="{0:0}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="charting:NumericAxisLabel"> <TextBlock /> </ControlTemplate> </Setter.Value> </Setter> </Style> 

And, of course, you need to add this:

 <charting:LineSeries.DependentRangeAxis> <charting:LinearAxis AxisLabelStyle="{StaticResource EmptyStyle}" Orientation="Y" ShowGridLines="True"/> </charting:LineSeries.DependentRangeAxis> 

I hope this solves your problem.

+3
source

LinearAxis has the Iterval property. Try to install

  <Charting:Chart.Axes> <Charting:LinearAxis Interval="1" Orientation="Y" Minimum="0" Title="" Location="Left" /> </Charting:Chart.Axes> 

According to your comment (sorry, I thought the problem was easier;)), I used a similar approach to render the label along the Y axis:

in resources, use a style similar to this

 <Style x:Key="ChartLabelNoDecimal" TargetType="chartingToolkit:AxisLabel"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="chartingToolkit:AxisLabel"> <TextBlock DataContext="{TemplateBinding FormattedContent}" Text="{Binding Converter={StaticResource NumericConverter1}}" FontSize="9" /> </ControlTemplate> </Setter.Value> </Setter> </Style> public class NumericConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { double x = double.Parse(value.ToString()); if(/*check if has decimals*/) return string.Empty; else return x; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 

then you can add LinearAxis with this style to your chart. My NumericConverter just check the label value that the chart wants to display and format accordingly, with my logic. You can check if the value is an integer, so return the correct string or empty otherwise. I think it can work.

+2
source

You can change the style to this:

 <Style x:Key="EmptyStyle" TargetType="charting:NumericAxisLabel"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="StringFormat" Value="{}{0:0.##}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="charting:NumericAxisLabel"> <TextBlock /> </ControlTemplate> </Setter.Value> </Setter> </Style> 

In addition, you need to create:

 <charting:LineSeries.DependentRangeAxis> <charting:LinearAxis AxisLabelStyle="{StaticResource EmptyStyle}" Orientation="Y" ShowGridLines="True"/> </charting:LineSeries.DependentRangeAxis> 

This is just an example, and you need to customize it to your needs. An example of a slightly different thing , but you might find it useful.

+1
source

This is the solution I came up with to fix this, excerpt from mine:

 public class BarSeriesAxis : LinearAxis { protected override double CalculateActualInterval(Size availableSize) { var result = base.CalculateActualInterval(availableSize); return (result < 1.0) ? 1.0 : result; } } 

You let the charts do their job and simply redefine the interval when you need to. Easy to modify above to round to nearest integer.

+1
source

Just use the Interval LinearAxis property:

 <chartingToolkit:LinearAxis Orientation="Y" Interval="1" Minimum="0"/> 

Works well for me.

+1
source

Source: https://habr.com/ru/post/1336925/


All Articles