Skip to main content
 首页 » 编程设计

c#之缩放时线条消失(使用 "Dynamic data display")

2024年02月20日61qlqwjy

在我的 WPF 项目中,我使用 Dynamic data display用于制图。 我在某处读到,如果放大得非常深,线条会消失(未绘制)是一个常见的错误。

有人解决这个问题吗?

请您参考如下方法:

虽然不是您的错误的确切解决方案,但您可以通过使用缩放限制将用户放大得太深来阻止用户。这样,用户将永远不会通过放大那么深来体验这个错误。您可以找到发生此错误的缩放比例,并限制该 DataRect 的缩放比例。我编写了自己的类来限制 D3 中的缩放,并将为您提供。

这里:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
 
namespace Microsoft.Research.DynamicDataDisplay.ViewportRestrictions { 
/// <summary> 
/// Represents a restriction, which limits the maximal size of <see cref="Viewport"/>'s Visible property. 
/// </summary> 
public class ZoomInRestriction : ViewportRestriction { 
    /// <summary> 
    /// Initializes a new instance of the <see cref="MaximalSizeRestriction"/> class. 
    /// </summary> 
    public ZoomInRestriction() { } 
 
    /// <summary> 
    /// Initializes a new instance of the <see cref="MaximalSizeRestriction"/> class with the given maximal size of Viewport's Visible. 
    /// </summary> 
    /// <param name="maxSize">Maximal size of Viewport's Visible.</param> 
    public ZoomInRestriction(double height, double width) { 
        Height = height; 
        Width = width; 
    } 
 
    private double height; 
    private double width; 
 
    public double Height { 
        get { return height; } 
        set { 
            if (height != value) { 
                height = value; 
                RaiseChanged(); 
            } 
        } 
    } 
 
    public double Width { 
        get { return width; } 
        set { 
            if (width != value) { 
                width = value; 
                RaiseChanged(); 
            } 
        } 
    } 
 
    /// <summary> 
    /// Applies the specified old data rect. 
    /// </summary> 
    /// <param name="oldDataRect">The old data rect.</param> 
    /// <param name="newDataRect">The new data rect.</param> 
    /// <param name="viewport">The viewport.</param> 
    /// <returns></returns> 
    public override DataRect Apply(DataRect oldDataRect, DataRect newDataRect, Viewport2D viewport) { 
        if ((newDataRect.Width < width || newDataRect.Height < height)) { 
            return oldDataRect; 
        } 
        return newDataRect; 
    } 
} 
} 

现在您可以在代码中使用它来实现这样的限制:

plotter.Viewport.Restrictions.Add(new ZoomInRestriction(height, width)); 

虽然不是该错误的确切解决方案,但您至少应该能够防止用户遇到它。