LinearAxis without decimal numbers

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

I want to avoid from decimal numbers in my Axis, how can I do that ?

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> 

回答1:

In case you're still struggling on this, or if anyone else is interested: the solution is almost what baalazamon wrote. It's just that {0:0.##} will display two decimal digits if they exists (that's what ".##" means). So what you should write is

<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 will solve your problem.



回答2:

LinearAxis has an Iterval property. Try to set

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

According with your comment (sorry, i thinked the problem was simpler ;)), i used a similar approach to render the label on Y axis:

in resources, use a style like 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 a LinearAxis with this style to your chart. My NumericConverter just Test the value of the label that chart want to display and format it accordingly with my logic. You can test if the value is integer, so return the correct string or empty otherwise. I think it can work.



回答3:

You can change the style to this one:

<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> 

Additionally you need to create:

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

It's only example and you need to adjust it to your needs. Example of slightly different thing but maybe you will find it useful.



回答4:

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

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 thing, and just override the interval when you need to. Easy to change the above to round to the nearest whole number.



回答5:

Just use the Interval property of LinearAxis:

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

Works for me pretty well.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!