[12503] | 1 | using System;
|
---|
| 2 | using System.Windows;
|
---|
| 3 | using Microsoft.Research.DynamicDataDisplay.DataSources;
|
---|
| 4 | using System.Collections.Generic;
|
---|
| 5 | using System.Collections.ObjectModel;
|
---|
| 6 | using Microsoft.Research.DynamicDataDisplay.Common;
|
---|
| 7 | using Microsoft.Research.DynamicDataDisplay.Charts;
|
---|
| 8 |
|
---|
| 9 | namespace Microsoft.Research.DynamicDataDisplay
|
---|
| 10 | {
|
---|
| 11 | public abstract class PointsGraphBase : ViewportElement2D, IOneDimensionalChart
|
---|
| 12 | {
|
---|
| 13 | /// <summary>
|
---|
| 14 | /// Initializes a new instance of the <see cref="PointsGraphBase"/> class.
|
---|
| 15 | /// </summary>
|
---|
| 16 | protected PointsGraphBase()
|
---|
| 17 | {
|
---|
| 18 | Viewport2D.SetIsContentBoundsHost(this, true);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | #region DataSource
|
---|
| 22 |
|
---|
| 23 | public IPointDataSource DataSource
|
---|
| 24 | {
|
---|
| 25 | get { return (IPointDataSource)GetValue(DataSourceProperty); }
|
---|
| 26 | set { SetValue(DataSourceProperty, value); }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public static readonly DependencyProperty DataSourceProperty =
|
---|
| 30 | DependencyProperty.Register(
|
---|
| 31 | "DataSource",
|
---|
| 32 | typeof(IPointDataSource),
|
---|
| 33 | typeof(PointsGraphBase),
|
---|
| 34 | new FrameworkPropertyMetadata
|
---|
| 35 | {
|
---|
| 36 | AffectsRender = true,
|
---|
| 37 | DefaultValue = null,
|
---|
| 38 | PropertyChangedCallback = OnDataSourceChangedCallback
|
---|
| 39 | }
|
---|
| 40 | );
|
---|
| 41 |
|
---|
| 42 | private static void OnDataSourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
---|
| 43 | {
|
---|
| 44 | PointsGraphBase graph = (PointsGraphBase)d;
|
---|
| 45 | if (e.NewValue != e.OldValue)
|
---|
| 46 | {
|
---|
| 47 | graph.DetachDataSource(e.OldValue as IPointDataSource);
|
---|
| 48 | graph.AttachDataSource(e.NewValue as IPointDataSource);
|
---|
| 49 | }
|
---|
| 50 | graph.OnDataSourceChanged(e);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | private void AttachDataSource(IPointDataSource source)
|
---|
| 54 | {
|
---|
| 55 | if (source != null)
|
---|
| 56 | {
|
---|
| 57 | source.DataChanged += OnDataChanged;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private void DetachDataSource(IPointDataSource source)
|
---|
| 62 | {
|
---|
| 63 | if (source != null)
|
---|
| 64 | {
|
---|
| 65 | source.DataChanged -= OnDataChanged;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | private void OnDataChanged(object sender, EventArgs e)
|
---|
| 70 | {
|
---|
| 71 | OnDataChanged();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | protected virtual void OnDataChanged()
|
---|
| 75 | {
|
---|
| 76 | UpdateBounds(DataSource);
|
---|
| 77 |
|
---|
| 78 | RaiseDataChanged();
|
---|
| 79 | Update();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private void RaiseDataChanged()
|
---|
| 83 | {
|
---|
| 84 | if (DataChanged != null)
|
---|
| 85 | {
|
---|
| 86 | DataChanged(this, EventArgs.Empty);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | public event EventHandler DataChanged;
|
---|
| 90 |
|
---|
| 91 | protected virtual void OnDataSourceChanged(DependencyPropertyChangedEventArgs args)
|
---|
| 92 | {
|
---|
| 93 | IPointDataSource newDataSource = (IPointDataSource)args.NewValue;
|
---|
| 94 | if (newDataSource != null)
|
---|
| 95 | {
|
---|
| 96 | UpdateBounds(newDataSource);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | Update();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | private void UpdateBounds(IPointDataSource dataSource)
|
---|
| 103 | {
|
---|
| 104 | if (Plotter2D != null)
|
---|
| 105 | {
|
---|
| 106 | var transform = GetTransform();
|
---|
| 107 | DataRect bounds = BoundsHelper.GetViewportBounds(dataSource.GetPoints(), transform.DataTransform);
|
---|
| 108 | Viewport2D.SetContentBounds(this, bounds);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | #region DataTransform
|
---|
| 115 |
|
---|
| 116 | private DataTransform dataTransform = null;
|
---|
| 117 | public DataTransform DataTransform
|
---|
| 118 | {
|
---|
| 119 | get { return dataTransform; }
|
---|
| 120 | set
|
---|
| 121 | {
|
---|
| 122 | if (dataTransform != value)
|
---|
| 123 | {
|
---|
| 124 | dataTransform = value;
|
---|
| 125 | Update();
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | protected CoordinateTransform GetTransform()
|
---|
| 131 | {
|
---|
| 132 | if (Plotter == null)
|
---|
| 133 | return null;
|
---|
| 134 |
|
---|
| 135 | var transform = Plotter2D.Viewport.Transform;
|
---|
| 136 | if (dataTransform != null)
|
---|
| 137 | transform = transform.WithDataTransform(dataTransform);
|
---|
| 138 |
|
---|
| 139 | return transform;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | #endregion
|
---|
| 143 |
|
---|
| 144 | #region VisiblePoints
|
---|
| 145 |
|
---|
| 146 | public ReadOnlyCollection<Point> VisiblePoints
|
---|
| 147 | {
|
---|
| 148 | get { return GetVisiblePoints(this); }
|
---|
| 149 | protected set { SetVisiblePoints(this, value); }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | public static ReadOnlyCollection<Point> GetVisiblePoints(DependencyObject obj)
|
---|
| 153 | {
|
---|
| 154 | return (ReadOnlyCollection<Point>)obj.GetValue(VisiblePointsProperty);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | public static void SetVisiblePoints(DependencyObject obj, ReadOnlyCollection<Point> value)
|
---|
| 158 | {
|
---|
| 159 | obj.SetValue(VisiblePointsProperty, value);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | public static readonly DependencyProperty VisiblePointsProperty = DependencyProperty.RegisterAttached(
|
---|
| 163 | "VisiblePoints",
|
---|
| 164 | typeof(ReadOnlyCollection<Point>),
|
---|
| 165 | typeof(PointsGraphBase),
|
---|
| 166 | new FrameworkPropertyMetadata(null, OnVisiblePointsChanged));
|
---|
| 167 |
|
---|
| 168 | private static void OnVisiblePointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
---|
| 169 | {
|
---|
| 170 | PointsGraphBase graph = d as PointsGraphBase;
|
---|
| 171 | if (graph != null)
|
---|
| 172 | {
|
---|
| 173 | graph.RaiseVisiblePointsChanged();
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | public event EventHandler VisiblePointsChanged;
|
---|
| 178 | protected void RaiseVisiblePointsChanged()
|
---|
| 179 | {
|
---|
| 180 | VisiblePointsChanged.Raise(this);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | private bool provideVisiblePoints = false;
|
---|
| 184 | public bool ProvideVisiblePoints
|
---|
| 185 | {
|
---|
| 186 | get { return provideVisiblePoints; }
|
---|
| 187 | set
|
---|
| 188 | {
|
---|
| 189 | provideVisiblePoints = value;
|
---|
| 190 | UpdateCore();
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | #endregion
|
---|
| 195 |
|
---|
| 196 | protected IEnumerable<Point> GetPoints()
|
---|
| 197 | {
|
---|
| 198 | return DataSource.GetPoints(GetContext());
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | private readonly DataSource2dContext context = new DataSource2dContext();
|
---|
| 202 | protected DependencyObject GetContext()
|
---|
| 203 | {
|
---|
| 204 | //context.VisibleRect = Plotter2D.Viewport.Visible;
|
---|
| 205 | //context.ScreenRect = Plotter2D.Viewport.Output;
|
---|
| 206 |
|
---|
| 207 | return context;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|