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

如何在WPF中添加chart控件,绘制X,Y轴及动态曲线,第1张

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

添加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.DataVisualizationassembly=System.Windows.Controls.DataVisualization.Toolkit" 

        xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Chartingassembly=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>

WPF很强大,但是当WPF的image控件遇到gif时就只读了图片的第一帧,很好很强大!

WPF不屑于gif的简单动画!

但是这对程序员来说不大爽啊!急得我眼泪都下来了!

幸好WPF里有MediaElement这个东西,它是对MediaPlyer的一个封装,果然很强大啊。不过另我不爽的是我这里有N个gif图片就要有N个MediaElement,要了亲命了。

还是不好,如果你能想到用WebBrowseControl来实现,或者用Frame来实现,恭喜你,你太有才了!

我还是不想这么去做,才分不够啊!

重写一下WPF的image,good idea!

public class GIFImageControl : System.Windows.Controls.Image

{

delegate void OnFrameChangedDelegate()

private Bitmap m_Bitmap

public string Path { getset}

BitmapSource bitmapSource

public void AnimatedImageControl(string path)

{

Path = path

m_Bitmap = (Bitmap)Image.FromFile(path)

Width = m_Bitmap.Width

Height = m_Bitmap.Height

ImageAnimator.Animate(m_Bitmap, OnFrameChanged)

bitmapSource = GetBitmapSource()

Source = bitmapSource

}

private void OnFrameChanged(object sender, EventArgs e)

{

Dispatcher.BeginInvoke(DispatcherPriority.Normal,

new OnFrameChangedDelegate(OnFrameChangedInMainThread))

}

private void OnFrameChangedInMainThread()

{

ImageAnimator.UpdateFrames()

if (bitmapSource != null)

bitmapSource.Freeze()

bitmapSource = GetBitmapSource()

Source = bitmapSource

InvalidateVisual()

}

//private static bool loaded

private BitmapSource GetBitmapSource()

{

IntPtr inptr = m_Bitmap.GetHbitmap()

bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(

inptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())

DeleteObject(inptr)

return bitmapSource

}

[DllImport("gdi32")]

static extern int DeleteObject(IntPtr o)

}

ok,用window原有的东西去绑定到wpf上去。很好吧!是不是也比较有才呢?

来自: http://hi.baidu.com/mych/blog/item/1eb14f545f12a752564e00be.html


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11540884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存