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