[2908] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2908] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
[3703] | 25 | using System.Linq;
|
---|
[4068] | 26 | using System.Windows.Forms;
|
---|
[2908] | 27 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 28 | using HeuristicLab.Collections;
|
---|
| 29 | using HeuristicLab.Core.Views;
|
---|
| 30 | using HeuristicLab.MainForm;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Analysis.Views {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// The visual representation of a <see cref="Variable"/>.
|
---|
| 35 | /// </summary>
|
---|
[2917] | 36 | [View("DataTable View")]
|
---|
[2908] | 37 | [Content(typeof(DataTable), true)]
|
---|
[6342] | 38 | public partial class DataTableView : NamedItemView, IConfigureableView {
|
---|
[5097] | 39 | protected List<Series> invisibleSeries;
|
---|
| 40 | protected Dictionary<IObservableList<double>, DataRow> valuesRowsTable;
|
---|
[2908] | 41 | /// <summary>
|
---|
| 42 | /// Gets or sets the variable to represent visually.
|
---|
| 43 | /// </summary>
|
---|
| 44 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
| 45 | /// No own data storage present.</remarks>
|
---|
| 46 | public new DataTable Content {
|
---|
| 47 | get { return (DataTable)base.Content; }
|
---|
| 48 | set { base.Content = value; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /// <summary>
|
---|
| 52 | /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
|
---|
| 53 | /// </summary>
|
---|
| 54 | public DataTableView() {
|
---|
| 55 | InitializeComponent();
|
---|
| 56 | valuesRowsTable = new Dictionary<IObservableList<double>, DataRow>();
|
---|
[3703] | 57 | invisibleSeries = new List<Series>();
|
---|
[4636] | 58 | chart.CustomizeAllChartAreas();
|
---|
| 59 | chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
[2908] | 60 | }
|
---|
| 61 |
|
---|
[6342] | 62 | #region Event Handler Registration
|
---|
[2908] | 63 | /// <summary>
|
---|
| 64 | /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
|
---|
| 65 | /// </summary>
|
---|
| 66 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
| 67 | protected override void DeregisterContentEvents() {
|
---|
| 68 | foreach (DataRow row in Content.Rows)
|
---|
| 69 | DeregisterDataRowEvents(row);
|
---|
[4870] | 70 | Content.VisualPropertiesChanged -= new EventHandler(Content_VisualPropertiesChanged);
|
---|
[2908] | 71 | Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
|
---|
| 72 | Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
|
---|
| 73 | Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
|
---|
| 74 | Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
|
---|
| 75 | base.DeregisterContentEvents();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /// <summary>
|
---|
| 79 | /// Adds eventhandlers to the underlying <see cref="Variable"/>.
|
---|
| 80 | /// </summary>
|
---|
| 81 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
| 82 | protected override void RegisterContentEvents() {
|
---|
| 83 | base.RegisterContentEvents();
|
---|
[4870] | 84 | Content.VisualPropertiesChanged += new EventHandler(Content_VisualPropertiesChanged);
|
---|
[2908] | 85 | Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded);
|
---|
| 86 | Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved);
|
---|
| 87 | Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced);
|
---|
| 88 | Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset);
|
---|
| 89 | foreach (DataRow row in Content.Rows)
|
---|
| 90 | RegisterDataRowEvents(row);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[6342] | 93 | /// <summary>
|
---|
| 94 | /// Automatically called for every existing data row and whenever a data row is added
|
---|
| 95 | /// to the data table. Do not call this method directly.
|
---|
| 96 | /// </summary>
|
---|
| 97 | /// <param name="row">The DataRow that was added.</param>
|
---|
| 98 | protected virtual void RegisterDataRowEvents(DataRow row) {
|
---|
| 99 | row.NameChanged += new EventHandler(Row_NameChanged);
|
---|
| 100 | row.VisualPropertiesChanged += new EventHandler(Row_VisualPropertiesChanged);
|
---|
| 101 | valuesRowsTable.Add(row.Values, row);
|
---|
| 102 | row.Values.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
| 103 | row.Values.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
| 104 | row.Values.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
| 105 | row.Values.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
| 106 | row.Values.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /// <summary>
|
---|
| 110 | /// Automatically called for every data row that is removed from the DataTable. Do
|
---|
| 111 | /// not directly call this method.
|
---|
| 112 | /// </summary>
|
---|
| 113 | /// <param name="row">The DataRow that was removed.</param>
|
---|
| 114 | protected virtual void DeregisterDataRowEvents(DataRow row) {
|
---|
| 115 | row.Values.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded);
|
---|
| 116 | row.Values.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved);
|
---|
| 117 | row.Values.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced);
|
---|
| 118 | row.Values.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved);
|
---|
| 119 | row.Values.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset);
|
---|
| 120 | valuesRowsTable.Remove(row.Values);
|
---|
| 121 | row.VisualPropertiesChanged -= new EventHandler(Row_VisualPropertiesChanged);
|
---|
| 122 | row.NameChanged -= new EventHandler(Row_NameChanged);
|
---|
| 123 | }
|
---|
| 124 | #endregion
|
---|
| 125 |
|
---|
[2908] | 126 | protected override void OnContentChanged() {
|
---|
| 127 | base.OnContentChanged();
|
---|
[3703] | 128 | invisibleSeries.Clear();
|
---|
[4637] | 129 | chart.Titles[0].Text = string.Empty;
|
---|
[4870] | 130 | chart.ChartAreas[0].AxisX.Title = string.Empty;
|
---|
| 131 | chart.ChartAreas[0].AxisY.Title = string.Empty;
|
---|
| 132 | chart.ChartAreas[0].AxisY2.Title = string.Empty;
|
---|
[2908] | 133 | chart.Series.Clear();
|
---|
[3454] | 134 | if (Content != null) {
|
---|
[4637] | 135 | chart.Titles[0].Text = Content.Name;
|
---|
[2908] | 136 | foreach (DataRow row in Content.Rows)
|
---|
| 137 | AddDataRow(row);
|
---|
[6342] | 138 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
| 139 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[2908] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[3904] | 143 | protected override void SetEnabledStateOfControls() {
|
---|
| 144 | base.SetEnabledStateOfControls();
|
---|
[3454] | 145 | chart.Enabled = Content != null;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[6342] | 148 | public void ShowConfiguration() {
|
---|
| 149 | if (Content != null) {
|
---|
| 150 | DataTableVisualPropertiesDialog dialog = new DataTableVisualPropertiesDialog(Content);
|
---|
| 151 | dialog.ShowDialog();
|
---|
| 152 | } else MessageBox.Show("Nothing to configure.");
|
---|
| 153 | }
|
---|
[5097] | 154 |
|
---|
| 155 | /// <summary>
|
---|
| 156 | /// Add the DataRow as a series to the chart.
|
---|
| 157 | /// </summary>
|
---|
| 158 | /// <param name="row">DataRow to add as series to the chart.</param>
|
---|
| 159 | protected virtual void AddDataRow(DataRow row) {
|
---|
[2908] | 160 | Series series = new Series(row.Name);
|
---|
[6628] | 161 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
| 162 | else series.LegendText = row.Name;
|
---|
[6342] | 163 | ConfigureSeries(series, row);
|
---|
| 164 | FillSeriesWithRowValues(series, row);
|
---|
| 165 |
|
---|
| 166 | chart.Series.Add(series);
|
---|
| 167 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
| 168 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 169 | UpdateYCursorInterval();
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | private void ConfigureSeries(Series series, DataRow row) {
|
---|
| 173 | RemoveCustomPropertyIfExists(series, "PointWidth");
|
---|
| 174 | series.BorderWidth = 1;
|
---|
| 175 | series.BorderDashStyle = ChartDashStyle.Solid;
|
---|
| 176 | series.BorderColor = Color.Empty;
|
---|
| 177 |
|
---|
| 178 | if (row.VisualProperties.Color != Color.Empty)
|
---|
| 179 | series.Color = row.VisualProperties.Color;
|
---|
| 180 | else series.Color = Color.Empty;
|
---|
[7215] | 181 | series.IsVisibleInLegend = row.VisualProperties.IsVisibleInLegend;
|
---|
[6342] | 182 |
|
---|
[4644] | 183 | switch (row.VisualProperties.ChartType) {
|
---|
| 184 | case DataRowVisualProperties.DataRowChartType.Line:
|
---|
| 185 | series.ChartType = SeriesChartType.FastLine;
|
---|
[6342] | 186 | series.BorderWidth = row.VisualProperties.LineWidth;
|
---|
| 187 | series.BorderDashStyle = ConvertLineStyle(row.VisualProperties.LineStyle);
|
---|
[4644] | 188 | break;
|
---|
| 189 | case DataRowVisualProperties.DataRowChartType.Bars:
|
---|
[6342] | 190 | // Bar is incompatible with anything but Bar and StackedBar*
|
---|
| 191 | if (!chart.Series.Any(x => x.ChartType != SeriesChartType.Bar && x.ChartType != SeriesChartType.StackedBar && x.ChartType != SeriesChartType.StackedBar100))
|
---|
| 192 | series.ChartType = SeriesChartType.Bar;
|
---|
| 193 | else {
|
---|
| 194 | series.ChartType = SeriesChartType.FastPoint; //default
|
---|
| 195 | row.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points;
|
---|
| 196 | }
|
---|
[4644] | 197 | break;
|
---|
| 198 | case DataRowVisualProperties.DataRowChartType.Columns:
|
---|
| 199 | series.ChartType = SeriesChartType.Column;
|
---|
| 200 | break;
|
---|
| 201 | case DataRowVisualProperties.DataRowChartType.Points:
|
---|
| 202 | series.ChartType = SeriesChartType.FastPoint;
|
---|
| 203 | break;
|
---|
[6342] | 204 | case DataRowVisualProperties.DataRowChartType.Histogram:
|
---|
| 205 | series.ChartType = SeriesChartType.Column;
|
---|
| 206 | series.SetCustomProperty("PointWidth", "1");
|
---|
| 207 | if (!series.Color.IsEmpty && series.Color.GetBrightness() < 0.25)
|
---|
| 208 | series.BorderColor = Color.White;
|
---|
| 209 | else series.BorderColor = Color.Black;
|
---|
| 210 | break;
|
---|
[4644] | 211 | default:
|
---|
| 212 | series.ChartType = SeriesChartType.FastPoint;
|
---|
| 213 | break;
|
---|
| 214 | }
|
---|
| 215 | series.YAxisType = row.VisualProperties.SecondYAxis ? AxisType.Secondary : AxisType.Primary;
|
---|
[6342] | 216 | series.XAxisType = row.VisualProperties.SecondXAxis ? AxisType.Secondary : AxisType.Primary;
|
---|
[6628] | 217 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
| 218 | else series.LegendText = row.Name;
|
---|
| 219 | series.ToolTip = series.LegendText + " X = #INDEX, Y = #VAL";
|
---|
[2908] | 220 | }
|
---|
[3703] | 221 |
|
---|
[6342] | 222 | private void ConfigureChartArea(ChartArea area) {
|
---|
| 223 | if (Content.VisualProperties.TitleFont != null) chart.Titles[0].Font = Content.VisualProperties.TitleFont;
|
---|
| 224 | if (!Content.VisualProperties.TitleColor.IsEmpty) chart.Titles[0].ForeColor = Content.VisualProperties.TitleColor;
|
---|
[5097] | 225 |
|
---|
[6342] | 226 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisX.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
| 227 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
| 228 | area.AxisX.Title = Content.VisualProperties.XAxisTitle;
|
---|
| 229 |
|
---|
| 230 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisX2.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
| 231 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
| 232 | area.AxisX2.Title = Content.VisualProperties.SecondXAxisTitle;
|
---|
| 233 |
|
---|
| 234 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisY.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
| 235 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
| 236 | area.AxisY.Title = Content.VisualProperties.YAxisTitle;
|
---|
| 237 |
|
---|
| 238 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisY2.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
| 239 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY2.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
| 240 | area.AxisY2.Title = Content.VisualProperties.SecondYAxisTitle;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | private void RecalculateAxesScale(ChartArea area) {
|
---|
| 244 | // Reset the axes bounds so that RecalculateAxesScale() will assign new bounds
|
---|
| 245 | foreach (Axis a in area.Axes) {
|
---|
| 246 | a.Minimum = double.NaN;
|
---|
| 247 | a.Maximum = double.NaN;
|
---|
| 248 | }
|
---|
| 249 | area.RecalculateAxesScale();
|
---|
| 250 | area.AxisX.IsMarginVisible = false;
|
---|
| 251 | area.AxisX2.IsMarginVisible = false;
|
---|
| 252 |
|
---|
| 253 | if (!Content.VisualProperties.XAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue)) area.AxisX.Minimum = Content.VisualProperties.XAxisMinimumFixedValue;
|
---|
| 254 | if (!Content.VisualProperties.XAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.XAxisMaximumFixedValue)) area.AxisX.Maximum = Content.VisualProperties.XAxisMaximumFixedValue;
|
---|
| 255 | if (!Content.VisualProperties.SecondXAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMinimumFixedValue)) area.AxisX2.Minimum = Content.VisualProperties.SecondXAxisMinimumFixedValue;
|
---|
| 256 | if (!Content.VisualProperties.SecondXAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondXAxisMaximumFixedValue)) area.AxisX2.Maximum = Content.VisualProperties.SecondXAxisMaximumFixedValue;
|
---|
| 257 | if (!Content.VisualProperties.YAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.YAxisMinimumFixedValue)) area.AxisY.Minimum = Content.VisualProperties.YAxisMinimumFixedValue;
|
---|
| 258 | if (!Content.VisualProperties.YAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.YAxisMaximumFixedValue)) area.AxisY.Maximum = Content.VisualProperties.YAxisMaximumFixedValue;
|
---|
| 259 | if (!Content.VisualProperties.SecondYAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMinimumFixedValue)) area.AxisY2.Minimum = Content.VisualProperties.SecondYAxisMinimumFixedValue;
|
---|
| 260 | if (!Content.VisualProperties.SecondYAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.SecondYAxisMaximumFixedValue)) area.AxisY2.Maximum = Content.VisualProperties.SecondYAxisMaximumFixedValue;
|
---|
[6676] | 261 | if (area.AxisX.Minimum >= area.AxisX.Maximum) area.AxisX.Maximum = area.AxisX.Minimum + 1;
|
---|
| 262 | if (area.AxisX2.Minimum >= area.AxisX2.Maximum) area.AxisX2.Maximum = area.AxisX2.Minimum + 1;
|
---|
| 263 | if (area.AxisY.Minimum >= area.AxisY.Maximum) area.AxisY.Maximum = area.AxisY.Minimum + 1;
|
---|
| 264 | if (area.AxisY2.Minimum >= area.AxisY2.Maximum) area.AxisY2.Maximum = area.AxisY2.Minimum + 1;
|
---|
[6342] | 265 | }
|
---|
| 266 |
|
---|
[5097] | 267 | /// <summary>
|
---|
| 268 | /// Set the Y Cursor interval to visible points of enabled series.
|
---|
| 269 | /// </summary>
|
---|
| 270 | protected virtual void UpdateYCursorInterval() {
|
---|
[6342] | 271 | double interestingValuesRange = (
|
---|
| 272 | from series in chart.Series
|
---|
| 273 | where series.Enabled
|
---|
| 274 | let values = (from point in series.Points
|
---|
| 275 | where !point.IsEmpty
|
---|
| 276 | select point.YValues[0]).DefaultIfEmpty(1.0)
|
---|
| 277 | let range = values.Max() - values.Min()
|
---|
| 278 | where range > 0.0
|
---|
| 279 | select range
|
---|
| 280 | ).DefaultIfEmpty(1.0).Min();
|
---|
[3703] | 281 |
|
---|
| 282 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
| 283 | double yZoomInterval = Math.Pow(10, digits);
|
---|
| 284 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[5097] | 287 |
|
---|
| 288 | /// <summary>
|
---|
| 289 | /// Remove the corresponding series for a certain DataRow.
|
---|
| 290 | /// </summary>
|
---|
| 291 | /// <param name="row">DataRow which series should be removed.</param>
|
---|
| 292 | protected virtual void RemoveDataRow(DataRow row) {
|
---|
[2908] | 293 | Series series = chart.Series[row.Name];
|
---|
| 294 | chart.Series.Remove(series);
|
---|
[3703] | 295 | if (invisibleSeries.Contains(series))
|
---|
| 296 | invisibleSeries.Remove(series);
|
---|
[6342] | 297 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[2908] | 298 | }
|
---|
| 299 |
|
---|
[6342] | 300 | #region Event Handlers
|
---|
| 301 | #region Content Event Handlers
|
---|
[2908] | 302 | protected override void Content_NameChanged(object sender, EventArgs e) {
|
---|
| 303 | if (InvokeRequired)
|
---|
| 304 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
| 305 | else {
|
---|
| 306 | chart.Titles[0].Text = Content.Name;
|
---|
| 307 | base.Content_NameChanged(sender, e);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
[4870] | 310 | private void Content_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
| 311 | if (InvokeRequired)
|
---|
| 312 | Invoke(new EventHandler(Content_VisualPropertiesChanged), sender, e);
|
---|
| 313 | else {
|
---|
[6342] | 314 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
| 315 | RecalculateAxesScale(chart.ChartAreas[0]); // axes min/max could have changed
|
---|
[4870] | 316 | }
|
---|
| 317 | }
|
---|
[6342] | 318 | #endregion
|
---|
| 319 | #region Rows Event Handlers
|
---|
[2908] | 320 | private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 321 | if (InvokeRequired)
|
---|
| 322 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsAdded), sender, e);
|
---|
| 323 | else {
|
---|
| 324 | foreach (DataRow row in e.Items) {
|
---|
| 325 | AddDataRow(row);
|
---|
| 326 | RegisterDataRowEvents(row);
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 331 | if (InvokeRequired)
|
---|
| 332 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsRemoved), sender, e);
|
---|
| 333 | else {
|
---|
| 334 | foreach (DataRow row in e.Items) {
|
---|
| 335 | DeregisterDataRowEvents(row);
|
---|
| 336 | RemoveDataRow(row);
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 | private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 341 | if (InvokeRequired)
|
---|
| 342 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_ItemsReplaced), sender, e);
|
---|
| 343 | else {
|
---|
| 344 | foreach (DataRow row in e.OldItems) {
|
---|
| 345 | DeregisterDataRowEvents(row);
|
---|
| 346 | RemoveDataRow(row);
|
---|
| 347 | }
|
---|
| 348 | foreach (DataRow row in e.Items) {
|
---|
| 349 | AddDataRow(row);
|
---|
| 350 | RegisterDataRowEvents(row);
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 | private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<DataRow> e) {
|
---|
| 355 | if (InvokeRequired)
|
---|
| 356 | Invoke(new CollectionItemsChangedEventHandler<DataRow>(Rows_CollectionReset), sender, e);
|
---|
| 357 | else {
|
---|
| 358 | foreach (DataRow row in e.OldItems) {
|
---|
| 359 | DeregisterDataRowEvents(row);
|
---|
| 360 | RemoveDataRow(row);
|
---|
| 361 | }
|
---|
| 362 | foreach (DataRow row in e.Items) {
|
---|
| 363 | AddDataRow(row);
|
---|
| 364 | RegisterDataRowEvents(row);
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
[6342] | 368 | #endregion
|
---|
| 369 | #region Row Event Handlers
|
---|
[4644] | 370 | private void Row_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
| 371 | if (InvokeRequired)
|
---|
| 372 | Invoke(new EventHandler(Row_VisualPropertiesChanged), sender, e);
|
---|
| 373 | else {
|
---|
| 374 | DataRow row = (DataRow)sender;
|
---|
[6342] | 375 | Series series = chart.Series[row.Name];
|
---|
| 376 | series.Points.Clear();
|
---|
| 377 | ConfigureSeries(series, row);
|
---|
| 378 | FillSeriesWithRowValues(series, row);
|
---|
| 379 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[4644] | 380 | }
|
---|
| 381 | }
|
---|
[2908] | 382 | private void Row_NameChanged(object sender, EventArgs e) {
|
---|
| 383 | if (InvokeRequired)
|
---|
| 384 | Invoke(new EventHandler(Row_NameChanged), sender, e);
|
---|
| 385 | else {
|
---|
| 386 | DataRow row = (DataRow)sender;
|
---|
| 387 | chart.Series[row.Name].Name = row.Name;
|
---|
| 388 | }
|
---|
| 389 | }
|
---|
[6342] | 390 | #endregion
|
---|
| 391 | #region Values Event Handlers
|
---|
[2908] | 392 | private void Values_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 393 | if (InvokeRequired)
|
---|
| 394 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsAdded), sender, e);
|
---|
| 395 | else {
|
---|
[3561] | 396 | DataRow row = null;
|
---|
| 397 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
| 398 | if (row != null) {
|
---|
[3703] | 399 | Series rowSeries = chart.Series[row.Name];
|
---|
| 400 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
[4778] | 401 | rowSeries.Points.Clear();
|
---|
| 402 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[6342] | 403 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[3703] | 404 | UpdateYCursorInterval();
|
---|
[3080] | 405 | }
|
---|
| 406 | }
|
---|
[2908] | 407 | }
|
---|
| 408 | }
|
---|
| 409 | private void Values_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 410 | if (InvokeRequired)
|
---|
| 411 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsRemoved), sender, e);
|
---|
| 412 | else {
|
---|
[3561] | 413 | DataRow row = null;
|
---|
| 414 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
| 415 | if (row != null) {
|
---|
[3703] | 416 | Series rowSeries = chart.Series[row.Name];
|
---|
| 417 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
[4778] | 418 | rowSeries.Points.Clear();
|
---|
| 419 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[6342] | 420 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[3703] | 421 | UpdateYCursorInterval();
|
---|
| 422 | }
|
---|
[3561] | 423 | }
|
---|
[2908] | 424 | }
|
---|
| 425 | }
|
---|
| 426 | private void Values_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 427 | if (InvokeRequired)
|
---|
| 428 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsReplaced), sender, e);
|
---|
| 429 | else {
|
---|
[3561] | 430 | DataRow row = null;
|
---|
| 431 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
| 432 | if (row != null) {
|
---|
[3703] | 433 | Series rowSeries = chart.Series[row.Name];
|
---|
| 434 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
[6342] | 435 | if (row.VisualProperties.ChartType == DataRowVisualProperties.DataRowChartType.Histogram) {
|
---|
| 436 | rowSeries.Points.Clear();
|
---|
| 437 | FillSeriesWithRowValues(rowSeries, row);
|
---|
| 438 | } else {
|
---|
| 439 | foreach (IndexedItem<double> item in e.Items) {
|
---|
| 440 | if (IsInvalidValue(item.Value))
|
---|
| 441 | rowSeries.Points[item.Index].IsEmpty = true;
|
---|
| 442 | else {
|
---|
| 443 | rowSeries.Points[item.Index].YValues = new double[] { item.Value };
|
---|
| 444 | rowSeries.Points[item.Index].IsEmpty = false;
|
---|
| 445 | }
|
---|
[3703] | 446 | }
|
---|
[3561] | 447 | }
|
---|
[6342] | 448 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[3703] | 449 | UpdateYCursorInterval();
|
---|
[3530] | 450 | }
|
---|
[3080] | 451 | }
|
---|
[2908] | 452 | }
|
---|
| 453 | }
|
---|
| 454 | private void Values_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 455 | if (InvokeRequired)
|
---|
| 456 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_ItemsMoved), sender, e);
|
---|
| 457 | else {
|
---|
[3561] | 458 | DataRow row = null;
|
---|
| 459 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
| 460 | if (row != null) {
|
---|
[3703] | 461 | Series rowSeries = chart.Series[row.Name];
|
---|
| 462 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
[4778] | 463 | rowSeries.Points.Clear();
|
---|
| 464 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[6342] | 465 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[3703] | 466 | UpdateYCursorInterval();
|
---|
[3530] | 467 | }
|
---|
[3080] | 468 | }
|
---|
[2908] | 469 | }
|
---|
| 470 | }
|
---|
[3530] | 471 |
|
---|
[2908] | 472 | private void Values_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<double>> e) {
|
---|
| 473 | if (InvokeRequired)
|
---|
| 474 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<double>>(Values_CollectionReset), sender, e);
|
---|
| 475 | else {
|
---|
[3561] | 476 | DataRow row = null;
|
---|
| 477 | valuesRowsTable.TryGetValue((IObservableList<double>)sender, out row);
|
---|
| 478 | if (row != null) {
|
---|
[3703] | 479 | Series rowSeries = chart.Series[row.Name];
|
---|
| 480 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
| 481 | rowSeries.Points.Clear();
|
---|
[4778] | 482 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[6342] | 483 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[4778] | 484 | UpdateYCursorInterval();
|
---|
[3530] | 485 | }
|
---|
[3080] | 486 | }
|
---|
[2908] | 487 | }
|
---|
| 488 | }
|
---|
| 489 | #endregion
|
---|
[6342] | 490 | #endregion
|
---|
[4623] | 491 |
|
---|
[6342] | 492 | #region Chart Event Handlers
|
---|
[3703] | 493 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 494 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 495 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
| 496 | ToggleSeriesVisible(result.Series);
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
[6342] | 499 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 500 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 501 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 502 | this.Cursor = Cursors.Hand;
|
---|
| 503 | else
|
---|
| 504 | this.Cursor = Cursors.Default;
|
---|
| 505 | }
|
---|
| 506 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 507 | foreach (LegendItem legendItem in e.LegendItems) {
|
---|
| 508 | var series = chart.Series[legendItem.SeriesName];
|
---|
| 509 | if (series != null) {
|
---|
| 510 | bool seriesIsInvisible = invisibleSeries.Contains(series);
|
---|
| 511 | foreach (LegendCell cell in legendItem.Cells) {
|
---|
| 512 | cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
| 517 | #endregion
|
---|
[3530] | 518 |
|
---|
[3703] | 519 | private void ToggleSeriesVisible(Series series) {
|
---|
| 520 | if (!invisibleSeries.Contains(series)) {
|
---|
| 521 | series.Points.Clear();
|
---|
| 522 | invisibleSeries.Add(series);
|
---|
| 523 | } else {
|
---|
| 524 | invisibleSeries.Remove(series);
|
---|
| 525 | if (Content != null) {
|
---|
| 526 |
|
---|
| 527 | var row = (from r in Content.Rows
|
---|
| 528 | where r.Name == series.Name
|
---|
| 529 | select r).Single();
|
---|
| 530 | FillSeriesWithRowValues(series, row);
|
---|
| 531 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
[6342] | 532 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[3703] | 533 | UpdateYCursorInterval();
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | private void FillSeriesWithRowValues(Series series, DataRow row) {
|
---|
[6342] | 539 | switch (row.VisualProperties.ChartType) {
|
---|
| 540 | case DataRowVisualProperties.DataRowChartType.Histogram:
|
---|
| 541 | CalculateHistogram(series, row);
|
---|
| 542 | break;
|
---|
| 543 | default: {
|
---|
| 544 | for (int i = 0; i < row.Values.Count; i++) {
|
---|
| 545 | var value = row.Values[i];
|
---|
| 546 | DataPoint point = new DataPoint();
|
---|
| 547 | point.XValue = row.VisualProperties.StartIndexZero ? i : i + 1;
|
---|
| 548 | if (IsInvalidValue(value))
|
---|
| 549 | point.IsEmpty = true;
|
---|
| 550 | else
|
---|
| 551 | point.YValues = new double[] { value };
|
---|
| 552 | series.Points.Add(point);
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 | break;
|
---|
[3703] | 556 | }
|
---|
| 557 | }
|
---|
| 558 |
|
---|
[6342] | 559 | protected virtual void CalculateHistogram(Series series, DataRow row) {
|
---|
| 560 | series.Points.Clear();
|
---|
| 561 | if (!row.Values.Any()) return;
|
---|
| 562 | int bins = row.VisualProperties.Bins;
|
---|
| 563 |
|
---|
| 564 | double minValue = row.Values.Min();
|
---|
| 565 | double maxValue = row.Values.Max();
|
---|
| 566 | double intervalWidth = (maxValue - minValue) / bins;
|
---|
| 567 | if (intervalWidth < 0) return;
|
---|
| 568 | if (intervalWidth == 0) {
|
---|
| 569 | series.Points.AddXY(minValue, row.Values.Count);
|
---|
| 570 | return;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | if (!row.VisualProperties.ExactBins) {
|
---|
| 574 | intervalWidth = HumanRoundRange(intervalWidth);
|
---|
| 575 | minValue = Math.Floor(minValue / intervalWidth) * intervalWidth;
|
---|
| 576 | maxValue = Math.Ceiling(maxValue / intervalWidth) * intervalWidth;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
[6978] | 579 | var area = chart.ChartAreas[0];
|
---|
| 580 | double current = 0.0;
|
---|
| 581 | if (!Double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue) && !Content.VisualProperties.XAxisMinimumAuto)
|
---|
| 582 | current = Content.VisualProperties.XAxisMinimumFixedValue;
|
---|
| 583 | else current = minValue;
|
---|
| 584 |
|
---|
| 585 | area.AxisX.Interval = intervalWidth / row.VisualProperties.ScaleFactor;
|
---|
| 586 | area.AxisX.IntervalOffset = intervalWidth / row.VisualProperties.ScaleFactor;
|
---|
| 587 |
|
---|
| 588 | series.SetCustomProperty("PointWidth", intervalWidth.ToString());
|
---|
| 589 |
|
---|
[6342] | 590 | int frequency = 0;
|
---|
| 591 | foreach (double v in row.Values.Where(x => !IsInvalidValue(x)).OrderBy(x => x)) {
|
---|
| 592 | while (v > current + intervalWidth) {
|
---|
[6978] | 593 | series.Points.AddXY(current + intervalWidth, frequency);
|
---|
[6342] | 594 | current += intervalWidth;
|
---|
| 595 | frequency = 0;
|
---|
[3703] | 596 | }
|
---|
[6342] | 597 | frequency++;
|
---|
[3703] | 598 | }
|
---|
[6978] | 599 | series.Points.AddXY(current + intervalWidth, frequency);
|
---|
[3703] | 600 | }
|
---|
| 601 |
|
---|
[6342] | 602 | #region Helpers
|
---|
| 603 | protected void RemoveCustomPropertyIfExists(Series series, string property) {
|
---|
| 604 | if (series.IsCustomPropertySet(property)) series.DeleteCustomProperty(property);
|
---|
| 605 | }
|
---|
[5097] | 606 |
|
---|
[6342] | 607 | private double HumanRoundRange(double range) {
|
---|
| 608 | double base10 = Math.Pow(10.0, Math.Floor(Math.Log10(range)));
|
---|
| 609 | double rounding = range / base10;
|
---|
| 610 | if (rounding <= 1.5) rounding = 1;
|
---|
| 611 | else if (rounding <= 2.25) rounding = 2;
|
---|
| 612 | else if (rounding <= 3.75) rounding = 2.5;
|
---|
| 613 | else if (rounding <= 7.5) rounding = 5;
|
---|
| 614 | else rounding = 10;
|
---|
| 615 | return rounding * base10;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | private double HumanRoundMax(double max) {
|
---|
| 619 | double base10;
|
---|
| 620 | if (max > 0) base10 = Math.Pow(10.0, Math.Floor(Math.Log10(max)));
|
---|
| 621 | else base10 = Math.Pow(10.0, Math.Ceiling(Math.Log10(-max)));
|
---|
| 622 | double rounding = (max > 0) ? base10 : -base10;
|
---|
| 623 | while (rounding < max) rounding += base10;
|
---|
| 624 | return rounding;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | private ChartDashStyle ConvertLineStyle(DataRowVisualProperties.DataRowLineStyle dataRowLineStyle) {
|
---|
| 628 | switch (dataRowLineStyle) {
|
---|
| 629 | case DataRowVisualProperties.DataRowLineStyle.Dash:
|
---|
| 630 | return ChartDashStyle.Dash;
|
---|
| 631 | case DataRowVisualProperties.DataRowLineStyle.DashDot:
|
---|
| 632 | return ChartDashStyle.DashDot;
|
---|
| 633 | case DataRowVisualProperties.DataRowLineStyle.DashDotDot:
|
---|
| 634 | return ChartDashStyle.DashDotDot;
|
---|
| 635 | case DataRowVisualProperties.DataRowLineStyle.Dot:
|
---|
| 636 | return ChartDashStyle.Dot;
|
---|
| 637 | case DataRowVisualProperties.DataRowLineStyle.NotSet:
|
---|
| 638 | return ChartDashStyle.NotSet;
|
---|
| 639 | case DataRowVisualProperties.DataRowLineStyle.Solid:
|
---|
| 640 | return ChartDashStyle.Solid;
|
---|
| 641 | default:
|
---|
| 642 | return ChartDashStyle.NotSet;
|
---|
| 643 | }
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[5097] | 646 | /// <summary>
|
---|
| 647 | /// Determines whether a double value can be displayed (converted to Decimal and not an NaN).
|
---|
| 648 | /// </summary>
|
---|
| 649 | /// <param name="x">The number to check.</param>
|
---|
| 650 | /// <returns><code>true</code> if the value can be safely shwon in the chart,
|
---|
| 651 | /// <code>false</code> otherwise.</returns>
|
---|
| 652 | protected static bool IsInvalidValue(double x) {
|
---|
[3530] | 653 | return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
|
---|
| 654 | }
|
---|
[6342] | 655 | #endregion
|
---|
[2908] | 656 | }
|
---|
| 657 | }
|
---|