WPF自学笔记(三):波形显示窗口(ni.Graph)

  在开发软件时,经常有实时显示数据的需求,所以需要一个波形显示的窗口。最早在使用WPF制作波形显示窗口时,都是用Image控件,直接实时计算一帧帧画图,然后实时刷新。虽然最终可以实现下图的效果,但是总归还是比较复杂。


  后来发现NI(National Instruments)公司开发的有集成好的窗口控件,能很好地应用于WPF里。所以这里记录一下该控件的使用。

1.安装NI Measurement studio

  官网地址在这里【NI Measurement studio】,但是是收费的,网上有一些破解版可以自己找找。
  安装完成后,在VS里就可以看到Measurement studio了。新建一个NI WPF Application 工程。

2.使用Graph控件

  Measurement studio提供了很多仪表控件,具体可以参看介绍手册。这里使用的是ni.Graph控件。xaml代码如下:

<ni:Graph Name="forceGraph" HorizontalAlignment="Left" Height="300" Margin="197,-1,0,0" VerticalAlignment="Top" Width="800">
    <ni:Graph.Axes>
        <ni:AxisDouble x:Name="horizontalAxisForce" Orientation="Horizontal" Range="1, 10000, System.Double" Adjuster="None"/>
        <ni:AxisDouble x:Name="verticalAxisForce" Orientation="Vertical" Range="-50, 50, System.Double" Label="F(N)"  Adjuster="None"/>
    </ni:Graph.Axes>
    <ni:Graph.Plots>
        <ni:Plot x:Name="plotFx" Label="plotFx">
            <ni:LinePlotRenderer Stroke="OrangeRed" StrokeThickness="1"/>
        </ni:Plot>
        <ni:Plot x:Name="plotFy" Label="plotFy">
            <ni:LinePlotRenderer Stroke="Green" StrokeThickness="1"/>
        </ni:Plot>
        <ni:Plot x:Name="plotFz" Label="plotFz">
            <ni:LinePlotRenderer Stroke="Blue" StrokeThickness="1"/>
        </ni:Plot>
    </ni:Graph.Plots>
</ni:Graph>

  这里重点要介绍的是ni:Graph控件的两个属性

  1.ni:Graph.Axes

  坐标轴。设置坐标轴的各种属性,例如范围,名称等

  2.ni:Plot

  曲线。每一个ni:Plot可以在图上显示一根曲线,所以如果要在一个窗口中显示多根曲线,只需要多添加几个ni:Plot。值得一提的是,改变ni:Plot的属性StrokeDashArray,可以将曲线变成虚线,例如:StrokeDashArray=”4,4”,即将曲线设置成线段长为4,间隙为4的虚线。

3.显示波形数据

  在后台代码里,要显示数据直接传递给ni.Graph的DataSource,然后调用Refresh()方法更新下UI即可显示曲线。

this.forceGraph.DataSource = MyData; //绑定数据
this.forceGraph.Refresh();  //更新UI

  这里要注意的是,如果需要显示多根ni:Plot曲线,传入的数据MyData则是一个二维数组,每列代表一根ni:Plot曲线的数据。

4.隐藏曲线

  如果多根ni:Plot曲线一起显示时,可以利用Visibility属性控制每根曲线的显示和关闭。

this.plotFy.Visibility == Visibility.Visible; //显示曲线
this.plotFy.Visibility = Visibility.Hidden; //隐藏曲线

  最终实现的效果如下,点击左侧按钮可以控制每根曲线的显示和关闭。整体使用起来还是比自己做控件更方便。


  此外Measurement studio还有winform的波形控件,使用起来比WPF更简单。是以为记!

猜你喜欢

转载自blog.csdn.net/u011389706/article/details/80377823