简体   繁体   English

不使用画布C#绘制线条的最佳方法

[英]Best way to draw a line without using a canvas C#

I am wondering what the best way to draw a line, on a maximum value, from a list is without using a canvas? 我想知道从列表中以最大值绘制一条线的最佳方法是不使用画布吗?

I have identified the Max, Min and Median I'm wondering what the best way to draw a line/point without using a canvas would be? 我已经确定了最大,最小和中位数,我想知道在不使用画布的情况下绘制线/点的最佳方法是什么?

public partial class SpectrumControl : UserControl
{
    private double Highest;
    private double Minimum;
    private double Median;
    private int Total;
    private int CellWidth;

    public int Width { get; set; }

    public SpectrumControl()
    {

        InitializeComponent();
    }

    public void Bind(KLayer klayer)
    {
        if (Width == 0)
        {
            Width = 300;
        }

        Highest = klayer.Values.Max();
        Minimum = klayer.Values.Min();
        Median = ((Highest - Minimum) / 2) + Minimum;
        Total = klayer.Values.Count;
        CellWidth = Width / Total;
        int rowNumber = 0;
        foreach (var item in klayer.Values)
        {
            var label = CreateLabel(item, rowNumber);
            Color backgroundColour = GetColour(item);
            stk1.Children.Add(label);
            rowNumber++;
        }
    }

    private Label CreateLabel(double item, int rowNumber)
    {

        var label = new Label()
        {
            Background = new SolidColorBrush(GetColour(item)),
            Width = CellWidth
        };
        return label;

    }

    private Color GetColour(double item)
    {
        byte a = Convert.ToByte(GetTransparency(item)*255);
        Color backgroundColour;
        if (item < Median)
        {
            backgroundColour = Color.FromArgb(a, 128, 128, 255);
        }
        else if (item > Median)
        {
            backgroundColour = Color.FromArgb(a, 255, 128, 128);
        }
        else
        {
            backgroundColour = Colors.White;
        }

        return backgroundColour;
    }

    private double GetTransparency(double item)
    {
        double x = Highest - Minimum;
        double difference;
        if (item > Median)
        {
            difference = item - Median;
        }
        else
        {
            difference = Median - item;
        }

        var fraction = difference / x;
        return fraction;
    }
}

Well, assuming you are going to use something like GridPanel or any other panel, really, you could do this: 好吧,假设您将要使用诸如GridPanel类的GridPanel或其他面板,实际上,您可以这样做:

var line = new Line();
line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
line.X1 = 1;
line.X2 = 50;
line.Y1 = 1;
line.Y2 = 50;
line.HorizontalAlignment = HorizontalAlignment.Left;
line.VerticalAlignment = VerticalAlignment.Center;
line.StrokeThickness = 2;
grid.Children.Add(line);

Same thing may be achieved in XAML, but it looks like you prefer to work in code-behind, so that is what I posted here. 在XAML中也可以实现相同的目的,但是您似乎更喜欢在后台代码中工作,所以这就是我在此处发布的内容。

Reference: https://msdn.microsoft.com/en-us/library/system.windows.shapes.line%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 参考: https : //msdn.microsoft.com/zh-cn/library/system.windows.shapes.line%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

I'm not sure why you'd avoid canvas, though (well, why someone told you to do that). 不过,我不确定您为什么要避免使用画布(嗯,为什么有人告诉您这样做)。 I've created plenty of plots using canvas. 我已经使用画布创建了很多图。

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

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