C#WPF Telerik RadCartesianChart:尽管存在标签,也强制设置恒定的轴长

时间:2018-11-26 14:08:18

标签: wpf charts telerik

我创建了一个非常简单的图表,如下所示:

<telerik:RadCartesianChart x:Name="chartMainCandleChart" Height="350" Width="620" VerticalAlignment="Top" HorizontalAlignment="Left" ZoomChanged="chartMainCandleChart_ZoomChanged" PanOffsetChanged="chartMainCandleChart_OffsetChanged" >
    <telerik:RadCartesianChart.HorizontalAxis>
        <telerik:DateTimeCategoricalAxis SmartLabelsMode="SmartStep" IsStepRecalculationOnZoomEnabled="False" />
    </telerik:RadCartesianChart.HorizontalAxis>
    <telerik:RadCartesianChart.VerticalAxis>
        <telerik:LinearAxis HorizontalLocation="Right" SmartLabelsMode="SmartStep" IsStepRecalculationOnZoomEnabled="False"/>
    </telerik:RadCartesianChart.VerticalAxis>
    <telerik:CandlestickSeries x:Name="candleSeries" ItemsSource="{Binding SeriesCollection}" CategoryBinding="Timestamp" OpenBinding="Open" CloseBinding="Close" LowBinding="Low" HighBinding="High">
        <telerik:CandlestickSeries.DefaultVisualStyle>
            <Style TargetType="telerik:Candlestick">
                <Setter Property="StrokeThickness" Value="2" />
                <Setter Property="UpStroke" Value="GreenYellow"/>
                <Setter Property="DownStroke" Value="Red"/>
                <Setter Property="UpFill" Value="GreenYellow"/>
                <Setter Property="DownFill" Value="Red"/>
            </Style>
        </telerik:CandlestickSeries.DefaultVisualStyle>
    </telerik:CandlestickSeries>

    <telerik:RadCartesianChart.Behaviors>
        <telerik:ChartPanAndZoomBehavior x:Name="bvPanZoom" MouseWheelMode="ZoomHorizontally" DragMode="Pan" PanMode="Horizontal" ZoomMode="None" />
    </telerik:RadCartesianChart.Behaviors>

    <telerik:RadCartesianChart.Resources>
        <Style TargetType="telerik:PanZoomBar">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
    </telerik:RadCartesianChart.Resources>
</telerik:RadCartesianChart>

我在OffsetChanged事件期间使用此功能对其进行自动缩放:

private void ScaleChartDynamically()
{
    var viewPortWidth = chartMainCandleChart.PlotAreaClip.Width;
    var xPixelsPerElem = viewPortWidth * chartMainCandleChart.Zoom.Width / SeriesCollection.Count;
    var notVisible = -chartMainCandleChart.PanOffset.X / xPixelsPerElem; // how many candles are invisible completely (on the left of viewport)
    var visible = viewPortWidth / xPixelsPerElem; // last partially visible in viewport
    var firstElem = (int)Math.Floor(notVisible) + 1; // index is zero based
    var lastElem = (int)Math.Ceiling(notVisible + visible);
    var noElements = lastElem - firstElem + 1;

    var visibleRange = SeriesCollection.Range(firstElem, noElements).SelectMany(ohlc => new[] { ohlc.Low, ohlc.High }).Select(hl => hl.ToDouble()).ToArray();
    if (visibleRange.Any())
    {
        var yAxis = (LinearAxis)chartMainCandleChart.VerticalAxis;
        yAxis.Minimum = visibleRange.Min();
        yAxis.Maximum = visibleRange.Max();
    }
}

沿x轴拖动时遇到以下问题:

chart bug

问题似乎已在此处解决: https://www.telerik.com/forums/fixed-width-height-for-axis 但是提供的解决方案根本无法解决问题。

我暂时在LeftMouseButtonClick上使用一次占用PlotAreaClip.Width的变通办法,以防止拖动时“跳”:

private double _plotAreaClipWidth;

private void chartMainCandleChart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    _plotAreaClipWidth = chartMainCandleChart.PlotAreaClip.Width;
}

ScaleChartDynamically()函数的第一行现在是:

var viewPortWidth = _plotAreaClipWidth;

因此请澄清一下:

我想删除下图中可见的间隙,该间隙在变化,每当PlotAreaClip.Width不同时,该图表就会“跳转”(再次引发事件)。换句话说,我想强制使用PlotAreaClip.Width(x轴的长度)的大小并保留标签,应该在轴的两端将其切除。为了满足我的一般要求,带有轴的PlotAreaClip应该跨整个控件呈现区域,而没有人为的标签间隙。赏金归给向我展示如何做到这一点的人。通过查看下面的示例,您可以了解我的意思。查看缩放/拖动以适应标签时x轴长度的变化。这是导致调整PlotAreaClip.Width大小并在图表两边创建空白的问题的原因。如果仔细观察,您会发现在y轴上也可以看到相同的问题:

gap

zoom x-axis resize

0 个答案:

没有答案