开发者

LinearAxis without decimal numbers

开发者 https://www.devze.com 2023-02-06 08:36 出处:网络
I want to avoid from decima开发者_如何学JAVAl numbers in my Axis, how can I do that ? XAML: <Charting:ChartVerticalAlignment=\"Stretch\"

I want to avoid from decima开发者_如何学JAVAl numbers in my Axis, how can I do that ?

LinearAxis without decimal numbers

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>


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.


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.


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.


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.


Just use the Interval property of LinearAxis:

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

Works for me pretty well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号