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


    在这里插入图片描述

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

    System.Drawing.dll
    
    System.Windows.Forms.dll
    
    WindowsFormsIntegration.dll
    
    System.Windows.Forms.DataVisualization.dll
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2、xml添加命名空间

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

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

    **ps:Windows窗口 设置 AllowsTransparency="False",才能显示Winfrom Chart控件**
    
    • 1
    <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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

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

    using System.Windows.Forms; 
    using System.Windows.Forms.DataVisualization.Charting;
    
    • 1
    • 2

    5、后台代码:

    a、图表初始化

     #region 初始化图表
    
            private ChartArea AREA = new ChartArea() {
        Name = "Line" };             // 画图区域
            private Legend LEGEND = new Legend();                                   // 一个图例
    
            /// 
            /// 初始化图表
            /// 
            /// 线条数
            private void initChart(int lineCount)
            {
       
                ChartPlot.Series.Clear();                                           //清除线条
                ChartPlot.ChartAreas.Clear();                                       //清除画图区域
                ChartPlot.Legends.Clear();                                          //清除图例
    
                AREA.AxisX
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    MySQL表的增删改查(进阶)
    Vue3 + Echarts(5.x) 实现中国地图
    Linux服务器编程是一个比较刚需的开发方向
    IBM LinuxONE Community Cloud 免费试用申请教程
    解读 Servlet 源码:GenericServlet,ServletConfig,ServletContext
    FPGA学习
    【Unity】实现轮盘抽奖
    手把手教你使用Tensorflow2.0搭建循环神经网络
    常见数据类型
    17. Letter Combinations of a Phone Number
  • 原文地址:https://blog.csdn.net/BeanGo/article/details/126658748