[14449] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[14498] | 26 | using System.Text;
|
---|
[14449] | 27 | using System.Windows.Forms;
|
---|
| 28 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
| 29 | using HeuristicLab.Collections;
|
---|
| 30 | using HeuristicLab.Common;
|
---|
[14982] | 31 | using HeuristicLab.Core.Views;
|
---|
[14860] | 32 | using HeuristicLab.MainForm;
|
---|
[14449] | 33 |
|
---|
| 34 | namespace HeuristicLab.Analysis.Views {
|
---|
[14982] | 35 | [View("ScatterPlot View")]
|
---|
| 36 | [Content(typeof(ScatterPlot), true)]
|
---|
| 37 | public partial class ScatterPlotView : NamedItemView, IConfigureableView {
|
---|
[14449] | 38 | protected List<Series> invisibleSeries;
|
---|
| 39 | protected Dictionary<IObservableList<Point2D<double>>, ScatterPlotDataRow> pointsRowsTable;
|
---|
[14498] | 40 | protected Dictionary<Series, Series> seriesToRegressionSeriesTable;
|
---|
[14449] | 41 | private double xMin, xMax, yMin, yMax;
|
---|
| 42 |
|
---|
[14542] | 43 | public new ScatterPlot Content {
|
---|
| 44 | get { return (ScatterPlot)base.Content; }
|
---|
| 45 | set { base.Content = value; }
|
---|
[14449] | 46 | }
|
---|
| 47 |
|
---|
[14982] | 48 | public bool ShowName {
|
---|
| 49 | get { return nameTextBox.Visible; }
|
---|
| 50 | set {
|
---|
| 51 | if (nameTextBox.Visible != value) {
|
---|
| 52 | foreach (Control c in Controls) {
|
---|
| 53 | if (c == chart) continue;
|
---|
| 54 | c.Visible = value;
|
---|
| 55 | }
|
---|
| 56 | chart.Dock = value ? DockStyle.None : DockStyle.Fill;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public ScatterPlotView() {
|
---|
[14449] | 62 | InitializeComponent();
|
---|
| 63 | pointsRowsTable = new Dictionary<IObservableList<Point2D<double>>, ScatterPlotDataRow>();
|
---|
[14498] | 64 | seriesToRegressionSeriesTable = new Dictionary<Series, Series>();
|
---|
[14449] | 65 | invisibleSeries = new List<Series>();
|
---|
| 66 | chart.CustomizeAllChartAreas();
|
---|
| 67 | chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
| 68 | chart.ContextMenuStrip.Items.Add(configureToolStripMenuItem);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | #region Event Handler Registration
|
---|
[14542] | 72 | protected override void DeregisterContentEvents() {
|
---|
[14449] | 73 | foreach (ScatterPlotDataRow row in Content.Rows)
|
---|
| 74 | DeregisterScatterPlotDataRowEvents(row);
|
---|
| 75 | Content.VisualPropertiesChanged -= new EventHandler(Content_VisualPropertiesChanged);
|
---|
| 76 | Content.Rows.ItemsAdded -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsAdded);
|
---|
| 77 | Content.Rows.ItemsRemoved -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsRemoved);
|
---|
| 78 | Content.Rows.ItemsReplaced -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsReplaced);
|
---|
| 79 | Content.Rows.CollectionReset -= new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_CollectionReset);
|
---|
| 80 | }
|
---|
[14542] | 81 | protected override void RegisterContentEvents() {
|
---|
[14449] | 82 | Content.VisualPropertiesChanged += new EventHandler(Content_VisualPropertiesChanged);
|
---|
| 83 | Content.Rows.ItemsAdded += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsAdded);
|
---|
| 84 | Content.Rows.ItemsRemoved += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsRemoved);
|
---|
| 85 | Content.Rows.ItemsReplaced += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsReplaced);
|
---|
| 86 | Content.Rows.CollectionReset += new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_CollectionReset);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | protected virtual void RegisterScatterPlotDataRowEvents(ScatterPlotDataRow row) {
|
---|
| 90 | row.NameChanged += new EventHandler(Row_NameChanged);
|
---|
| 91 | row.VisualPropertiesChanged += new EventHandler(Row_VisualPropertiesChanged);
|
---|
| 92 | pointsRowsTable.Add(row.Points, row);
|
---|
| 93 | row.Points.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded);
|
---|
| 94 | row.Points.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved);
|
---|
| 95 | row.Points.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced);
|
---|
| 96 | row.Points.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset);
|
---|
| 97 | }
|
---|
| 98 | protected virtual void DeregisterScatterPlotDataRowEvents(ScatterPlotDataRow row) {
|
---|
| 99 | row.Points.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded);
|
---|
| 100 | row.Points.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved);
|
---|
| 101 | row.Points.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced);
|
---|
| 102 | row.Points.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset);
|
---|
| 103 | pointsRowsTable.Remove(row.Points);
|
---|
| 104 | row.VisualPropertiesChanged -= new EventHandler(Row_VisualPropertiesChanged);
|
---|
| 105 | row.NameChanged -= new EventHandler(Row_NameChanged);
|
---|
| 106 | }
|
---|
| 107 | #endregion
|
---|
| 108 |
|
---|
[14542] | 109 | protected override void OnContentChanged() {
|
---|
[14982] | 110 | base.OnContentChanged();
|
---|
[14449] | 111 | invisibleSeries.Clear();
|
---|
| 112 | chart.Titles[0].Text = string.Empty;
|
---|
| 113 | chart.ChartAreas[0].AxisX.Title = string.Empty;
|
---|
| 114 | chart.ChartAreas[0].AxisY.Title = string.Empty;
|
---|
| 115 | chart.Series.Clear();
|
---|
| 116 | if (Content != null) {
|
---|
| 117 | chart.Titles[0].Text = Content.Name;
|
---|
| 118 | chart.Titles[0].Visible = !string.IsNullOrEmpty(Content.Name);
|
---|
| 119 | AddScatterPlotDataRows(Content.Rows);
|
---|
| 120 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
| 121 | RecalculateMinMaxPointValues();
|
---|
| 122 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[14542] | 126 | protected override void SetEnabledStateOfControls() {
|
---|
[14982] | 127 | base.SetEnabledStateOfControls();
|
---|
[14449] | 128 | chart.Enabled = Content != null;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public void ShowConfiguration() {
|
---|
| 132 | if (Content != null) {
|
---|
| 133 | using (ScatterPlotVisualPropertiesDialog dialog = new ScatterPlotVisualPropertiesDialog(Content)) {
|
---|
| 134 | dialog.ShowDialog(this);
|
---|
| 135 | }
|
---|
| 136 | } else MessageBox.Show("Nothing to configure.");
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | protected virtual void AddScatterPlotDataRows(IEnumerable<ScatterPlotDataRow> rows) {
|
---|
| 140 | foreach (var row in rows) {
|
---|
| 141 | RegisterScatterPlotDataRowEvents(row);
|
---|
[14860] | 142 | Series series = new Series(row.Name) { Tag = row };
|
---|
| 143 |
|
---|
[14449] | 144 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
| 145 | else series.LegendText = row.Name;
|
---|
[14860] | 146 |
|
---|
[14498] | 147 | var regressionSeries = new Series(row.Name + "_Regression") {
|
---|
| 148 | Tag = row,
|
---|
| 149 | ChartType = SeriesChartType.Line,
|
---|
| 150 | BorderDashStyle = ChartDashStyle.Dot,
|
---|
| 151 | IsVisibleInLegend = false,
|
---|
| 152 | Color = Color.Transparent // to avoid auto color assignment via color palette
|
---|
| 153 | };
|
---|
[14860] | 154 |
|
---|
[14498] | 155 | seriesToRegressionSeriesTable.Add(series, regressionSeries);
|
---|
| 156 | ConfigureSeries(series, regressionSeries, row);
|
---|
[14449] | 157 | FillSeriesWithRowValues(series, row);
|
---|
[14860] | 158 |
|
---|
[14449] | 159 | chart.Series.Add(series);
|
---|
[14498] | 160 | chart.Series.Add(regressionSeries);
|
---|
| 161 | FillRegressionSeries(regressionSeries, row);
|
---|
[14449] | 162 | }
|
---|
| 163 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
| 164 | RecalculateMinMaxPointValues();
|
---|
| 165 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 166 | UpdateYCursorInterval();
|
---|
[14498] | 167 | UpdateRegressionSeriesColors();
|
---|
[14449] | 168 | }
|
---|
| 169 |
|
---|
| 170 | protected virtual void RemoveScatterPlotDataRows(IEnumerable<ScatterPlotDataRow> rows) {
|
---|
| 171 | foreach (var row in rows) {
|
---|
| 172 | DeregisterScatterPlotDataRowEvents(row);
|
---|
| 173 | Series series = chart.Series[row.Name];
|
---|
| 174 | chart.Series.Remove(series);
|
---|
| 175 | if (invisibleSeries.Contains(series))
|
---|
| 176 | invisibleSeries.Remove(series);
|
---|
[14498] | 177 | chart.Series.Remove(seriesToRegressionSeriesTable[series]);
|
---|
| 178 | seriesToRegressionSeriesTable.Remove(series);
|
---|
[14449] | 179 | }
|
---|
| 180 | RecalculateMinMaxPointValues();
|
---|
| 181 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[14498] | 184 | private void ConfigureSeries(Series series, Series regressionSeries, ScatterPlotDataRow row) {
|
---|
[14449] | 185 | series.BorderWidth = 1;
|
---|
| 186 | series.BorderDashStyle = ChartDashStyle.Solid;
|
---|
| 187 | series.BorderColor = Color.Empty;
|
---|
| 188 |
|
---|
| 189 | if (row.VisualProperties.Color != Color.Empty)
|
---|
| 190 | series.Color = row.VisualProperties.Color;
|
---|
| 191 | else series.Color = Color.Empty;
|
---|
| 192 | series.IsVisibleInLegend = row.VisualProperties.IsVisibleInLegend;
|
---|
| 193 | series.ChartType = SeriesChartType.FastPoint;
|
---|
| 194 | series.MarkerSize = row.VisualProperties.PointSize;
|
---|
| 195 | series.MarkerStyle = ConvertPointStyle(row.VisualProperties.PointStyle);
|
---|
| 196 | series.XAxisType = AxisType.Primary;
|
---|
| 197 | series.YAxisType = AxisType.Primary;
|
---|
| 198 |
|
---|
| 199 | if (row.VisualProperties.DisplayName.Trim() != String.Empty) series.LegendText = row.VisualProperties.DisplayName;
|
---|
| 200 | else series.LegendText = row.Name;
|
---|
| 201 |
|
---|
| 202 | string xAxisTitle = string.IsNullOrEmpty(Content.VisualProperties.XAxisTitle)
|
---|
| 203 | ? "X"
|
---|
| 204 | : Content.VisualProperties.XAxisTitle;
|
---|
| 205 | string yAxisTitle = string.IsNullOrEmpty(Content.VisualProperties.YAxisTitle)
|
---|
| 206 | ? "Y"
|
---|
| 207 | : Content.VisualProperties.YAxisTitle;
|
---|
| 208 | series.ToolTip =
|
---|
| 209 | series.LegendText + Environment.NewLine +
|
---|
| 210 | xAxisTitle + " = " + "#VALX," + Environment.NewLine +
|
---|
| 211 | yAxisTitle + " = " + "#VAL";
|
---|
[14498] | 212 |
|
---|
| 213 | regressionSeries.BorderWidth = Math.Max(1, row.VisualProperties.PointSize / 2);
|
---|
| 214 | regressionSeries.IsVisibleInLegend = row.VisualProperties.IsRegressionVisibleInLegend &&
|
---|
| 215 | row.VisualProperties.RegressionType != ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.None;
|
---|
| 216 | regressionSeries.LegendText = string.IsNullOrEmpty(row.VisualProperties.RegressionDisplayName)
|
---|
| 217 | ? string.Format("{0}({1})", row.VisualProperties.RegressionType, row.Name)
|
---|
| 218 | : row.VisualProperties.RegressionDisplayName;
|
---|
[14449] | 219 | }
|
---|
| 220 |
|
---|
| 221 | private void ConfigureChartArea(ChartArea area) {
|
---|
| 222 | if (Content.VisualProperties.TitleFont != null) chart.Titles[0].Font = Content.VisualProperties.TitleFont;
|
---|
| 223 | if (!Content.VisualProperties.TitleColor.IsEmpty) chart.Titles[0].ForeColor = Content.VisualProperties.TitleColor;
|
---|
| 224 | chart.Titles[0].Text = Content.VisualProperties.Title;
|
---|
| 225 | chart.Titles[0].Visible = !string.IsNullOrEmpty(Content.VisualProperties.Title);
|
---|
| 226 |
|
---|
| 227 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisX.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
| 228 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisX.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
| 229 | area.AxisX.Title = Content.VisualProperties.XAxisTitle;
|
---|
| 230 | area.AxisX.MajorGrid.Enabled = Content.VisualProperties.XAxisGrid;
|
---|
| 231 |
|
---|
| 232 | if (Content.VisualProperties.AxisTitleFont != null) area.AxisY.TitleFont = Content.VisualProperties.AxisTitleFont;
|
---|
| 233 | if (!Content.VisualProperties.AxisTitleColor.IsEmpty) area.AxisY.TitleForeColor = Content.VisualProperties.AxisTitleColor;
|
---|
| 234 | area.AxisY.Title = Content.VisualProperties.YAxisTitle;
|
---|
| 235 | area.AxisY.MajorGrid.Enabled = Content.VisualProperties.YAxisGrid;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | private void RecalculateAxesScale(ChartArea area) {
|
---|
| 239 | area.AxisX.Minimum = CalculateMinBound(xMin);
|
---|
| 240 | area.AxisX.Maximum = CalculateMaxBound(xMax);
|
---|
| 241 | if (area.AxisX.Minimum == area.AxisX.Maximum) {
|
---|
| 242 | area.AxisX.Minimum = xMin - 0.5;
|
---|
| 243 | area.AxisX.Maximum = xMax + 0.5;
|
---|
| 244 | }
|
---|
| 245 | area.AxisY.Minimum = CalculateMinBound(yMin);
|
---|
| 246 | area.AxisY.Maximum = CalculateMaxBound(yMax);
|
---|
| 247 | if (area.AxisY.Minimum == area.AxisY.Maximum) {
|
---|
| 248 | area.AxisY.Minimum = yMin - 0.5;
|
---|
| 249 | area.AxisY.Maximum = yMax + 0.5;
|
---|
| 250 | }
|
---|
| 251 | if (xMax - xMin > 0) area.CursorX.Interval = Math.Pow(10, Math.Floor(Math.Log10(area.AxisX.Maximum - area.AxisX.Minimum) - 3));
|
---|
| 252 | else area.CursorX.Interval = 1;
|
---|
| 253 | area.AxisX.IsMarginVisible = false;
|
---|
| 254 |
|
---|
| 255 | if (!Content.VisualProperties.XAxisMinimumAuto && !double.IsNaN(Content.VisualProperties.XAxisMinimumFixedValue)) area.AxisX.Minimum = Content.VisualProperties.XAxisMinimumFixedValue;
|
---|
| 256 | if (!Content.VisualProperties.XAxisMaximumAuto && !double.IsNaN(Content.VisualProperties.XAxisMaximumFixedValue)) area.AxisX.Maximum = Content.VisualProperties.XAxisMaximumFixedValue;
|
---|
| 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 | }
|
---|
| 260 |
|
---|
| 261 | private static double CalculateMinBound(double min) {
|
---|
| 262 | if (min == 0) return 0;
|
---|
| 263 | var scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(min))));
|
---|
| 264 | return scale * (Math.Floor(min / scale));
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | private static double CalculateMaxBound(double max) {
|
---|
| 268 | if (max == 0) return 0;
|
---|
| 269 | var scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(max))));
|
---|
| 270 | return scale * (Math.Ceiling(max / scale));
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | protected virtual void UpdateYCursorInterval() {
|
---|
| 274 | double interestingValuesRange = (
|
---|
| 275 | from series in chart.Series
|
---|
| 276 | where series.Enabled
|
---|
| 277 | let values = (from point in series.Points
|
---|
| 278 | where !point.IsEmpty
|
---|
| 279 | select point.YValues[0]).DefaultIfEmpty(1.0)
|
---|
| 280 | let range = values.Max() - values.Min()
|
---|
| 281 | where range > 0.0
|
---|
| 282 | select range
|
---|
| 283 | ).DefaultIfEmpty(1.0).Min();
|
---|
| 284 |
|
---|
| 285 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
| 286 | double yZoomInterval = Math.Pow(10, digits);
|
---|
| 287 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[14498] | 290 | protected void UpdateRegressionSeriesColors() {
|
---|
| 291 | chart.ApplyPaletteColors();
|
---|
| 292 | foreach (var row in Content.Rows) {
|
---|
| 293 | var series = chart.Series[row.Name];
|
---|
| 294 | var regressionSeries = seriesToRegressionSeriesTable[series];
|
---|
| 295 | regressionSeries.Color = series.Color;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[14449] | 299 | #region Event Handlers
|
---|
| 300 | #region Content Event Handlers
|
---|
[14982] | 301 | protected override void Content_NameChanged(object sender, EventArgs e) {
|
---|
| 302 | if (InvokeRequired)
|
---|
| 303 | Invoke(new EventHandler(Content_NameChanged), sender, e);
|
---|
| 304 | else {
|
---|
| 305 | Content.VisualProperties.Title = Content.Name;
|
---|
| 306 | base.Content_NameChanged(sender, e);
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
[14449] | 309 | private void Content_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
| 310 | if (InvokeRequired)
|
---|
| 311 | Invoke(new EventHandler(Content_VisualPropertiesChanged), sender, e);
|
---|
| 312 | else {
|
---|
| 313 | ConfigureChartArea(chart.ChartAreas[0]);
|
---|
| 314 | RecalculateAxesScale(chart.ChartAreas[0]); // axes min/max could have changed
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | #endregion
|
---|
| 318 | #region Rows Event Handlers
|
---|
| 319 | private void Rows_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
| 320 | if (InvokeRequired)
|
---|
| 321 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsAdded), sender, e);
|
---|
| 322 | else {
|
---|
| 323 | AddScatterPlotDataRows(e.Items);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 | private void Rows_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
| 327 | if (InvokeRequired)
|
---|
| 328 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsRemoved), sender, e);
|
---|
| 329 | else {
|
---|
| 330 | RemoveScatterPlotDataRows(e.Items);
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | private void Rows_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
| 334 | if (InvokeRequired)
|
---|
| 335 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_ItemsReplaced), sender, e);
|
---|
| 336 | else {
|
---|
| 337 | RemoveScatterPlotDataRows(e.OldItems);
|
---|
| 338 | AddScatterPlotDataRows(e.Items);
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 | private void Rows_CollectionReset(object sender, CollectionItemsChangedEventArgs<ScatterPlotDataRow> e) {
|
---|
| 342 | if (InvokeRequired)
|
---|
| 343 | Invoke(new CollectionItemsChangedEventHandler<ScatterPlotDataRow>(Rows_CollectionReset), sender, e);
|
---|
| 344 | else {
|
---|
| 345 | RemoveScatterPlotDataRows(e.OldItems);
|
---|
| 346 | AddScatterPlotDataRows(e.Items);
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 | #endregion
|
---|
| 350 | #region Row Event Handlers
|
---|
| 351 | private void Row_VisualPropertiesChanged(object sender, EventArgs e) {
|
---|
| 352 | if (InvokeRequired)
|
---|
| 353 | Invoke(new EventHandler(Row_VisualPropertiesChanged), sender, e);
|
---|
| 354 | else {
|
---|
| 355 | ScatterPlotDataRow row = (ScatterPlotDataRow)sender;
|
---|
| 356 | Series series = chart.Series[row.Name];
|
---|
[14498] | 357 | Series regressionSeries = seriesToRegressionSeriesTable[series];
|
---|
[14449] | 358 | series.Points.Clear();
|
---|
[14498] | 359 | regressionSeries.Points.Clear();
|
---|
| 360 | ConfigureSeries(series, regressionSeries, row);
|
---|
[14449] | 361 | FillSeriesWithRowValues(series, row);
|
---|
[14498] | 362 | FillRegressionSeries(regressionSeries, row);
|
---|
[14449] | 363 | RecalculateMinMaxPointValues();
|
---|
| 364 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
[14498] | 365 | UpdateRegressionSeriesColors();
|
---|
[14449] | 366 | }
|
---|
| 367 | }
|
---|
| 368 | private void Row_NameChanged(object sender, EventArgs e) {
|
---|
| 369 | if (InvokeRequired)
|
---|
| 370 | Invoke(new EventHandler(Row_NameChanged), sender, e);
|
---|
| 371 | else {
|
---|
| 372 | ScatterPlotDataRow row = (ScatterPlotDataRow)sender;
|
---|
| 373 | chart.Series[row.Name].Name = row.Name;
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | #endregion
|
---|
| 377 | #region Points Event Handlers
|
---|
| 378 | private void Points_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
| 379 | if (InvokeRequired)
|
---|
| 380 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsAdded), sender, e);
|
---|
| 381 | else {
|
---|
| 382 | ScatterPlotDataRow row = null;
|
---|
| 383 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
| 384 | if (row != null) {
|
---|
| 385 | Series rowSeries = chart.Series[row.Name];
|
---|
[14498] | 386 | Series regressionSeries = seriesToRegressionSeriesTable[rowSeries];
|
---|
[14449] | 387 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
| 388 | rowSeries.Points.Clear();
|
---|
[14498] | 389 | regressionSeries.Points.Clear();
|
---|
[14449] | 390 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[14498] | 391 | FillRegressionSeries(regressionSeries, row);
|
---|
[14449] | 392 | RecalculateMinMaxPointValues();
|
---|
| 393 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 394 | UpdateYCursorInterval();
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 | private void Points_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
| 400 | if (InvokeRequired)
|
---|
| 401 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsRemoved), sender, e);
|
---|
| 402 | else {
|
---|
| 403 | ScatterPlotDataRow row = null;
|
---|
| 404 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
| 405 | if (row != null) {
|
---|
| 406 | Series rowSeries = chart.Series[row.Name];
|
---|
[14498] | 407 | Series regressionSeries = seriesToRegressionSeriesTable[rowSeries];
|
---|
[14449] | 408 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
| 409 | rowSeries.Points.Clear();
|
---|
[14498] | 410 | regressionSeries.Points.Clear();
|
---|
[14449] | 411 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[14498] | 412 | FillRegressionSeries(regressionSeries, row);
|
---|
[14449] | 413 | RecalculateMinMaxPointValues();
|
---|
| 414 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 415 | UpdateYCursorInterval();
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 | private void Points_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
| 421 | if (InvokeRequired)
|
---|
| 422 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_ItemsReplaced), sender, e);
|
---|
| 423 | else {
|
---|
| 424 | ScatterPlotDataRow row = null;
|
---|
| 425 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
| 426 | if (row != null) {
|
---|
| 427 | Series rowSeries = chart.Series[row.Name];
|
---|
[14498] | 428 | Series regressionSeries = seriesToRegressionSeriesTable[rowSeries];
|
---|
[14449] | 429 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
| 430 | rowSeries.Points.Clear();
|
---|
[14498] | 431 | regressionSeries.Points.Clear();
|
---|
[14449] | 432 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[14498] | 433 | FillRegressionSeries(regressionSeries, row);
|
---|
[14449] | 434 | RecalculateMinMaxPointValues();
|
---|
| 435 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 436 | UpdateYCursorInterval();
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | private void Points_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Point2D<double>>> e) {
|
---|
| 442 | if (InvokeRequired)
|
---|
| 443 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<Point2D<double>>>(Points_CollectionReset), sender, e);
|
---|
| 444 | else {
|
---|
| 445 | ScatterPlotDataRow row = null;
|
---|
| 446 | pointsRowsTable.TryGetValue((IObservableList<Point2D<double>>)sender, out row);
|
---|
| 447 | if (row != null) {
|
---|
| 448 | Series rowSeries = chart.Series[row.Name];
|
---|
[14498] | 449 | Series regressionSeries = seriesToRegressionSeriesTable[rowSeries];
|
---|
[14449] | 450 | if (!invisibleSeries.Contains(rowSeries)) {
|
---|
| 451 | rowSeries.Points.Clear();
|
---|
[14498] | 452 | regressionSeries.Points.Clear();
|
---|
[14449] | 453 | FillSeriesWithRowValues(rowSeries, row);
|
---|
[14498] | 454 | FillRegressionSeries(regressionSeries, row);
|
---|
[14449] | 455 | RecalculateMinMaxPointValues();
|
---|
| 456 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 457 | UpdateYCursorInterval();
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
| 462 | #endregion
|
---|
| 463 | private void configureToolStripMenuItem_Click(object sender, System.EventArgs e) {
|
---|
| 464 | ShowConfiguration();
|
---|
| 465 | }
|
---|
| 466 | #endregion
|
---|
| 467 |
|
---|
| 468 | #region Chart Event Handlers
|
---|
[14860] | 469 | private void chart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
[14982] | 470 | HitTestResult result = chart.HitTest(e.X, e.Y, ChartElementType.DataPoint);
|
---|
[14860] | 471 | if (result.ChartElementType != ChartElementType.DataPoint) return;
|
---|
| 472 |
|
---|
| 473 | var series = result.Series;
|
---|
| 474 | var dataPoint = series.Points[result.PointIndex];
|
---|
| 475 | var tag = dataPoint.Tag;
|
---|
| 476 | var content = tag as IContent;
|
---|
| 477 |
|
---|
| 478 | if (tag == null) return;
|
---|
| 479 | if (content == null) return;
|
---|
| 480 |
|
---|
| 481 | MainFormManager.MainForm.ShowContent(content);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
[14449] | 484 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 485 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 486 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
| 487 | ToggleSeriesVisible(result.Series);
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
| 490 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
| 491 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 492 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
| 493 | this.Cursor = Cursors.Hand;
|
---|
| 494 | else
|
---|
| 495 | this.Cursor = Cursors.Default;
|
---|
| 496 | }
|
---|
| 497 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
| 498 | foreach (LegendItem legendItem in e.LegendItems) {
|
---|
| 499 | var series = chart.Series[legendItem.SeriesName];
|
---|
| 500 | if (series != null) {
|
---|
| 501 | bool seriesIsInvisible = invisibleSeries.Contains(series);
|
---|
| 502 | foreach (LegendCell cell in legendItem.Cells) {
|
---|
| 503 | cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black;
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 | #endregion
|
---|
| 509 |
|
---|
| 510 | private void ToggleSeriesVisible(Series series) {
|
---|
| 511 | if (!invisibleSeries.Contains(series)) {
|
---|
| 512 | series.Points.Clear();
|
---|
| 513 | invisibleSeries.Add(series);
|
---|
| 514 | RecalculateMinMaxPointValues();
|
---|
| 515 | } else {
|
---|
| 516 | invisibleSeries.Remove(series);
|
---|
| 517 | if (Content != null) {
|
---|
[14498] | 518 | var row = (ScatterPlotDataRow)series.Tag;
|
---|
| 519 | if (seriesToRegressionSeriesTable.ContainsKey(series))
|
---|
| 520 | FillSeriesWithRowValues(series, row);
|
---|
| 521 | else
|
---|
| 522 | FillRegressionSeries(series, row);
|
---|
[14449] | 523 | RecalculateMinMaxPointValues();
|
---|
| 524 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
| 525 | RecalculateAxesScale(chart.ChartAreas[0]);
|
---|
| 526 | UpdateYCursorInterval();
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | private void RecalculateMinMaxPointValues() {
|
---|
| 532 | yMin = xMin = double.MaxValue;
|
---|
| 533 | yMax = xMax = double.MinValue;
|
---|
| 534 | foreach (var s in chart.Series.Where(x => x.Enabled)) {
|
---|
| 535 | foreach (var p in s.Points) {
|
---|
| 536 | double x = p.XValue, y = p.YValues[0];
|
---|
| 537 | if (xMin > x) xMin = x;
|
---|
| 538 | if (xMax < x) xMax = x;
|
---|
| 539 | if (yMin > y) yMin = y;
|
---|
| 540 | if (yMax < y) yMax = y;
|
---|
| 541 | }
|
---|
| 542 | }
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | private void FillSeriesWithRowValues(Series series, ScatterPlotDataRow row) {
|
---|
[14542] | 546 | bool zerosOnly = true;
|
---|
[14449] | 547 | for (int i = 0; i < row.Points.Count; i++) {
|
---|
| 548 | var value = row.Points[i];
|
---|
| 549 | DataPoint point = new DataPoint();
|
---|
| 550 | if (IsInvalidValue(value.X) || IsInvalidValue(value.Y))
|
---|
| 551 | point.IsEmpty = true;
|
---|
| 552 | else {
|
---|
| 553 | point.XValue = value.X;
|
---|
| 554 | point.YValues = new double[] { value.Y };
|
---|
| 555 | }
|
---|
[14860] | 556 | point.Tag = value.Tag;
|
---|
[14449] | 557 | series.Points.Add(point);
|
---|
[14542] | 558 | if (value.X != 0.0f)
|
---|
| 559 | zerosOnly = false;
|
---|
[14449] | 560 | }
|
---|
[14542] | 561 | if (zerosOnly) // if all x-values are zero, the x-values are interpreted as 1, 2, 3, ...
|
---|
| 562 | series.Points.Add(new DataPoint(1, 1) { IsEmpty = true });
|
---|
[14498] | 563 | double correlation = Correlation(row.Points);
|
---|
| 564 | series.LegendToolTip = string.Format("Correlation (R²) = {0:G4}", correlation * correlation);
|
---|
[14449] | 565 | }
|
---|
| 566 |
|
---|
[14498] | 567 | private void FillRegressionSeries(Series regressionSeries, ScatterPlotDataRow row) {
|
---|
| 568 | if (row.VisualProperties.RegressionType == ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.None
|
---|
| 569 | || invisibleSeries.Contains(regressionSeries))
|
---|
| 570 | return;
|
---|
| 571 |
|
---|
| 572 | double[] coefficients;
|
---|
| 573 | if (!Fitting(row, out coefficients))
|
---|
| 574 | return;
|
---|
| 575 |
|
---|
| 576 | // Fill regrssion series
|
---|
| 577 | var validPoints = row.Points.Where(p => !IsInvalidValue(p.X));
|
---|
| 578 | double min = validPoints.Min(p => p.X), max = validPoints.Max(p => p.X);
|
---|
| 579 | double range = max - min, delta = range / row.Points.Count;
|
---|
| 580 | for (double x = min; x < max; x += delta) {
|
---|
| 581 | regressionSeries.Points.AddXY(x, Estimate(x, row, coefficients));
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | // Correlation
|
---|
| 585 | var data = row.Points.Select(p => new Point2D<double>(p.Y, Estimate(p.X, row, coefficients)));
|
---|
| 586 | double correlation = Correlation(data.ToList());
|
---|
| 587 | regressionSeries.LegendToolTip = GetStringFormula(row, coefficients) + Environment.NewLine +
|
---|
| 588 | string.Format("Correlation (R²) = {0:G4}", correlation * correlation);
|
---|
| 589 | regressionSeries.ToolTip = GetStringFormula(row, coefficients);
|
---|
| 590 | }
|
---|
| 591 |
|
---|
[14449] | 592 | #region Helpers
|
---|
| 593 | private MarkerStyle ConvertPointStyle(ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle pointStyle) {
|
---|
| 594 | switch (pointStyle) {
|
---|
| 595 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle:
|
---|
| 596 | return MarkerStyle.Circle;
|
---|
| 597 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Cross:
|
---|
| 598 | return MarkerStyle.Cross;
|
---|
| 599 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Diamond:
|
---|
| 600 | return MarkerStyle.Diamond;
|
---|
| 601 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Square:
|
---|
| 602 | return MarkerStyle.Square;
|
---|
| 603 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star4:
|
---|
| 604 | return MarkerStyle.Star4;
|
---|
| 605 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star5:
|
---|
| 606 | return MarkerStyle.Star5;
|
---|
| 607 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star6:
|
---|
| 608 | return MarkerStyle.Star6;
|
---|
| 609 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Star10:
|
---|
| 610 | return MarkerStyle.Star10;
|
---|
| 611 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Triangle:
|
---|
| 612 | return MarkerStyle.Triangle;
|
---|
| 613 | default:
|
---|
| 614 | return MarkerStyle.None;
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | protected static bool IsInvalidValue(double x) {
|
---|
| 619 | return double.IsNaN(x) || x < (double)decimal.MinValue || x > (double)decimal.MaxValue;
|
---|
| 620 | }
|
---|
| 621 | #endregion
|
---|
[14498] | 622 |
|
---|
| 623 | #region Correlation and Fitting Helper
|
---|
| 624 | protected static double Correlation(IList<Point2D<double>> values) {
|
---|
| 625 | // sums of x, y, x squared etc.
|
---|
| 626 | double sx = 0.0;
|
---|
| 627 | double sy = 0.0;
|
---|
| 628 | double sxx = 0.0;
|
---|
| 629 | double syy = 0.0;
|
---|
| 630 | double sxy = 0.0;
|
---|
| 631 |
|
---|
| 632 | int n = 0;
|
---|
| 633 | for (int i = 0; i < values.Count; i++) {
|
---|
| 634 | double x = values[i].X;
|
---|
| 635 | double y = values[i].Y;
|
---|
| 636 | if (IsInvalidValue(x) || IsInvalidValue(y))
|
---|
| 637 | continue;
|
---|
| 638 |
|
---|
| 639 | sx += x;
|
---|
| 640 | sy += y;
|
---|
| 641 | sxx += x * x;
|
---|
| 642 | syy += y * y;
|
---|
| 643 | sxy += x * y;
|
---|
| 644 | n++;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | // covariation
|
---|
| 648 | double cov = sxy / n - sx * sy / n / n;
|
---|
| 649 | // standard error of x
|
---|
[14860] | 650 | double sigmaX = Math.Sqrt(sxx / n - sx * sx / n / n);
|
---|
[14498] | 651 | // standard error of y
|
---|
[14860] | 652 | double sigmaY = Math.Sqrt(syy / n - sy * sy / n / n);
|
---|
[14498] | 653 |
|
---|
| 654 | // correlation
|
---|
| 655 | return cov / sigmaX / sigmaY;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | protected static bool Fitting(ScatterPlotDataRow row, out double[] coefficients) {
|
---|
| 659 | var xs = row.Points.Select(p => p.X).ToList();
|
---|
| 660 | var ys = row.Points.Select(p => p.Y).ToList();
|
---|
| 661 |
|
---|
| 662 | // Input transformations
|
---|
| 663 | double[,] matrix;
|
---|
| 664 | int nRows;
|
---|
| 665 | switch (row.VisualProperties.RegressionType) {
|
---|
| 666 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Linear:
|
---|
| 667 | matrix = CreateMatrix(out nRows, ys, xs);
|
---|
| 668 | break;
|
---|
| 669 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Polynomial:
|
---|
| 670 | var xss = Enumerable.Range(1, row.VisualProperties.PolynomialRegressionOrder)
|
---|
| 671 | .Select(o => xs.Select(x => Math.Pow(x, o)).ToList())
|
---|
| 672 | .Reverse(); // higher order first
|
---|
| 673 | matrix = CreateMatrix(out nRows, ys, xss.ToArray());
|
---|
| 674 | break;
|
---|
| 675 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Exponential:
|
---|
| 676 | matrix = CreateMatrix(out nRows, ys.Select(y => Math.Log(y)).ToList(), xs);
|
---|
| 677 | break;
|
---|
| 678 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Power:
|
---|
| 679 | matrix = CreateMatrix(out nRows, ys.Select(y => Math.Log(y)).ToList(), xs.Select(x => Math.Log(x)).ToList());
|
---|
| 680 | break;
|
---|
| 681 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Logarithmic:
|
---|
| 682 | matrix = CreateMatrix(out nRows, ys, xs.Select(x => Math.Log(x)).ToList());
|
---|
| 683 | break;
|
---|
| 684 | default:
|
---|
| 685 | throw new ArgumentException("Unknown RegressionType: " + row.VisualProperties.RegressionType);
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | // Linear fitting
|
---|
| 689 | bool success = LinearFitting(matrix, nRows, out coefficients);
|
---|
| 690 | if (!success) return success;
|
---|
| 691 |
|
---|
| 692 | // Output transformation
|
---|
| 693 | switch (row.VisualProperties.RegressionType) {
|
---|
| 694 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Exponential:
|
---|
| 695 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Power:
|
---|
| 696 | coefficients[1] = Math.Exp(coefficients[1]);
|
---|
| 697 | break;
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | return true;
|
---|
| 701 | }
|
---|
| 702 | protected static double[,] CreateMatrix(out int nRows, IList<double> ys, params IList<double>[] xss) {
|
---|
| 703 | var matrix = new double[ys.Count, xss.Length + 1];
|
---|
| 704 | int rowIdx = 0;
|
---|
| 705 | for (int i = 0; i < ys.Count; i++) {
|
---|
| 706 | if (IsInvalidValue(ys[i]) || xss.Any(xs => IsInvalidValue(xs[i])))
|
---|
| 707 | continue;
|
---|
| 708 | for (int j = 0; j < xss.Length; j++) {
|
---|
| 709 | matrix[rowIdx, j] = xss[j][i];
|
---|
| 710 | }
|
---|
| 711 | matrix[rowIdx, xss.Length] = ys[i];
|
---|
| 712 | rowIdx++;
|
---|
| 713 | }
|
---|
| 714 | nRows = rowIdx;
|
---|
| 715 | return matrix;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | protected static bool LinearFitting(double[,] xsy, int nRows, out double[] coefficients) {
|
---|
| 719 | int nFeatures = xsy.GetLength(1) - 1;
|
---|
| 720 |
|
---|
| 721 | alglib.linearmodel lm;
|
---|
| 722 | alglib.lrreport ar;
|
---|
| 723 | int retVal;
|
---|
| 724 | alglib.lrbuild(xsy, nRows, nFeatures, out retVal, out lm, out ar);
|
---|
| 725 | if (retVal != 1) {
|
---|
| 726 | coefficients = new double[0];
|
---|
| 727 | return false;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | alglib.lrunpack(lm, out coefficients, out nFeatures);
|
---|
| 731 | return true;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | protected static double Estimate(double x, ScatterPlotDataRow row, double[] coefficients) {
|
---|
| 735 | switch (row.VisualProperties.RegressionType) {
|
---|
| 736 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Linear:
|
---|
| 737 | return coefficients[0] * x + coefficients[1];
|
---|
| 738 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Polynomial:
|
---|
| 739 | return coefficients
|
---|
| 740 | .Reverse() // to match index and order
|
---|
| 741 | .Select((c, o) => c * Math.Pow(x, o))
|
---|
| 742 | .Sum();
|
---|
| 743 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Exponential:
|
---|
| 744 | return coefficients[1] * Math.Exp(coefficients[0] * x);
|
---|
| 745 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Power:
|
---|
| 746 | return coefficients[1] * Math.Pow(x, coefficients[0]);
|
---|
| 747 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Logarithmic:
|
---|
| 748 | return coefficients[0] * Math.Log(x) + coefficients[1];
|
---|
| 749 | default:
|
---|
| 750 | throw new ArgumentException("Unknown RegressionType: " + row.VisualProperties.RegressionType);
|
---|
| 751 | }
|
---|
| 752 | }
|
---|
| 753 |
|
---|
| 754 | protected static string GetStringFormula(ScatterPlotDataRow row, double[] coefficients) {
|
---|
| 755 | switch (row.VisualProperties.RegressionType) {
|
---|
| 756 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Linear:
|
---|
| 757 | return string.Format("{0:G4} x {1} {2:G4}", coefficients[0], Sign(coefficients[1]), Math.Abs(coefficients[1]));
|
---|
| 758 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Polynomial:
|
---|
| 759 | var sb = new StringBuilder();
|
---|
| 760 | sb.AppendFormat("{0:G4}{1}", coefficients[0], PolyFactor(coefficients.Length - 1));
|
---|
| 761 | foreach (var x in coefficients
|
---|
| 762 | .Reverse() // match index and order
|
---|
| 763 | .Select((c, o) => new { c, o })
|
---|
| 764 | .Reverse() // higher order first
|
---|
| 765 | .Skip(1)) // highest order poly already added
|
---|
| 766 | sb.AppendFormat(" {0} {1:G4}{2}", Sign(x.c), Math.Abs(x.c), PolyFactor(x.o));
|
---|
| 767 | return sb.ToString();
|
---|
| 768 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Exponential:
|
---|
| 769 | return string.Format("{0:G4} e^({1:G4} x)", coefficients[1], coefficients[0]);
|
---|
| 770 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Power:
|
---|
| 771 | return string.Format("{0:G4} x^({1:G4})", coefficients[1], coefficients[0]);
|
---|
| 772 | case ScatterPlotDataRowVisualProperties.ScatterPlotDataRowRegressionType.Logarithmic:
|
---|
| 773 | return string.Format("{0:G4} ln(x) {1} {2:G4}", coefficients[0], Sign(coefficients[1]), Math.Abs(coefficients[1]));
|
---|
| 774 | default:
|
---|
| 775 | throw new ArgumentException("Unknown RegressionType: " + row.VisualProperties.RegressionType);
|
---|
| 776 | }
|
---|
| 777 | }
|
---|
| 778 | private static string Sign(double value) {
|
---|
| 779 | return value >= 0 ? "+" : "-";
|
---|
| 780 | }
|
---|
| 781 | private static string PolyFactor(int order) {
|
---|
| 782 | if (order == 0) return "";
|
---|
| 783 | if (order == 1) return " x";
|
---|
| 784 | if (order == 2) return " x²";
|
---|
| 785 | if (order == 3) return " x³";
|
---|
| 786 | return " x^" + order;
|
---|
| 787 | }
|
---|
| 788 | #endregion
|
---|
[14449] | 789 | }
|
---|
| 790 | }
|
---|