简体   繁体   English

在WPF Toolkit图表上移动轴标签的位置

[英]Move location of axis labels on WPF Toolkit Chart

I have a chart, all working fine except for I need to have the x-axis labels 'follow' the y-axis zero crossing. 我有一张图表,除了我需要使x轴标签“跟随” y轴零交叉点之外,其他一切正常。

在此处输入图片说明

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. 任何帮助或指针,不胜感激。

Thanks. 谢谢。

After lots of trial and error and reading articles I have finally put together a really simple and elegant solution. 经过大量的反复试验和阅读文章,我终于找到了一个非常简单而优雅的解决方案。

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. 我已经将AxisLabel的margin属性绑定到了图表区域的ActualHeight ,并使用MultiValueConverter创建了正确的margin值。

XAML 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 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();
        }

    }

Range is the maximum and minimum y values when I draw the graph and then bound to a property on the VM. 范围是绘制图形然后绑定到VM上的属性时的最大和最小y值。

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. 调整视图大小后,标签将重新定位为新尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM