2

我编写了以下代码用于在画布上生成线条

XAML

<Canvas HorizontalAlignment="Left" 
        x:Name="canvas1" Height="219" 
        Margin="10,10,0,0" Grid.Row="1" 
        VerticalAlignment="Top" Width="365"/>

C#

private void Draw()
{
    canvas1.Children.Clear();
    for (int i = 0; i < data.Length; i++)
    {
        data[i] = i;
        lines[i] = new Line()
        {
            X1 = leftMargin,
            Y1 = i * scale,
            X2 = i * scale,
            Y2 = i * scale,
            StrokeThickness = 2,
            Stroke = new SolidColorBrush(Colors.Black)
        };
        canvas1.Children.Add(lines[i]);
    }
}

在此处输入图像描述

但我想画线如下。如何旋转画布以实现所需的输出 在此处输入图像描述

4

3 回答 3

1
<Canvas.RenderTransform>
    <RotateTransform CenterX="110" CenterY="183" Angle="270" />
</Canvas.RenderTransform>

或通过代码:

Canvas.RenderTransform = new RotateTransform(270, 109.5, 182.5);

像这样的东西?

于 2013-04-04T13:09:29.043 回答
1

如果要旋转画布,只需在其上应用变换即可:

<Canvas.RenderTransform>
  <RotateTransform CenterX="110" CenterY="183" Angle="270" />
</Canvas.RenderTransform>

如果您想按照 John Willemse 的建议将您的代码更改为:

X1 = i * scale,
Y1 = bottomMargin,
X2 = i * scale,
Y2 = i * scale,
于 2013-04-04T13:12:00.913 回答
1

x = 0 和 y = 0 是左上角(不是左下角),所以 y 就像上下

private void Draw()
{
    Line[] lines = new Line[100];
    int scale = 3;
    canvas1.Children.Clear();
    int yStart = 290;
    for (int i = 0; i < lines.Length; i++)
    {
        //data[i] = i;
        lines[i] = new Line()
        {
            X1 = i * scale,
            Y1 = yStart,
            X2 = i * scale,
            Y2 = 300 - (i * scale),
            StrokeThickness = 1,
            Stroke = new SolidColorBrush(Colors.Black)
        };
        canvas1.Children.Add(lines[i]);
    }
}
于 2013-04-04T14:10:08.257 回答