[3408] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3408] | 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;
|
---|
[4068] | 25 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
[5829] | 26 | using HeuristicLab.Core.Views;
|
---|
[3408] | 27 | using HeuristicLab.MainForm;
|
---|
| 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 29 |
|
---|
[3442] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 31 | [View("Line Chart")]
|
---|
[5663] | 32 | [Content(typeof(IRegressionSolution))]
|
---|
[5829] | 33 | public partial class RegressionSolutionLineChartView : ItemView, IRegressionSolutionEvaluationView {
|
---|
[3442] | 34 | private const string TARGETVARIABLE_SERIES_NAME = "TargetVariable";
|
---|
| 35 | private const string ESTIMATEDVALUES_SERIES_NAME = "EstimatedValues";
|
---|
| 36 |
|
---|
[5663] | 37 | public new IRegressionSolution Content {
|
---|
| 38 | get { return (IRegressionSolution)base.Content; }
|
---|
[3916] | 39 | set { base.Content = value; }
|
---|
[3442] | 40 | }
|
---|
| 41 |
|
---|
[5663] | 42 | public RegressionSolutionLineChartView()
|
---|
[3408] | 43 | : base() {
|
---|
| 44 | InitializeComponent();
|
---|
| 45 | //configure axis
|
---|
[4651] | 46 | this.chart.CustomizeAllChartAreas();
|
---|
[3408] | 47 | this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
| 48 | this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
[3707] | 49 | this.chart.ChartAreas[0].CursorX.Interval = 1;
|
---|
[3408] | 50 |
|
---|
| 51 | this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
| 52 | this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
[3442] | 53 | this.chart.ChartAreas[0].CursorY.Interval = 0;
|
---|
[3408] | 54 | }
|
---|
| 55 |
|
---|
[3462] | 56 | private void RedrawChart() {
|
---|
[3442] | 57 | this.chart.Series.Clear();
|
---|
[4011] | 58 | if (Content != null) {
|
---|
| 59 | this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME);
|
---|
[5663] | 60 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable;
|
---|
[4011] | 61 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
[5663] | 62 | this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable));
|
---|
[4011] | 63 | this.UpdateStripLines();
|
---|
[3462] | 64 |
|
---|
[4011] | 65 | this.chart.Series.Add(ESTIMATEDVALUES_SERIES_NAME);
|
---|
| 66 | this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].LegendText = Content.ItemName;
|
---|
| 67 | this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].ChartType = SeriesChartType.FastLine;
|
---|
| 68 | this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.DataBindY(Content.EstimatedValues.ToArray());
|
---|
| 69 | this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Tag = Content;
|
---|
| 70 | UpdateCursorInterval();
|
---|
| 71 | }
|
---|
[3408] | 72 | }
|
---|
| 73 |
|
---|
[3707] | 74 | private void UpdateCursorInterval() {
|
---|
| 75 | var estimatedValues = this.chart.Series[ESTIMATEDVALUES_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
| 76 | var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);
|
---|
| 77 | double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min();
|
---|
| 78 | double targetValuesRange = targetValues.Max() - targetValues.Min();
|
---|
| 79 | double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0));
|
---|
| 80 | double digits = (int)Math.Log10(interestingValuesRange) - 3;
|
---|
| 81 | double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5);
|
---|
| 82 | this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[3442] | 85 | #region events
|
---|
| 86 | protected override void RegisterContentEvents() {
|
---|
| 87 | base.RegisterContentEvents();
|
---|
[5663] | 88 | Content.ModelChanged += new EventHandler(Content_ModelChanged);
|
---|
[3442] | 89 | Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
|
---|
[3408] | 90 | }
|
---|
[3442] | 91 | protected override void DeregisterContentEvents() {
|
---|
| 92 | base.DeregisterContentEvents();
|
---|
[5663] | 93 | Content.ModelChanged -= new EventHandler(Content_ModelChanged);
|
---|
[3442] | 94 | Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
|
---|
[3408] | 95 | }
|
---|
| 96 |
|
---|
[3916] | 97 | private void Content_ProblemDataChanged(object sender, EventArgs e) {
|
---|
[3462] | 98 | RedrawChart();
|
---|
[3408] | 99 | }
|
---|
| 100 |
|
---|
[5663] | 101 | private void Content_ModelChanged(object sender, EventArgs e) {
|
---|
[3462] | 102 | UpdateEstimatedValuesLineChart();
|
---|
[3442] | 103 | }
|
---|
| 104 |
|
---|
| 105 | protected override void OnContentChanged() {
|
---|
| 106 | base.OnContentChanged();
|
---|
[3462] | 107 | RedrawChart();
|
---|
[3442] | 108 | }
|
---|
| 109 |
|
---|
| 110 | private void UpdateEstimatedValuesLineChart() {
|
---|
| 111 | if (InvokeRequired) Invoke((Action)UpdateEstimatedValuesLineChart);
|
---|
| 112 | else {
|
---|
| 113 | if (this.chart.Series.Count > 0) {
|
---|
| 114 | Series s = this.chart.Series.SingleOrDefault(x => x.Tag == Content);
|
---|
| 115 | if (s != null) {
|
---|
| 116 | s.Points.DataBindY(Content.EstimatedValues.ToArray());
|
---|
| 117 | s.LegendText = Content.ItemName;
|
---|
| 118 | this.UpdateStripLines();
|
---|
[3707] | 119 | UpdateCursorInterval();
|
---|
[3442] | 120 | }
|
---|
[3408] | 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[5006] | 124 |
|
---|
| 125 | private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
|
---|
| 126 | HitTestResult result = chart.HitTest(e.X, e.Y);
|
---|
| 127 | if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
|
---|
| 128 | result.ChartElementType == ChartElementType.Gridlines) ||
|
---|
| 129 | result.ChartElementType == ChartElementType.StripLines) {
|
---|
| 130 | foreach (var axis in result.ChartArea.Axes)
|
---|
| 131 | axis.ScaleView.ZoomReset(int.MaxValue);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
[3442] | 134 | #endregion
|
---|
[3408] | 135 |
|
---|
| 136 | private void UpdateStripLines() {
|
---|
| 137 | this.chart.ChartAreas[0].AxisX.StripLines.Clear();
|
---|
[3442] | 138 | this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
|
---|
[5759] | 139 | Content.ProblemData.TrainingPartition.Start,
|
---|
| 140 | Content.ProblemData.TrainingPartition.End);
|
---|
[3442] | 141 | this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
|
---|
[5759] | 142 | Content.ProblemData.TestPartition.Start,
|
---|
| 143 | Content.ProblemData.TestPartition.End);
|
---|
[3408] | 144 | }
|
---|
| 145 |
|
---|
| 146 | private void CreateAndAddStripLine(string title, Color c, int start, int end) {
|
---|
| 147 | StripLine stripLine = new StripLine();
|
---|
| 148 | stripLine.BackColor = c;
|
---|
| 149 | stripLine.Text = title;
|
---|
| 150 | stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
|
---|
| 151 | stripLine.StripWidth = end - start;
|
---|
| 152 | stripLine.IntervalOffset = start;
|
---|
| 153 | this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|