santosh singh
santosh singh

Reputation: 28672

Draw vertical lines in wpf

I have written following code for generating lines on canvas

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]);
    }
}

enter image description here

But I want to draw lines as below.How I can rotatle the canvas to achieve the desired output enter image description here

Upvotes: 2

Views: 5877

Answers (3)

paparazzo
paparazzo

Reputation: 45106

x = 0 and y = 0 is upper left corner (not lower left) so y is like up side down

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]);
    }
}

Upvotes: 1

Luke Marlin
Luke Marlin

Reputation: 1398

If you want to rotate the canvas, you can simply apply a transform on it:

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

If you want to do as John Willemse suggested change your code to this :

X1 = i * scale,
Y1 = bottomMargin,
X2 = i * scale,
Y2 = i * scale,

Upvotes: 1

mlemay
mlemay

Reputation: 1637

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

or by code:

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

Something like this?

Upvotes: 1

Related Questions