Click here to Skip to main content
15,886,823 members
Articles / Desktop Programming / WPF

WPF Charting using MVVM Pattern

Rate me:
Please Sign up or sign in to vote.
4.95/5 (15 votes)
19 Oct 2010CPOL8 min read 328.9K   5.4K   72  
Shows how to implement a WPF Chart in .NET 4.0 using MVVM Pattern
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Controls;

namespace WPFCharting 
{
    public class MainWindowVM: INotifyPropertyChanged
    {
        private MainWindowModel _mwm;
        private static MainWindowVM _VM;
        /// <summary>
        /// Gets the singleton instance of this Viewmodel.
        /// </summary>
        /// <returns>This instance of MainWindowVM or Null if not instanciated</returns>
        public static MainWindowVM getInstance()
        {
            return _VM;
        }
        /// <summary>
        /// CTOR for MainWindowVM - View Model for MainWindow
        /// </summary>
        public MainWindowVM ()
        {
            //store this instance;
            _VM = this;
            //create a new moel
            _mwm = new MainWindowModel();
            //the model IS the data, this is a getter only method
            _data = _mwm;
            propChanged("data");
            //set default chart type
            CurrentChartType = "ColumnSeries";
            //default title
            Title = "Welcome to WPFCharting";
            //Default Legend Title
            LegendTitle = "Animals";    
            //Allow user to dynamically change chart types
            SeriesTypes =  new ObservableCollection<string>();
           // MainChart = new ucColumnSeriesChart();
            
        }
        private ObservableCollection<KeyValuePair<string, int>> _data;
        public ObservableCollection<KeyValuePair<string,int>> data {
            get { return _data; }          
        }
        private ContentControl _MainChart;
        public ContentControl MainChart {
            get { return _MainChart; }
            set { 
                _MainChart = value;
                propChanged("MainChart");            
            } 
        }
        private string _CurrentChartType;
        public string CurrentChartType {
            get { return _CurrentChartType; }
            set { 
                _CurrentChartType = value;
                Commands.ShowSeries ss = new Commands.ShowSeries();
                ss.Execute(value);
                propChanged("CurrentChartType");
            }
        }     
        private ObservableCollection<string> _SeriesTypes;
        public ObservableCollection<string> SeriesTypes {
            get { return _SeriesTypes; }
            set {
                Utilities.ForChart fc = new Utilities.ForChart();
                _SeriesTypes = fc.getChartSeriesTypes();
                propChanged("SeriesTypes");              
            }  
        }
        private string _LegendTitle;
        public string LegendTitle { 
            get {return _LegendTitle;} 
            set {
                _LegendTitle = value;
                propChanged("LegendTitle");
            }
        }
        private string _Title;
        /// <summary>
        /// The title to the chart
        /// </summary>
        public string Title { 
            get {return _Title;}
            set { 
                _Title = value;
                propChanged("Title");
            } 
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void propChanged(String propname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
            }
        }
    }
}

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 The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
ex: Mr Javaman

Comments and Discussions