WPF:WPF繪制曲線

簡(jiǎn)述

??WPF開發(fā)中經(jīng)常需要繪制曲線、直方圖等。雖然WPF自帶了繪制圖形等基礎(chǔ)功能,但做程序一個(gè)很基礎(chǔ)的原則就是避免重復(fù)造輪子。在GitHub上找到了微軟官方的WPF繪制曲線開源庫(kù):InteractiveDataDisplay.WPF。
我使用的IDE是VS201x,建議使用NuGet安裝--引用InteractiveDataDisplay.WPF。如何使用NuGet,請(qǐng)自行百度。
以下是我實(shí)驗(yàn)的該開源庫(kù)繪制的WPF曲線程序。

創(chuàng)新互聯(lián)專注于保亭黎族企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),成都做商城網(wǎng)站。保亭黎族網(wǎng)站建設(shè)公司,為保亭黎族等地區(qū)提供建站服務(wù)。全流程按需策劃,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

代碼

MainWindow.xaml

<Window x:Class="WpfDrawPlot.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d3="clr-namespace:InteractiveDataDisplay.WPF;assembly=InteractiveDataDisplay.WPF"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- BarGraph -->
        <d3:Chart Grid.Row="0">
            <d3:Chart.Title>
                <TextBlock Text="WPF Bar Chart" HorizontalAlignment="Center" FontSize="18" Margin="0, 5"/>
            </d3:Chart.Title>
            <d3:BarGraph x:Name="BarChart" Description="BarChart" Stroke="Red" StrokeThickness="1"/>
        </d3:Chart>

        <!-- LineGraph -->
        <d3:Chart x:Name="LinePlot" Grid.Row="1">
            <d3:Chart.Title>
                <TextBlock Text="WPF Line Chart" HorizontalAlignment="Center" FontSize="18" Margin="0, 5"/>
            </d3:Chart.Title>
            <d3:LineGraph x:Name="LineChart" Description="LineChart" Stroke="Green" StrokeThickness="1"/>
        </d3:Chart>

    </Grid>
</Window>

MainWindow.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.Threading;
using InteractiveDataDisplay.WPF;

namespace WpfDrawPlot
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //線程中更新曲線
            Thread threadTmp = new Thread( UpdateChart );
            threadTmp.Start();
        }

        private void UpdateChart()
        {
            int nPointNum = 100;
            Random randm = new Random();
            double[] dArray = new double[ nPointNum ];
            double[] dX = new double[ nPointNum ];
            double[] dY = new double[ nPointNum ];
            double dRandomtTmp = 0;

            while( true )
            {
                Thread.Sleep( 1000 );//每秒刷新一次
                for ( int n = 0; n < dArray.Length; n++ )
                {
                    dRandomtTmp = randm.NextDouble();
                    dArray[ n ] = ( dRandomtTmp < 0.5 ) ? -dRandomtTmp * dArray.Length : dRandomtTmp * dArray.Length;
                }
                for ( int n = 0; n < dX.Length; n++ )
                {
                    dX[ n ] = n;
                    dY[ n ] = randm.Next( dX.Length );
                }

                //更新UI
                Dispatcher.Invoke( new Action( delegate
                {
                    this.BarChart.PlotBars( dArray );
                    this.LineChart.Plot( dX, dY );
                } ) );
            }
        }
    }
}

效果

WPF:WPF繪制曲線

網(wǎng)站題目:WPF:WPF繪制曲線
網(wǎng)頁(yè)網(wǎng)址:http://bm7419.com/article48/geehep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、服務(wù)器托管定制網(wǎng)站、網(wǎng)站內(nèi)鏈、微信公眾號(hào)、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開發(fā)