1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Drawing;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 | using HeuristicLab.MainForm.WindowsForms;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
30 | [View("Line Chart")]
|
---|
31 | [Content(typeof(IRegressionSolution))]
|
---|
32 | public partial class RegressionSolutionLineChartView : DataAnalysisSolutionEvaluationView {
|
---|
33 | private const string TARGETVARIABLE_SERIES_NAME = "Target Variable";
|
---|
34 | private const string ESTIMATEDVALUES_TRAINING_SERIES_NAME = "Estimated Values (training)";
|
---|
35 | private const string ESTIMATEDVALUES_TEST_SERIES_NAME = "Estimated Values (test)";
|
---|
36 |
|
---|
37 | public new IRegressionSolution Content {
|
---|
38 | get { return (IRegressionSolution)base.Content; }
|
---|
39 | set { base.Content = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public RegressionSolutionLineChartView()
|
---|
43 | : base() {
|
---|
44 | InitializeComponent();
|
---|
45 | //configure axis
|
---|
46 | this.chart.CustomizeAllChartAreas();
|
---|
47 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
48 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
49 | this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
|
---|
50 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
51 |
|
---|
52 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
53 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
54 | this.chart.ChartAreas[0].CursorY.Interval = 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void RedrawChart() {
|
---|
58 | this.chart.Series.Clear();
|
---|
59 | if (Content != null) {
|
---|
60 | this.chart.ChartAreas[0].AxisX.Minimum = 0;
|
---|
61 | this.chart.ChartAreas[0].AxisX.Maximum = Content.ProblemData.Dataset.Rows - 1;
|
---|
62 |
|
---|
63 | this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
|
---|
64 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable;
|
---|
65 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
66 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray(),
|
---|
67 | Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable));
|
---|
68 |
|
---|
69 | this.chart.Series.Add(ESTIMATEDVALUES_TRAINING_SERIES_NAME);
|
---|
70 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME;
|
---|
71 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
72 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TrainingIndizes.ToArray(),
|
---|
73 | Content.EstimatedTrainingValues.ToArray());
|
---|
74 | this.chart.DataManipulator.InsertEmptyPoints(Content.ProblemData.Dataset.Rows, IntervalType.Number, ESTIMATEDVALUES_TRAINING_SERIES_NAME);
|
---|
75 | this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Tag = Content;
|
---|
76 |
|
---|
77 | this.chart.Series.Add(ESTIMATEDVALUES_TEST_SERIES_NAME);
|
---|
78 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].LegendText = ESTIMATEDVALUES_TEST_SERIES_NAME;
|
---|
79 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
80 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TestIndizes.ToArray(),
|
---|
81 | Content.EstimatedTestValues.ToArray());
|
---|
82 | this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Tag = Content;
|
---|
83 | UpdateCursorInterval();
|
---|
84 | this.UpdateStripLines();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void UpdateCursorInterval() {
|
---|
89 | var estimatedValues = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
90 | var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
91 | double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
|
---|
92 | double targetValuesRange = targetValues.Max() - targetValues.Min();
|
---|
93 | double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
|
---|
94 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
95 | double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
|
---|
96 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
97 | }
|
---|
98 |
|
---|
99 | #region events
|
---|
100 | protected override void RegisterContentEvents() {
|
---|
101 | base.RegisterContentEvents();
|
---|
102 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
103 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
104 | }
|
---|
105 | protected override void DeregisterContentEvents() {
|
---|
106 | base.DeregisterContentEvents();
|
---|
107 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
108 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
112 | RedrawChart();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
116 | UpdateEstimatedValuesLineChart();
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void OnContentChanged() {
|
---|
120 | base.OnContentChanged();
|
---|
121 | RedrawChart();
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void UpdateEstimatedValuesLineChart() {
|
---|
125 | if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
|
---|
126 | else {
|
---|
127 | if (this.chart.Series.Count > 0) {
|
---|
128 | Series s = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME];
|
---|
129 | if (s != null) {
|
---|
130 | s.Points.DataBindXY(Content.ProblemData.TrainingIndizes.ToArray(), Content.EstimatedTrainingValues.ToArray());
|
---|
131 | s.LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME;
|
---|
132 | }
|
---|
133 | s = this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME];
|
---|
134 | if (s != null) {
|
---|
135 | s.Points.DataBindXY(Content.ProblemData.TestIndizes.ToArray(), Content.EstimatedTestValues.ToArray());
|
---|
136 | s.LegendText = ESTIMATEDVALUES_TEST_SERIES_NAME;
|
---|
137 | }
|
---|
138 | this.UpdateStripLines();
|
---|
139 | UpdateCursorInterval();
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
145 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
146 | if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
|
---|
147 | result.ChartElementType == ChartElementType.Gridlines) ||
|
---|
148 | result.ChartElementType == ChartElementType.StripLines) {
|
---|
149 | foreach (var axis in result.ChartArea.Axes)
|
---|
150 | axis.ScaleView.ZoomReset(int.MaxValue);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | private void UpdateStripLines() {
|
---|
156 | this.chart.ChartAreas[0].AxisX.StripLines.Clear();
|
---|
157 |
|
---|
158 | int[] attr = new int[Content.ProblemData.Dataset.Rows + 1]; // add a virtual last row that is again empty to simplify loop further down
|
---|
159 | foreach (var row in Content.ProblemData.TrainingIndizes) {
|
---|
160 | attr[row] += 1;
|
---|
161 | }
|
---|
162 | foreach (var row in Content.ProblemData.TestIndizes) {
|
---|
163 | attr[row] += 2;
|
---|
164 | }
|
---|
165 | int start = 0;
|
---|
166 | int curAttr = attr[start];
|
---|
167 | for (int row = 0; row < attr.Length; row++) {
|
---|
168 | if (attr[row] != curAttr) {
|
---|
169 | switch (curAttr) {
|
---|
170 | case 0: break;
|
---|
171 | case 1:
|
---|
172 | this.CreateAndAddStripLine("Training", start, row, Color.FromArgb(40, Color.Green), Color.Transparent);
|
---|
173 | break;
|
---|
174 | case 2:
|
---|
175 | this.CreateAndAddStripLine("Test", start, row, Color.FromArgb(40, Color.Red), Color.Transparent);
|
---|
176 | break;
|
---|
177 | case 3:
|
---|
178 | this.CreateAndAddStripLine("Training and Test", start, row, Color.FromArgb(40, Color.Green), Color.FromArgb(40, Color.Red), ChartHatchStyle.WideUpwardDiagonal);
|
---|
179 | break;
|
---|
180 | default:
|
---|
181 | // should not happen
|
---|
182 | break;
|
---|
183 | }
|
---|
184 | curAttr = attr[row];
|
---|
185 | start = row;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void CreateAndAddStripLine(string title, int start, int end, Color color, Color secondColor, ChartHatchStyle hatchStyle = ChartHatchStyle.None) {
|
---|
191 | StripLine stripLine = new StripLine();
|
---|
192 | stripLine.BackColor = color;
|
---|
193 | stripLine.BackSecondaryColor = secondColor;
|
---|
194 | stripLine.BackHatchStyle = hatchStyle;
|
---|
195 | stripLine.Text = title;
|
---|
196 | stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
|
---|
197 | // strip range is [start .. end] inclusive, but we evaluate [start..end[ (end is exclusive)
|
---|
198 | // the strip should be by one longer (starting at start - 0.5 and ending at end + 0.5)
|
---|
199 | stripLine.StripWidth = end - start;
|
---|
200 | stripLine.IntervalOffset = start - 0.5; // start slightly to the left of the first point to clearly indicate the first point in the partition
|
---|
201 | this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|