在WPF Toolkit图表上移动轴标签的位置 [英] Move location of axis labels on WPF Toolkit Chart

查看:132
本文介绍了在WPF Toolkit图表上移动轴标签的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图表,除了我需要使x轴标签跟随" y轴零交叉点之外,其他一切工作正常.

I have a chart, all working fine except for I need to have the x-axis labels 'follow' the y-axis zero crossing.

我一直在使用标签边距,可以将标签向网格线的左侧或右侧偏移

I have been playing with the label margins and can offset the label to the left or right of the gridline

<DVC:LinearAxis.AxisLabelStyle>
                <Style TargetType="{x:Type DVC:AxisLabel}">
                    <Setter Property="Margin" Value="25,0,0,0" />
                </Style>
            </DVC:LinearAxis.AxisLabelStyle>

我不知道如何将标签向上移动到图表的中间.我将绑定值来确定确切位置,但是我什至无法弄清楚如何在垂直平面上移动标签.

I haven't got a clue how to move the label up the chart so that it appears in the middle. I will bind to value for determining the exact location but I can't even work out how to move the label in the vertical plane.

非常感谢任何帮助或指针.

Any help or pointers much appreciated.

谢谢.

推荐答案

经过反复试验和阅读文章,我终于找到了一个非常简单而优雅的解决方案.

After lots of trial and error and reading articles I have finally put together a really simple and elegant solution.

我将AxisLabel的margin属性绑定到图表区域的ActualHeight,并使用MultiValueConverter创建正确的margin值.

I have bound the margin property of the AxisLabel to the ActualHeight of the chart area and used a MultiValueConverter to create the correct margin values.

XAML

<DVC:LinearAxis.AxisLabelStyle>
    <Style TargetType="{x:Type DVC:AxisLabel}">
        <Setter Property="Margin">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource MarginConverter}">
                    <Binding Path="ActualHeight"
                        RelativeSource="{RelativeSource AncestorType={x:Type primitives:EdgePanel}}" />
                    <Binding Path="DataContext.ChartRange" RelativeSource="{RelativeSource FindAncestor, 
                        AncestorType={x:Type DVC:LinearAxis}}" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</DVC:LinearAxis.AxisLabelStyle>

MarginConverter.cs

public class MarginConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var height = 0d;
            var chartHeight = (double) values[0];
            var range = (Range<double>) values[1];

            if (range.HasData)
            {
                if (range.Minimum > 0)
                {
                    // Set labels to bottom
                    height = 0;
                }
                else if (range.Maximum < 0)
                {
                    // Set labels to top
                    height = -chartHeight;
                }
                else
                {
                    var rangeHeight = range.Maximum - range.Minimum;
                    var pointsPerHeight = chartHeight / rangeHeight;
                    height = range.Minimum * pointsPerHeight;
                }
            }

            return new Thickness(25, height, 0, 0);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

范围是我绘制图形然后绑定到VM上的属性时的最大y值和最小y值.

Range is the maximum and minimum y values when I draw the graph and then bound to a property on the VM.

这是一个令人惊讶的优雅解决方案,可以解决我认为很棘手的问题.调整视图大小后,标签将重新定位为新尺寸.

This is a surprisingly elegant solution to what I thought was going to be quite hacky. As the view is resized, the labels are repositioned with new dimensions.

这篇关于在WPF Toolkit图表上移动轴标签的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆