1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 | using System;
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
31 | [View("Scatter Plot View")]
|
---|
32 | [Content(typeof(DataAnalysisSolution), true)]
|
---|
33 | public partial class ScatterPlotView : AsynchronousContentView {
|
---|
34 | private const string ALL_SERIES = "All Samples";
|
---|
35 | private const string TRAINING_SERIES = "Training Samples";
|
---|
36 | private const string TEST_SERIES = "Test Samples";
|
---|
37 |
|
---|
38 | public new DataAnalysisSolution Content {
|
---|
39 | get { return (DataAnalysisSolution)base.Content; }
|
---|
40 | set { base.Content = value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public ScatterPlotView()
|
---|
44 | : base() {
|
---|
45 | InitializeComponent();
|
---|
46 |
|
---|
47 | this.chart.Series.Add(ALL_SERIES);
|
---|
48 | this.chart.Series[ALL_SERIES].LegendText = ALL_SERIES;
|
---|
49 | this.chart.Series[ALL_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
50 |
|
---|
51 | this.chart.Series.Add(TRAINING_SERIES);
|
---|
52 | this.chart.Series[TRAINING_SERIES].LegendText = TRAINING_SERIES;
|
---|
53 | this.chart.Series[TRAINING_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
54 | this.chart.Series[TRAINING_SERIES].Points.Add(1.0);
|
---|
55 |
|
---|
56 | this.chart.Series.Add(TEST_SERIES);
|
---|
57 | this.chart.Series[TEST_SERIES].LegendText = TEST_SERIES;
|
---|
58 | this.chart.Series[TEST_SERIES].ChartType = SeriesChartType.FastPoint;
|
---|
59 |
|
---|
60 | this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High;
|
---|
61 | this.chart.AxisViewChanged += new EventHandler<System.Windows.Forms.DataVisualization.Charting.ViewEventArgs>(chart_AxisViewChanged);
|
---|
62 |
|
---|
63 | //configure axis
|
---|
64 | this.chart.CustomizeAllChartAreas();
|
---|
65 | this.chart.ChartAreas[0].AxisX.Title = "Estimated Values";
|
---|
66 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
67 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
68 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
69 | this.chart.ChartAreas[0].CursorY.Interval = 1;
|
---|
70 |
|
---|
71 | this.chart.ChartAreas[0].AxisY.Title = "Target Values";
|
---|
72 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
73 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
74 | this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override void RegisterContentEvents() {
|
---|
78 | base.RegisterContentEvents();
|
---|
79 | Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
|
---|
80 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
81 | }
|
---|
82 | protected override void DeregisterContentEvents() {
|
---|
83 | base.DeregisterContentEvents();
|
---|
84 | Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
|
---|
85 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
90 | UpdateChart();
|
---|
91 | }
|
---|
92 | private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
|
---|
93 | UpdateSeries();
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected override void OnContentChanged() {
|
---|
97 | base.OnContentChanged();
|
---|
98 | UpdateChart();
|
---|
99 | }
|
---|
100 |
|
---|
101 | private void UpdateChart() {
|
---|
102 | if (InvokeRequired) Invoke((Action)UpdateChart);
|
---|
103 | else {
|
---|
104 | if (Content != null) {
|
---|
105 | this.UpdateSeries();
|
---|
106 | if (!this.chart.Series.Any(s => s.Points.Count > 0))
|
---|
107 | this.ClearChart();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void UpdateCursorInterval() {
|
---|
113 | var estimatedValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0);
|
---|
114 | var targetValues = this.chart.Series[ALL_SERIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
115 | double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
|
---|
116 | double targetValuesRange = targetValues.Max() - targetValues.Min();
|
---|
117 | double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
|
---|
118 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
119 | double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
|
---|
120 | this.chart.ChartAreas[0].CursorX.Interval = zoomInterval;
|
---|
121 | this.chart.ChartAreas[0].CursorY.Interval = zoomInterval;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | private void UpdateSeries() {
|
---|
126 | if (InvokeRequired) Invoke((Action)UpdateSeries);
|
---|
127 | else {
|
---|
128 | string targetVariableName = Content.ProblemData.TargetVariable.Value;
|
---|
129 | Dataset dataset = Content.ProblemData.Dataset;
|
---|
130 | if (this.chart.Series[ALL_SERIES].Points.Count > 0)
|
---|
131 | this.chart.Series[ALL_SERIES].Points.DataBindXY(Content.EstimatedValues.ToArray(), "",
|
---|
132 | dataset.GetVariableValues(targetVariableName), "");
|
---|
133 | if (this.chart.Series[TRAINING_SERIES].Points.Count > 0)
|
---|
134 | this.chart.Series[TRAINING_SERIES].Points.DataBindXY(Content.EstimatedTrainingValues.ToArray(), "",
|
---|
135 | dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray(), "");
|
---|
136 | if (this.chart.Series[TEST_SERIES].Points.Count > 0)
|
---|
137 | this.chart.Series[TEST_SERIES].Points.DataBindXY(Content.EstimatedTestValues.ToArray(), "",
|
---|
138 | dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray(), "");
|
---|
139 |
|
---|
140 | double max = Math.Max(Content.EstimatedValues.Max(), dataset.GetVariableValues(targetVariableName).Max());
|
---|
141 | double min = Math.Min(Content.EstimatedValues.Min(), dataset.GetVariableValues(targetVariableName).Min());
|
---|
142 |
|
---|
143 | max = Math.Ceiling(max) * 1.2;
|
---|
144 | min = Math.Floor(min) * 0.8;
|
---|
145 |
|
---|
146 | this.chart.ChartAreas[0].AxisX.Maximum = max;
|
---|
147 | this.chart.ChartAreas[0].AxisX.Minimum = min;
|
---|
148 | this.chart.ChartAreas[0].AxisY.Maximum = max;
|
---|
149 | this.chart.ChartAreas[0].AxisY.Minimum = min;
|
---|
150 | UpdateCursorInterval();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void ClearChart() {
|
---|
155 | this.chart.Series[ALL_SERIES].Points.Clear();
|
---|
156 | this.chart.Series[TRAINING_SERIES].Points.Clear();
|
---|
157 | this.chart.Series[TEST_SERIES].Points.Clear();
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void ToggleSeriesData(Series series) {
|
---|
161 | if (series.Points.Count > 0) { //checks if series is shown
|
---|
162 | if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) {
|
---|
163 | series.Points.Clear();
|
---|
164 | }
|
---|
165 | } else if (Content != null) {
|
---|
166 | string targetVariableName = Content.ProblemData.TargetVariable.Value;
|
---|
167 |
|
---|
168 | IEnumerable<double> predictedValues = null;
|
---|
169 | IEnumerable<double> targetValues = null;
|
---|
170 | switch (series.Name) {
|
---|
171 | case ALL_SERIES:
|
---|
172 | predictedValues = Content.EstimatedValues.ToArray();
|
---|
173 | targetValues = Content.ProblemData.Dataset.GetVariableValues(targetVariableName);
|
---|
174 | break;
|
---|
175 | case TRAINING_SERIES:
|
---|
176 | predictedValues = Content.EstimatedTrainingValues.ToArray();
|
---|
177 | targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TrainingIndizes).ToArray();
|
---|
178 | break;
|
---|
179 | case TEST_SERIES:
|
---|
180 | predictedValues = Content.EstimatedTestValues.ToArray();
|
---|
181 | targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(targetVariableName, Content.ProblemData.TestIndizes).ToArray();
|
---|
182 | break;
|
---|
183 | }
|
---|
184 | series.Points.DataBindXY(predictedValues, "", targetValues, "");
|
---|
185 | this.chart.Legends[series.Legend].ForeColor = Color.Black;
|
---|
186 | UpdateCursorInterval();
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void chart_MouseDown(object sender, MouseEventArgs e) {
|
---|
191 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
192 | if (result.ChartElementType == ChartElementType.LegendItem) {
|
---|
193 | this.ToggleSeriesData(result.Series);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
198 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
199 | if (result.ChartElementType == ChartElementType.LegendItem)
|
---|
200 | this.Cursor = Cursors.Hand;
|
---|
201 | else
|
---|
202 | this.Cursor = Cursors.Default;
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void chart_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) {
|
---|
206 | this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize;
|
---|
207 | this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize;
|
---|
208 | }
|
---|
209 |
|
---|
210 | private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) {
|
---|
211 | e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[ALL_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
212 | e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[TRAINING_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
213 | e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|