WPF:WPF绘制曲线

简述

  WPF开发中经常需要绘制曲线、直方图等。虽然WPF自带了绘制图形等基础功能,但做程序一个很基础的原则就是避免重复造轮子。在GitHub上找到了微软官方的WPF绘制曲线开源库:InteractiveDataDisplay.WPF。
我使用的IDE是VS201x,建议使用NuGet安装--引用InteractiveDataDisplay.WPF。如何使用NuGet,请自行百度。
以下是我实验的该开源库绘制的WPF曲线程序。

创新互联专注于保亭黎族企业网站建设,响应式网站建设,成都做商城网站。保亭黎族网站建设公司,为保亭黎族等地区提供建站服务。全流程按需策划,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

代码

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绘制曲线

网站题目:WPF:WPF绘制曲线
网页网址:https://www.cdcxhl.com/article48/geehep.html

成都网站建设公司_创新互联,为您提供营销型网站建设服务器托管定制网站网站内链微信公众号企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发