none
如何在WPF中添加chart控件,绘制X,Y轴及动态曲线 RRS feed

  • 问题

  •        我是一个学习WPF编程的新手,对于绘制动态实时曲线遇到了一个问题:想要绘制无限制的坐标轴,X  Y轴,随时绘制获得数据的波形图。对于怎么画坐标,怎样添加相应控件遇到问题,希望得到各位的及时帮助,不胜感谢!
    2014年2月18日 1:59

答案

  • 你好,

    我们可以使用Toolkit Chart 控件去绘制图形

    #WPF Toolkit - February 2010 Release
    http://wpf.codeplex.com/releases/view/40535

    添加System.Windows.Controls.DataVisualization.Toolkit.dll 引用后,XAML中可以以下面的方式使用

    <Window x:Class="WpfChart3._5.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
            xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
            Title="MainWindow" Height="350" Width="525">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Grid.Column="1" Grid.Row="0">
            <charting:Chart x:Name="waitingtasks" BorderBrush="Transparent" BorderThickness="0" VerticalAlignment="Stretch" Title="Waiting Tasks">
                <charting:Chart.TitleStyle>
                    <Style TargetType="datavis:Title">
                        <Setter Property="HorizontalAlignment" Value="Left"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Style>
                </charting:Chart.TitleStyle>
                <charting:Chart.LegendStyle>
                    <Style TargetType="datavis:Legend">
                        <Setter Property="Width" Value="0" />
                    </Style>
                </charting:Chart.LegendStyle>
                <charting:Chart.Axes>
                    <charting:LinearAxis Orientation="X" Title="Time" Interval="20" ShowGridLines="False"/>
                    <charting:LinearAxis Title="Number of Waiting Tasks" Orientation="Y" Interval="5" ShowGridLines="False"/>
                </charting:Chart.Axes>
                <charting:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" 
                                        AnimationSequence="FirstToLast"/>
            </charting:Chart>
        </Grid>
    </Window>

    这里有一个比较详细的英文文章和 Demo: http://www.codeproject.com/Articles/196502/WPF-Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co

    我的测试项目下载: http://sdrv.ms/18qPYej


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年2月19日 4:42
    版主

全部回复

  • 我用的是VS2012,帖子中的操作无法正确照着做
    2014年2月18日 7:13
  • 你好,

    我们可以使用Toolkit Chart 控件去绘制图形

    #WPF Toolkit - February 2010 Release
    http://wpf.codeplex.com/releases/view/40535

    添加System.Windows.Controls.DataVisualization.Toolkit.dll 引用后,XAML中可以以下面的方式使用

    <Window x:Class="WpfChart3._5.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
            xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
            Title="MainWindow" Height="350" Width="525">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10" Grid.Column="1" Grid.Row="0">
            <charting:Chart x:Name="waitingtasks" BorderBrush="Transparent" BorderThickness="0" VerticalAlignment="Stretch" Title="Waiting Tasks">
                <charting:Chart.TitleStyle>
                    <Style TargetType="datavis:Title">
                        <Setter Property="HorizontalAlignment" Value="Left"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Style>
                </charting:Chart.TitleStyle>
                <charting:Chart.LegendStyle>
                    <Style TargetType="datavis:Legend">
                        <Setter Property="Width" Value="0" />
                    </Style>
                </charting:Chart.LegendStyle>
                <charting:Chart.Axes>
                    <charting:LinearAxis Orientation="X" Title="Time" Interval="20" ShowGridLines="False"/>
                    <charting:LinearAxis Title="Number of Waiting Tasks" Orientation="Y" Interval="5" ShowGridLines="False"/>
                </charting:Chart.Axes>
                <charting:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True" 
                                        AnimationSequence="FirstToLast"/>
            </charting:Chart>
        </Grid>
    </Window>

    这里有一个比较详细的英文文章和 Demo: http://www.codeproject.com/Articles/196502/WPF-Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co

    我的测试项目下载: http://sdrv.ms/18qPYej


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年2月19日 4:42
    版主