Advertisement
Guest User

OxyPlot Selectable Column Chart

a guest
Feb 10th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.14 KB | None | 0 0
  1.     /// <summary>
  2.     /// SelectedColumnChanged event delegate.
  3.     /// </summary>
  4.     /// <param name="sender">Object triggering the event.</param>
  5.     /// <param name="e">Event arguments.</param>
  6.     public delegate void SelectedColumnChangedEventHandler(object sender, SelectedColumnChangedEventArgs e);
  7.  
  8.     /// <summary>
  9.     /// SelectedColumnChanged event arguments.  Contains the old and the newly
  10.     /// selected column.
  11.     /// </summary>
  12.     public class SelectedColumnChangedEventArgs : EventArgs
  13.     {
  14.         /// <summary>
  15.         /// Old selected column.
  16.         /// </summary>
  17.         public ColumnItem OldColumn { get; private set; }
  18.  
  19.         /// <summary>
  20.         /// New selected column.
  21.         /// </summary>
  22.         public ColumnItem NewColumn { get; private set; }
  23.  
  24.         /// <summary>
  25.         /// Constructor.
  26.         /// </summary>
  27.         /// <param name="oldColumn">Old selected column.</param>
  28.         /// <param name="newColumn">New selected column.</param>
  29.         public SelectedColumnChangedEventArgs(ColumnItem oldColumn, ColumnItem newColumn)
  30.         {
  31.             OldColumn = oldColumn;
  32.             NewColumn = newColumn;
  33.         }
  34.  
  35.     }
  36.  
  37.     /// <summary>
  38.     /// Extended OxyPlot ColumnSeries class.  Allows selection of columns and tracks which
  39.     /// column has been selected.
  40.     /// </summary>
  41.     public class SelectableColumnSeries : ColumnSeries, INotifyPropertyChanged
  42.     {
  43.         /// <summary>
  44.         /// Selected ColumnItem field.
  45.         /// </summary>
  46.         private ColumnItem _selectedColumn;
  47.  
  48.         /// <summary>
  49.         /// Selected ColumnItem property.
  50.         /// </summary>
  51.         public ColumnItem SelectedColumn
  52.         {
  53.             get { return _selectedColumn; }
  54.             set
  55.             {
  56.                 if (_selectedColumn != value)
  57.                 {
  58.                     var changeArgs = new SelectedColumnChangedEventArgs(_selectedColumn, value);
  59.                     _selectedColumn = value;
  60.                     NotifyPropertyChanged();
  61.                     NotifySelectedColumnChanged(changeArgs);
  62.                 }
  63.             }
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Selected ColumnItem color field.
  68.         /// </summary>
  69.         private OxyColor _selectedColumnColor;
  70.  
  71.         /// <summary>
  72.         /// Selected ColumnItem color property.
  73.         /// </summary>
  74.         public OxyColor SelectedColumnColor
  75.         {
  76.             get { return _selectedColumnColor; }
  77.             set
  78.             {
  79.                 _selectedColumnColor = value;
  80.                 NotifyPropertyChanged();
  81.             }
  82.         }
  83.  
  84.         /// <summary>
  85.         /// SelectableColumnSeries constructor.
  86.         /// </summary>
  87.         public SelectableColumnSeries()
  88.         {
  89.             MouseDown += SelectableColumnSeries_MouseDown;
  90.         }
  91.  
  92.         /// <summary>
  93.         /// MouseDown event handler for the SelectableColumnSeries.  Handles changing of the selected
  94.         /// ColumnItem, as well as its color, if a SelectedColumnColor was provided.  If the color for
  95.         /// selection was not provided, then no visual feedback is provided, but the selection is still
  96.         /// made.
  97.         /// </summary>
  98.         /// <param name="sender">Event sender (object).</param>
  99.         /// <param name="e">Event arguments (OxyMouseDownEventArgs).</param>
  100.         protected void SelectableColumnSeries_MouseDown(object sender, OxyMouseDownEventArgs e)
  101.         {
  102.             var nearest = GetNearestPoint(e.Position, false);
  103.             var column = nearest.Item as ColumnItem;
  104.             if (column == null)
  105.             {
  106.                 return;
  107.             }
  108.  
  109.             if (SelectedColumn != null)
  110.             {
  111.                 SelectedColumn.Color = FillColor;
  112.             }
  113.  
  114.             if (!SelectedColumnColor.IsUndefined())
  115.             {
  116.                 column.Color = SelectedColumnColor;
  117.             }
  118.  
  119.             SelectedColumn = column;
  120.         }
  121.  
  122.         /// <summary>
  123.         /// PropertyChanged event handler.
  124.         /// </summary>
  125.         public event PropertyChangedEventHandler PropertyChanged;
  126.  
  127.         /// <summary>
  128.         /// PropertyChanged event handling method.
  129.         /// </summary>
  130.         /// <param name="propertyName">Property name.  If not passed, name of the property,
  131.         /// which called this method, is used.</param>
  132.         private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
  133.         {
  134.             if (PlotModel != null)
  135.             {
  136.                 PlotModel.InvalidatePlot(true);
  137.             }
  138.  
  139.             if (PropertyChanged != null)
  140.             {
  141.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  142.             }
  143.         }
  144.  
  145.         /// <summary>
  146.         /// SelectedColumnChanged event handler.
  147.         /// </summary>
  148.         public event SelectedColumnChangedEventHandler SelectedColumnChanged;
  149.  
  150.         /// <summary>
  151.         /// SelectedColumnChanged handling method.
  152.         /// </summary>
  153.         /// <param name="e">Event arguments.  Includes old column and the newly selected column.</param>
  154.         protected virtual void NotifySelectedColumnChanged(SelectedColumnChangedEventArgs e)
  155.         {
  156.             if (SelectedColumnChanged != null)
  157.             {
  158.                 SelectedColumnChanged(this, e);
  159.             }
  160.         }
  161.     }
  162.  
  163.     /// <summary>
  164.     /// Column graph view model.
  165.     /// </summary>
  166.     public class ColumnGraphViewModel : ViewModelBase
  167.     {
  168.         /// <summary>
  169.         /// Graph item observable collection field.
  170.         /// </summary>
  171.         private ObservableCollection<ExtendedColumnItem> _items;
  172.         /// <summary>
  173.         /// Graph item observable collection property.
  174.         /// </summary>
  175.         public ObservableCollection<ExtendedColumnItem> Items
  176.         {
  177.             get { return _items; }
  178.             set { Set(() => Items, ref _items, value); }
  179.         }
  180.  
  181.         /// <summary>
  182.         /// Plot model field.
  183.         /// </summary>
  184.         private PlotModel _model;
  185.         /// <summary>
  186.         /// Plot model property.
  187.         /// </summary>
  188.         public PlotModel Model
  189.         {
  190.             get { return _model; }
  191.             set { Set(() => Model, ref _model, value); }
  192.         }
  193.  
  194.         /// <summary>
  195.         /// Linear axis field.  This is the axis on the left side, which
  196.         /// provides a reference for numerical values.
  197.         /// </summary>
  198.         private LinearAxis _valueAxis;
  199.         /// <summary>
  200.         /// Linear axis property.  This is the axis on the left side, which
  201.         /// provides a reference for numerical values.
  202.         /// </summary>
  203.         public LinearAxis ValueAxis
  204.         {
  205.             get { return _valueAxis; }
  206.             set { Set(() => ValueAxis, ref _valueAxis, value); }
  207.         }
  208.  
  209.         /// <summary>
  210.         /// Category axis field.  This is the axis on the bottom, which
  211.         /// provides categories for the columns.
  212.         /// </summary>
  213.         private CategoryAxis _bottomCategoryAxis;
  214.         /// <summary>
  215.         /// Category axis property.  This is the axis on the bottom, which
  216.         /// provides categories for the columns.
  217.         /// </summary>
  218.         public CategoryAxis BottomCategoryAxis
  219.         {
  220.             get { return _bottomCategoryAxis; }
  221.             set { Set(() => BottomCategoryAxis, ref _bottomCategoryAxis, value); }
  222.         }
  223.  
  224.         /// <summary>
  225.         /// Column series field.
  226.         /// </summary>
  227.         private SelectableColumnSeries _series;
  228.         /// <summary>
  229.         /// Column series property.
  230.         /// </summary>
  231.         public SelectableColumnSeries Series
  232.         {
  233.             get { return _series; }
  234.             set { Set(() => Series, ref _series, value); }
  235.         }
  236.  
  237.         /// <summary>
  238.         /// Default constructor.
  239.         /// </summary>
  240.         public ColumnGraphViewModel(List<ExtendedColumnItem> items,
  241.             string graphTitle = "", string valueAxisTitle = "", string categoryAxisTitle = "")
  242.         {
  243.             Initialize(items, graphTitle, valueAxisTitle, categoryAxisTitle);
  244.         }
  245.  
  246.         /// <summary>
  247.         /// Initializes all of the properties.  May be overridden.
  248.         /// </summary>
  249.         private void Initialize(List<ExtendedColumnItem> items,
  250.             string graphTitle = "", string valueAxisTitle = "", string categoryAxisTitle = "")
  251.         {
  252.             Items = new ObservableCollection<ExtendedColumnItem>(items);
  253.  
  254.             Model = new PlotModel
  255.             {
  256.                 Title = graphTitle,
  257.                 TitleColor = OxyColor.FromArgb(200, 0, 0, 0),
  258.                 IsLegendVisible = false
  259.             };
  260.  
  261.             BottomCategoryAxis = new CategoryAxis
  262.             {
  263.                 Title = categoryAxisTitle,
  264.                 AxisTitleDistance = 10,
  265.                 TitleFontSize = 13,
  266.                 ItemsSource = Items,
  267.                 LabelField = "Label",
  268.                 IsZoomEnabled = false,
  269.                 IsPanEnabled = false,
  270.                 GapWidth = .2
  271.  
  272.             };
  273.             Model.Axes.Add(BottomCategoryAxis);
  274.  
  275.             ValueAxis = new LinearAxis
  276.             {
  277.                 Title = valueAxisTitle,
  278.                 AxisTitleDistance = 10,
  279.                 TitleFontSize = 13,
  280.                 Position = AxisPosition.Left,
  281.                 MinimumPadding = 0,
  282.                 AbsoluteMinimum = 0,
  283.                 IsZoomEnabled = false,
  284.                 IsPanEnabled = false
  285.             };
  286.             Model.Axes.Add(ValueAxis);
  287.  
  288.             Series = new SelectableColumnSeries
  289.             {
  290.                 ValueField = "Value",
  291.                 StrokeThickness = 1,
  292.                 LabelPlacement = LabelPlacement.Inside,
  293.                 LabelFormatString = "{0}",
  294.                 TextColor = OxyColor.FromArgb(255, 255, 255, 255),
  295.                 FillColor = OxyColor.FromArgb(200, 0, 121, 211),
  296.                 SelectedColumnColor = OxyColor.FromArgb(200, 76, 179, 255),
  297.                 TrackerFormatString = "{1}: {Value}"
  298.             };
  299.             Series.Items.AddRange(Items);
  300.             Model.Series.Add(Series);
  301.         }
  302.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement