WPF中使用WinForm Chart控件(1)----实时曲线

本文介绍了如何在WPF应用中使用Windows Forms的Chart控件来显示实时曲线。首先需要添加相关DLL引用和XML命名空间,接着在WPF界面中通过WindowsFormsHost作为宿主容器嵌入Chart控件。然后在后台代码中初始化图表并实时从USB设备获取数据进行更新。

在这里插入图片描述

1、首先添加以下四个dll引用

System.Drawing.dll

System.Windows.Forms.dll

WindowsFormsIntegration.dll

System.Windows.Forms.DataVisualization.dll

2、xml添加命名空间

 xmlns:Wchart="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"

3、WPF是不可以直接引用WinForm Chart,但可以托管在WindowsFormsHost中使用,所以我们要添加一个宿主容器

**ps:Windows窗口 设置 AllowsTransparency="False",才能显示Winfrom Chart控件**
<Grid Margin="4 4 4 2">
  <Grid.ColumnDefinitions>
       <ColumnDefinition/>
       <ColumnDefinition Width="100"/>
   </Grid.ColumnDefinitions>
   <Grid  Margin="5" Background="WhiteSmoke">
       <WindowsFormsHost >
           <Wchart:Chart x:Name="ChartPlot" >
               <!--<Wchart:Chart.ChartAreas>
                       <Wchart:ChartArea>
                           <Wchart:ChartArea.AxisY>
                               <Wchart:Axis></Wchart:Axis>
                           </Wchart:ChartArea.AxisY>
                       </Wchart:ChartArea>
                   </Wchart:Chart.ChartAreas>-->
           </Wchart:Chart>
       </WindowsFormsHost>
   </Grid>
   <StackPanel x:Name="IsEnableChannel" Orientation="Vertical" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center">

   </StackPanel>
</Grid>

4.后台代码.cs添加命名空间

using System.Windows.Forms; 
using System.Windows.Forms.DataVisualization.Charting;

5、后台代码:

a、图表初始化

 #region 初始化图表

        private ChartArea AREA = new ChartArea() {
   
    Name = "Line" };             // 画图区域
        private Legend LEGEND = new Legend();                                   // 一个图例

        /// <summary>
        /// 初始化图表
        /// </summary>
        /// <param name="lineCount">线条数</param>
        private void initChart(int lineCount)
        {
   
   
            ChartPlot.Series.Clear();                                           //清除线条
            ChartPlot.ChartAreas.Clear();                                       //清除画图区域
            ChartPlot.Legends.Clear();                                          //清除图例

            AREA.AxisX.Enabled 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BeanGo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值