在使用WPF画横纵坐标图,可以使用本身的Canvas下实现。默认坐标原点在左上角。能过下面的实现可以实现在左正解,并连续曲线图,不过比不上DynamicDataDisplay的效果美观。
首先在Xaml中定义Canvas。如下:
-
<Border BorderBrush="Black" BorderThickness="1" Height="200" Width="200">
-
<Canvas Height="200" Width="200" x:Name="Canvas1">
-
<Canvas.RenderTransform>
-
<TransformGroup>
-
<ScaleTransform ScaleY="-1" />
-
<TranslateTransform Y="200" />
-
</TransformGroup>
-
</Canvas.RenderTransform> </Canvas>
-
</Border>
设置Canvas是正方形,200长。
在窗体的Page_Load事件中添加画线:
-
private void Window_Loaded(object sender, RoutedEventArgs e)
-
{
-
int[] slices = new int[67];
-
for (int i = 0; i < 67; i++)
-
{
-
slices[i] = i*3;
-
}
-
double[] sens = new double[67];
-
sens[0] = 0;
-
sens[66] = 0;
-
for (int i = 1; i < 66; i++)
-
{
-
if (i % 2 == 0)
-
{
-
sens[i] = 119.8 + 20;
-
}
-
else
-
{
-
sens[i] = 119.8 - 20;
-
}
-
}
-
for (int i = 0; i < 66; i++)
-
{
-
Line line = new Line();
-
line.X1 = slices[i];
-
line.Y1 = sens[i];
-
line.X2 = slices[i + 1]+1;
-
line.Y2 = sens[i + 1];
-
line.Stroke = System.Windows.Media.Brushes.Black;
-
line.StrokeThickness = 1;
-
this.Canvas1.Children.Add(line);
-
}
-
}
其中:line.Stroke = System.Windows.Media.Brushes.Black;很重要,如果没有这行,画出的线条都是白色的。
line.StrokeThickness = 1;设置线条的粗细。
效果图如下:
阅读(9282) | 评论(0) | 转发(0) |