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.Core.Views;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
31 | [View("Line Chart")]
|
---|
32 | [Content(typeof(IRegressionEnsembleSolution))]
|
---|
33 | public partial class RegressionEnsembleSolutionLineChartView : ItemView, IRegressionEnsembleSolutionEvaluationView {
|
---|
34 | private const string TARGETVARIABLE_SERIES_NAME = "TargetVariable";
|
---|
35 | private const string ESTIMATEDTRAININGVALUES_SERIES_NAME = "EstimatedValues (training)";
|
---|
36 | private const string ESTIMATEDTESTVALUES_SERIES_NAME = "EstimatedValues (test)";
|
---|
37 |
|
---|
38 | public new IRegressionEnsembleSolution Content {
|
---|
39 | get { return (IRegressionEnsembleSolution)base.Content; }
|
---|
40 | set { base.Content = value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public RegressionEnsembleSolutionLineChartView()
|
---|
44 | : base() {
|
---|
45 | InitializeComponent();
|
---|
46 | //configure axis
|
---|
47 | this.chart.CustomizeAllChartAreas();
|
---|
48 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
49 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = 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.Series.Add(TARGETVARIABLE_SERIES_NAME);
|
---|
61 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable;
|
---|
62 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
63 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable));
|
---|
64 | this.UpdateStripLines();
|
---|
65 |
|
---|
66 | var trainingIndizes = Enumerable.Range(Content.ProblemData.TrainingPartition.Start,
|
---|
67 | Content.ProblemData.TrainingPartition.End - Content.ProblemData.TrainingPartition.Start).ToArray();
|
---|
68 | var testIndizes = Enumerable.Range(Content.ProblemData.TestPartition.Start,
|
---|
69 | Content.ProblemData.TestPartition.End - Content.ProblemData.TestPartition.Start).ToArray();
|
---|
70 | this.chart.Series.Add(ESTIMATEDTRAININGVALUES_SERIES_NAME);
|
---|
71 | this.chart.Series[ESTIMATEDTRAININGVALUES_SERIES_NAME].LegendText = Content.ItemName;
|
---|
72 | this.chart.Series[ESTIMATEDTRAININGVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
73 | this.chart.Series[ESTIMATEDTRAININGVALUES_SERIES_NAME].Points.DataBindXY(trainingIndizes, Content.EstimatedTrainingValues.ToArray());
|
---|
74 | this.chart.Series[ESTIMATEDTRAININGVALUES_SERIES_NAME].Tag = Content;
|
---|
75 |
|
---|
76 | this.chart.Series.Add(ESTIMATEDTESTVALUES_SERIES_NAME);
|
---|
77 | this.chart.Series[ESTIMATEDTESTVALUES_SERIES_NAME].LegendText = Content.ItemName;
|
---|
78 | this.chart.Series[ESTIMATEDTESTVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
79 | this.chart.Series[ESTIMATEDTESTVALUES_SERIES_NAME].Points.DataBindXY(testIndizes, Content.EstimatedTestValues.ToArray());
|
---|
80 | this.chart.Series[ESTIMATEDTESTVALUES_SERIES_NAME].Tag = Content;
|
---|
81 | UpdateCursorInterval();
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void UpdateCursorInterval() {
|
---|
86 | var estimatedValues = this.chart.Series[ESTIMATEDTRAININGVALUES_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
87 | var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
88 | double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
|
---|
89 | double targetValuesRange = targetValues.Max() - targetValues.Min();
|
---|
90 | double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
|
---|
91 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
92 | double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
|
---|
93 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
94 | }
|
---|
95 |
|
---|
96 | #region events
|
---|
97 | protected override void RegisterContentEvents() {
|
---|
98 | base.RegisterContentEvents();
|
---|
99 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
100 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
101 | }
|
---|
102 | protected override void DeregisterContentEvents() {
|
---|
103 | base.DeregisterContentEvents();
|
---|
104 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
105 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
109 | RedrawChart();
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
113 | UpdateEstimatedValuesLineChart();
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected override void OnContentChanged() {
|
---|
117 | base.OnContentChanged();
|
---|
118 | RedrawChart();
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void UpdateEstimatedValuesLineChart() {
|
---|
122 | if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
|
---|
123 | else {
|
---|
124 | if (this.chart.Series.Count > 0) {
|
---|
125 | Series trainingSeries = this.chart.Series[ESTIMATEDTRAININGVALUES_SERIES_NAME];
|
---|
126 | if (trainingSeries != null) {
|
---|
127 | trainingSeries.Points.DataBindY(Content.EstimatedTrainingValues.ToArray());
|
---|
128 | trainingSeries.LegendText = Content.ItemName;
|
---|
129 | }
|
---|
130 | Series testSeries = this.chart.Series[ESTIMATEDTESTVALUES_SERIES_NAME];
|
---|
131 | if (testSeries != null) {
|
---|
132 | testSeries.Points.DataBindY(Content.EstimatedTestValues.ToArray());
|
---|
133 | testSeries.LegendText = Content.ItemName;
|
---|
134 | }
|
---|
135 | this.UpdateStripLines();
|
---|
136 | UpdateCursorInterval();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
142 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
143 | if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
|
---|
144 | result.ChartElementType == ChartElementType.Gridlines) ||
|
---|
145 | result.ChartElementType == ChartElementType.StripLines) {
|
---|
146 | foreach (var axis in result.ChartArea.Axes)
|
---|
147 | axis.ScaleView.ZoomReset(int.MaxValue);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | #endregion
|
---|
151 |
|
---|
152 | private void UpdateStripLines() {
|
---|
153 | //this.chart.ChartAreas[0].AxisX.StripLines.Clear();
|
---|
154 | //this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
|
---|
155 | // Content.ProblemData.TrainingPartition.Start,
|
---|
156 | // Content.ProblemData.TrainingPartition.End);
|
---|
157 | //this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
|
---|
158 | // Content.ProblemData.TestPartition.Start,
|
---|
159 | // Content.ProblemData.TestPartition.End);
|
---|
160 | }
|
---|
161 |
|
---|
162 | private void CreateAndAddStripLine(string title, Color c, int start, int end) {
|
---|
163 | //StripLine stripLine = new StripLine();
|
---|
164 | //stripLine.BackColor = c;
|
---|
165 | //stripLine.Text = title;
|
---|
166 | //stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
|
---|
167 | //stripLine.StripWidth = end - start;
|
---|
168 | //stripLine.IntervalOffset = start;
|
---|
169 | //this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|