C#上位机软件之——曲线显示

构建曲线显示界面

本设计基于 InteractiveDataDisplay.WPF 数据交互显示插件进行设计
安装方法:工具->NuGet包管理->管理解决方案的NuGet程序包,在浏览界面搜索该程序包,然后下载安装即可。
在文件开头引入这个程序包

 xmlns:d3="clr-namespace:InteractiveDataDisplay.WPF;assembly=InteractiveDataDisplay.WPF"

创建实例并设置相关参数

<d3:Chart Name="plotter" IsHorizontalNavigationEnabled="true" IsVerticalNavigationEnabled="true"  PlotHeight="1000" PlotWidth="300" MouseWheel="plotter_MouseWheel" KeyDown="plotter_KeyDown" MouseMove="plotter_MouseMove" MouseUp="plotter_MouseUp" MouseLeftButtonUp="plotter_MouseLeftButtonUp"  >
            <d3:Chart.Title>
                    <TextBlock HorizontalAlignment="Center"  FontSize="18" VerticalAlignment ="Center" IsEnabled="False" >采样曲线</TextBlock>
            </d3:Chart.Title>
            <d3:Chart.LegendContent>
             <d3:LegendItemsPanel>
                 <d3:LegendItemsPanel.Resources>
                     <DataTemplate x:Key="InteractiveDataDisplay.WPF.LineGraph">
                         <StackPanel Orientation="Horizontal">
                             <CheckBox IsChecked="{Binding Path=Visibility, Converter={StaticResource VisibilityToCheckedConverter}, Mode=TwoWay}"/>
                             <Line Width="15" Height="15" X1="0" Y1="0" X2="15" Y2="15" Stroke="{Binding Path=Stroke}" StrokeThickness="2"/>
                             <TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}"/>
                         </StackPanel>
                     </DataTemplate>
                 </d3:LegendItemsPanel.Resources>
             </d3:LegendItemsPanel>
         </d3:Chart.LegendContent>
         <Grid Name ="Lines"/>
</d3:Chart>

主要参量意义:
IsHorizontalNavigationEnabled 和 IsVerticalNavigationEnabled 分别为横向与纵向导航窜口,当设置为false 则限制显示数据的横向和纵向范围,不能拖动了。
plotHeight 和 plotwidth 则为初始显示时横纵坐标的高度和宽度
MouseWheel 为鼠标滚轮的事件响应函数,该控件有默认的事件响应处理(缩放横轴和纵轴),并且默认不再响应自定义函数,如果想要它响应自己的函数则需要在初始化时调用

plotter.AddHandler(Chart.MouseWheelEvent, new MouseWheelEventHandler(plotter_MouseWheel), true);

重新激活用户事件响应。
同样原因,MouseLeftButtonUpEvent事件也需要做同样处理:

plotter.AddHandler(Chart.MouseLeftButtonUpEvent, new MouseButtonEventHandler(plotter_MouseLeftButtonUp), true);

d3:Chart.Title为图表的标题属性;
d3:Chart.LegendContent为图表标签内容
在这里插入图片描述
该标签的模板为

 <d3:LegendItemsPanel>
     <d3:LegendItemsPanel.Resources>
         <DataTemplate x:Key="InteractiveDataDisplay.WPF.LineGraph">
             <StackPanel Orientation="Horizontal">
                 <CheckBox IsChecked="{Binding Path=Visibility, Converter={StaticResource VisibilityToCheckedConverter}, Mode=TwoWay}"/>
                 <Line Width="15" Height="15" X1="0" Y1="0" X2="15" Y2="15" Stroke="{Binding Path=Stroke}" StrokeThickness="2"/>
                 <TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}"/>
             </StackPanel>
         </DataTemplate>
     </d3:LegendItemsPanel.Resources>
 </d3:LegendItemsPanel>

解析:
表面该DateTemplate为InteractiveDataDisplay.WPF.LineGraph的参数显示打造,所以显示的控件需要连接到LineGraph的对应参数
比如
将CheckBox 的IsCheck属性与LineGraph的是否显示连接,此处需要进行,数据转换,所以在程序中加入转换类

  public class VisibilityToCheckedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((Visibility)value) == Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }
    }

并在文件开头引用该类

 <UserControl.Resources>
    <local:VisibilityToCheckedConverter x:Key="VisibilityToCheckedConverter"/>
</UserControl.Resources>

<Line Width="15" Height="15" X1="0" Y1="0" X2="15" Y2="15" Stroke="{Binding Path=Stroke}" StrokeThickness="2"/> 将颜色绑定
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Description}"/> 将描述绑定

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值