Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// SelectedColumnChanged event delegate.
- /// </summary>
- /// <param name="sender">Object triggering the event.</param>
- /// <param name="e">Event arguments.</param>
- public delegate void SelectedColumnChangedEventHandler(object sender, SelectedColumnChangedEventArgs e);
- /// <summary>
- /// SelectedColumnChanged event arguments. Contains the old and the newly
- /// selected column.
- /// </summary>
- public class SelectedColumnChangedEventArgs : EventArgs
- {
- /// <summary>
- /// Old selected column.
- /// </summary>
- public ColumnItem OldColumn { get; private set; }
- /// <summary>
- /// New selected column.
- /// </summary>
- public ColumnItem NewColumn { get; private set; }
- /// <summary>
- /// Constructor.
- /// </summary>
- /// <param name="oldColumn">Old selected column.</param>
- /// <param name="newColumn">New selected column.</param>
- public SelectedColumnChangedEventArgs(ColumnItem oldColumn, ColumnItem newColumn)
- {
- OldColumn = oldColumn;
- NewColumn = newColumn;
- }
- }
- /// <summary>
- /// Extended OxyPlot ColumnSeries class. Allows selection of columns and tracks which
- /// column has been selected.
- /// </summary>
- public class SelectableColumnSeries : ColumnSeries, INotifyPropertyChanged
- {
- /// <summary>
- /// Selected ColumnItem field.
- /// </summary>
- private ColumnItem _selectedColumn;
- /// <summary>
- /// Selected ColumnItem property.
- /// </summary>
- public ColumnItem SelectedColumn
- {
- get { return _selectedColumn; }
- set
- {
- if (_selectedColumn != value)
- {
- var changeArgs = new SelectedColumnChangedEventArgs(_selectedColumn, value);
- _selectedColumn = value;
- NotifyPropertyChanged();
- NotifySelectedColumnChanged(changeArgs);
- }
- }
- }
- /// <summary>
- /// Selected ColumnItem color field.
- /// </summary>
- private OxyColor _selectedColumnColor;
- /// <summary>
- /// Selected ColumnItem color property.
- /// </summary>
- public OxyColor SelectedColumnColor
- {
- get { return _selectedColumnColor; }
- set
- {
- _selectedColumnColor = value;
- NotifyPropertyChanged();
- }
- }
- /// <summary>
- /// SelectableColumnSeries constructor.
- /// </summary>
- public SelectableColumnSeries()
- {
- MouseDown += SelectableColumnSeries_MouseDown;
- }
- /// <summary>
- /// MouseDown event handler for the SelectableColumnSeries. Handles changing of the selected
- /// ColumnItem, as well as its color, if a SelectedColumnColor was provided. If the color for
- /// selection was not provided, then no visual feedback is provided, but the selection is still
- /// made.
- /// </summary>
- /// <param name="sender">Event sender (object).</param>
- /// <param name="e">Event arguments (OxyMouseDownEventArgs).</param>
- protected void SelectableColumnSeries_MouseDown(object sender, OxyMouseDownEventArgs e)
- {
- var nearest = GetNearestPoint(e.Position, false);
- var column = nearest.Item as ColumnItem;
- if (column == null)
- {
- return;
- }
- if (SelectedColumn != null)
- {
- SelectedColumn.Color = FillColor;
- }
- if (!SelectedColumnColor.IsUndefined())
- {
- column.Color = SelectedColumnColor;
- }
- SelectedColumn = column;
- }
- /// <summary>
- /// PropertyChanged event handler.
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>
- /// PropertyChanged event handling method.
- /// </summary>
- /// <param name="propertyName">Property name. If not passed, name of the property,
- /// which called this method, is used.</param>
- private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
- {
- if (PlotModel != null)
- {
- PlotModel.InvalidatePlot(true);
- }
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- /// <summary>
- /// SelectedColumnChanged event handler.
- /// </summary>
- public event SelectedColumnChangedEventHandler SelectedColumnChanged;
- /// <summary>
- /// SelectedColumnChanged handling method.
- /// </summary>
- /// <param name="e">Event arguments. Includes old column and the newly selected column.</param>
- protected virtual void NotifySelectedColumnChanged(SelectedColumnChangedEventArgs e)
- {
- if (SelectedColumnChanged != null)
- {
- SelectedColumnChanged(this, e);
- }
- }
- }
- /// <summary>
- /// Column graph view model.
- /// </summary>
- public class ColumnGraphViewModel : ViewModelBase
- {
- /// <summary>
- /// Graph item observable collection field.
- /// </summary>
- private ObservableCollection<ExtendedColumnItem> _items;
- /// <summary>
- /// Graph item observable collection property.
- /// </summary>
- public ObservableCollection<ExtendedColumnItem> Items
- {
- get { return _items; }
- set { Set(() => Items, ref _items, value); }
- }
- /// <summary>
- /// Plot model field.
- /// </summary>
- private PlotModel _model;
- /// <summary>
- /// Plot model property.
- /// </summary>
- public PlotModel Model
- {
- get { return _model; }
- set { Set(() => Model, ref _model, value); }
- }
- /// <summary>
- /// Linear axis field. This is the axis on the left side, which
- /// provides a reference for numerical values.
- /// </summary>
- private LinearAxis _valueAxis;
- /// <summary>
- /// Linear axis property. This is the axis on the left side, which
- /// provides a reference for numerical values.
- /// </summary>
- public LinearAxis ValueAxis
- {
- get { return _valueAxis; }
- set { Set(() => ValueAxis, ref _valueAxis, value); }
- }
- /// <summary>
- /// Category axis field. This is the axis on the bottom, which
- /// provides categories for the columns.
- /// </summary>
- private CategoryAxis _bottomCategoryAxis;
- /// <summary>
- /// Category axis property. This is the axis on the bottom, which
- /// provides categories for the columns.
- /// </summary>
- public CategoryAxis BottomCategoryAxis
- {
- get { return _bottomCategoryAxis; }
- set { Set(() => BottomCategoryAxis, ref _bottomCategoryAxis, value); }
- }
- /// <summary>
- /// Column series field.
- /// </summary>
- private SelectableColumnSeries _series;
- /// <summary>
- /// Column series property.
- /// </summary>
- public SelectableColumnSeries Series
- {
- get { return _series; }
- set { Set(() => Series, ref _series, value); }
- }
- /// <summary>
- /// Default constructor.
- /// </summary>
- public ColumnGraphViewModel(List<ExtendedColumnItem> items,
- string graphTitle = "", string valueAxisTitle = "", string categoryAxisTitle = "")
- {
- Initialize(items, graphTitle, valueAxisTitle, categoryAxisTitle);
- }
- /// <summary>
- /// Initializes all of the properties. May be overridden.
- /// </summary>
- private void Initialize(List<ExtendedColumnItem> items,
- string graphTitle = "", string valueAxisTitle = "", string categoryAxisTitle = "")
- {
- Items = new ObservableCollection<ExtendedColumnItem>(items);
- Model = new PlotModel
- {
- Title = graphTitle,
- TitleColor = OxyColor.FromArgb(200, 0, 0, 0),
- IsLegendVisible = false
- };
- BottomCategoryAxis = new CategoryAxis
- {
- Title = categoryAxisTitle,
- AxisTitleDistance = 10,
- TitleFontSize = 13,
- ItemsSource = Items,
- LabelField = "Label",
- IsZoomEnabled = false,
- IsPanEnabled = false,
- GapWidth = .2
- };
- Model.Axes.Add(BottomCategoryAxis);
- ValueAxis = new LinearAxis
- {
- Title = valueAxisTitle,
- AxisTitleDistance = 10,
- TitleFontSize = 13,
- Position = AxisPosition.Left,
- MinimumPadding = 0,
- AbsoluteMinimum = 0,
- IsZoomEnabled = false,
- IsPanEnabled = false
- };
- Model.Axes.Add(ValueAxis);
- Series = new SelectableColumnSeries
- {
- ValueField = "Value",
- StrokeThickness = 1,
- LabelPlacement = LabelPlacement.Inside,
- LabelFormatString = "{0}",
- TextColor = OxyColor.FromArgb(255, 255, 255, 255),
- FillColor = OxyColor.FromArgb(200, 0, 121, 211),
- SelectedColumnColor = OxyColor.FromArgb(200, 76, 179, 255),
- TrackerFormatString = "{1}: {Value}"
- };
- Series.Items.AddRange(Items);
- Model.Series.Add(Series);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement