当前位置:   article > 正文

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

winform实时曲线图控件

在这里插入图片描述

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();                                   // 一个图例

        /// <summary>
        /// 初始化图表
        /// </summary>
        /// <param name="lineCount">线条数</param>
        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
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号