(四)WPF使用 Polyline 元素動態繪製折線 (新增X軸時間標籤)
前端程式碼MainWindow.xaml.cs程式碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace 使用_Polyline_元素來繪製折線
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
private int currentSecond = 0;
private double xGrap = 100;//x座標的間隔
private int xPagePoint = 5;//每一頁要顯示多少個數據點
Random rd = new Random();
private DispatcherTimer dispatcherTimer = new DispatcherTimer();
//TextBlock[] myTextBlock = new TextBlock[5];//建立5個X軸的文字
TextBlock myTextBlock1 = new TextBlock();
TextBlock myTextBlock2 = new TextBlock();
TextBlock myTextBlock3 = new TextBlock();
TextBlock myTextBlock4= new TextBlock();
TextBlock myTextBlock5 = new TextBlock();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(1000);//1秒鐘產生一次中斷
dispatcherTimer.Tick += timer_Tick;//中斷入口函式
dispatcherTimer.IsEnabled = true;//開啟中斷
//myTextBlock.FontSize = 18;
//myTextBlock.FontWeight = FontWeights.Bold;
//myTextBlock.FontStyle = FontStyles.Italic;
//myTextBlock1.Text = "0";
//myTextBlock2.Text = "0";
//myTextBlock3.Text = "0";
//myTextBlock4.Text = "0";
//myTextBlock5.Text = "0";
myTextBlock1.Margin = new Thickness(10 + xGrap*1 - 20, 220, 0, 0);
myTextBlock2.Margin = new Thickness(10 + xGrap*2 - 20, 220, 0, 0);
myTextBlock3.Margin = new Thickness(10 + xGrap*3 - 20, 220, 0, 0);
myTextBlock4.Margin = new Thickness(10 + xGrap*4 - 20, 220, 0, 0);
myTextBlock5.Margin = new Thickness(10 + xGrap*5 - 20, 220, 0, 0);
_myGrid.Children.Add(myTextBlock1);
_myGrid.Children.Add(myTextBlock2);
_myGrid.Children.Add(myTextBlock3);
_myGrid.Children.Add(myTextBlock4);
_myGrid.Children.Add(myTextBlock5);
}
private void timer_Tick(object sender, EventArgs e)
{
DateTime _cur_date = DateTime.Now; ;
DateTime date1;
currentSecond++;
double x = currentSecond * xGrap;
double y = rd.Next(1,200);
//myTextBlock1.Text = (currentSecond-5).ToString();
//myTextBlock2.Text = (currentSecond-4).ToString();
//myTextBlock3.Text = (currentSecond-3).ToString();
//myTextBlock4.Text = (currentSecond-2).ToString();
//myTextBlock5.Text = (currentSecond-1).ToString();
//myTextBlock1.Text = date1.ToString("yyyy/MM/dd HH:mm:ss");;
date1 = _cur_date.AddSeconds(-5);
myTextBlock1.Text = date1.ToString("HH:mm:ss");
date1 = _cur_date.AddSeconds(-4);
myTextBlock2.Text = date1.ToString("HH:mm:ss");
date1 = _cur_date.AddSeconds(-3);
myTextBlock3.Text = date1.ToString("HH:mm:ss");
date1 = _cur_date.AddSeconds(-2);
myTextBlock4.Text = date1.ToString("HH:mm:ss");
date1 = _cur_date.AddSeconds(-1);
myTextBlock5.Text = date1.ToString("HH:mm:ss");
var point = new Point(x, y);
_myPolyline.Points.Add(point);//新增新的資料點
if (currentSecond > xPagePoint)
{
_myPolyline.Points.RemoveAt(0);
_myCanvas.Margin = new Thickness(-1*(xGrap * currentSecond)+ xPagePoint * xGrap, 0, 0, 0);
}
}
}
}
MainWindow.xaml程式碼如下:
<Window x:Class="使用_Polyline_元素來繪製折線.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:使用_Polyline_元素來繪製折線"
mc:Ignorable="d"
Title="MainWindow" Height="413.672" Width="800">
<Grid Name="_myGrid" Margin="10,10,10,10" >
<Border Style="{StaticResource MyGridBorderStyle}" Height="200" Margin="10,10,0,89" Width="600">
<Canvas Name="_myCanvas" Margin="0,0,0,0" >
<Polyline Name="_myPolyline" Stroke="Red" StrokeThickness="1" />
</Canvas>
</Border>
<Button Content="開始" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Top" />
</Grid>
</Window>
App.xaml的程式碼如下:
<Application x:Class="使用_Polyline_元素來繪製折線.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:使用_Polyline_元素來繪製折線"
StartupUri="MainWindow.xaml">
<Application.Resources>
<DrawingBrush x:Key="MyGridBrushResource" Viewport="0,0,100,50" ViewportUnits ="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="White">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" Brush="#CCCCFF" />
<GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="#CCCCFF" />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<Style x:Key="MyGridBorderStyle">
<Setter Property="Border.Background" Value="{StaticResource MyGridBrushResource}"/>
<Setter Property="Border.HorizontalAlignment" Value="Left"/>
<Setter Property="Border.VerticalAlignment" Value="Top"/>
<Setter Property="Border.BorderBrush" Value="Black"/>
<Setter Property="Border.BorderThickness" Value="1"/>
</Style>
</Application.Resources>
</Application>