Click here to Skip to main content
15,895,799 members
Articles / Operating Systems / Windows

WPF Chart Control With Pan, Zoom and More

Rate me:
Please Sign up or sign in to vote.
4.92/5 (42 votes)
10 Dec 2012Public Domain10 min read 387.8K   10.9K   174  
Chart Control for Microsoft .NET 3.0/WPF with pan, zoom, and offline rendering to the clipboard for custom sizes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interop;
using Microsoft.Windows.Media;
using System.Windows;

namespace WpfD2DSample
{
    class D2DD3DImage: D3DImage, IDisposable
    {
        // Interop
        SurfaceQueueInteropHelper helper;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        protected override System.Windows.Freezable CreateInstanceCore()
        {
            return new D2DD3DImage();
        }

        public void SetPixelSize(uint pixelWidth, uint pixelHeight)
        {
            EnsureHelper();
            helper.SetPixelSize(pixelWidth, pixelHeight);
        }

        public IntPtr HWNDOwner
        {
            get { return (IntPtr)GetValue(HWNDOwnerProperty); }
            set { SetValue(HWNDOwnerProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HWNDOwner.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HWNDOwnerProperty =
            DependencyProperty.Register("HWNDOwner", typeof(IntPtr), typeof(D2DD3DImage), new UIPropertyMetadata(IntPtr.Zero, new PropertyChangedCallback(HWNDOwnerChanged)));

        public Action<IntPtr> OnRender
        {
            get { return (Action<IntPtr>)GetValue(OnRenderProperty); }
            set { SetValue(OnRenderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OnRender.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OnRenderProperty =
            DependencyProperty.Register("OnRender", typeof(Action<IntPtr>), typeof(D2DD3DImage), new UIPropertyMetadata(null, new PropertyChangedCallback(RenderChanged)));

        public void RequestRender()
        {
            EnsureHelper();

            // Don't bother with a call if there's no callback registered.
            if (null != this.OnRender)
            {
                helper.RequestRenderD2D();
            }
        }

        #region Helpers
        private void EnsureHelper()
        {
            if (helper == null)
            {
                helper = new SurfaceQueueInteropHelper();
                helper.HWND = this.HWNDOwner;
                helper.D3DImage = this;
                helper.RenderD2D = this.OnRender;
            }
        }
        #endregion Helpers

        #region Callbacks

        static void HWNDOwnerChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            D2DD3DImage image = sender as D2DD3DImage;

            if (image != null)
            {
                if (image.helper != null)
                {
                    image.helper.HWND = (IntPtr)args.NewValue;
                }
            }
        }

        static void RenderChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            D2DD3DImage image = sender as D2DD3DImage;

            if (image != null)
            {
                if (image.helper != null)
                {
                    image.helper.RenderD2D = (Action<IntPtr>)args.NewValue;
                }
            }
        }

        #endregion Callbacks

        #region IDisposable Members

        public void Dispose()
        {
            if (helper != null)
            {
                helper.Dispose();
                helper = null;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Founder Cheesy Design
Taiwan Taiwan
John graduated from the University of South Australia in 1997 with a Bachelor of Electronic Engineering Degree, and since then he has worked on hardware and software in many fields including Aerospace, Defence, and Medical giving him over 10 of years experience in C++ and C# programming. In 2009 John Started his own contracting company doing business between Taiwan and Australia.

Comments and Discussions