Changeset 6760 for branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionLineChartView.cs
- Timestamp:
- 09/14/11 13:59:25 (13 years ago)
- Location:
- branches/PersistenceSpeedUp
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceSpeedUp
- Property svn:ignore
-
old new 12 12 *.psess 13 13 *.vsp 14 *.docstates
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionLineChartView.cs
r5975 r6760 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic; 22 23 using System.Drawing; 23 24 using System.Linq; 24 25 using System.Windows.Forms; 25 26 using System.Windows.Forms.DataVisualization.Charting; 26 using HeuristicLab.Core.Views;27 27 using HeuristicLab.MainForm; 28 28 using HeuristicLab.MainForm.WindowsForms; … … 31 31 [View("Line Chart")] 32 32 [Content(typeof(IRegressionSolution))] 33 public partial class RegressionSolutionLineChartView : ItemView, IRegressionSolutionEvaluationView { 34 private const string TARGETVARIABLE_SERIES_NAME = "TargetVariable"; 35 private const string ESTIMATEDVALUES_SERIES_NAME = "EstimatedValues"; 33 public partial class RegressionSolutionLineChartView : DataAnalysisSolutionEvaluationView { 34 private const string TARGETVARIABLE_SERIES_NAME = "Target Variable"; 35 private const string ESTIMATEDVALUES_TRAINING_SERIES_NAME = "Estimated Values (training)"; 36 private const string ESTIMATEDVALUES_TEST_SERIES_NAME = "Estimated Values (test)"; 37 private const string ESTIMATEDVALUES_ALL_SERIES_NAME = "Estimated Values (all samples)"; 36 38 37 39 public new IRegressionSolution Content { … … 47 49 this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; 48 50 this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; 51 this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true; 49 52 this.chart.ChartAreas[0].CursorX.Interval = 1; 50 53 … … 57 60 this.chart.Series.Clear(); 58 61 if (Content != null) { 62 this.chart.ChartAreas[0].AxisX.Minimum = 0; 63 this.chart.ChartAreas[0].AxisX.Maximum = Content.ProblemData.Dataset.Rows - 1; 64 59 65 this.chart.Series.Add(TARGETVARIABLE_SERIES_NAME); 60 66 this.chart.Series[TARGETVARIABLE_SERIES_NAME].LegendText = Content.ProblemData.TargetVariable; 61 67 this.chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine; 62 this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable)); 68 this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindXY(Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray(), 69 Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray()); 70 71 this.chart.Series.Add(ESTIMATEDVALUES_TRAINING_SERIES_NAME); 72 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].LegendText = ESTIMATEDVALUES_TRAINING_SERIES_NAME; 73 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].ChartType = SeriesChartType.FastLine; 74 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TrainingIndizes.ToArray(), Content.EstimatedTrainingValues.ToArray()); 75 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Tag = Content; 76 this.chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, ESTIMATEDVALUES_TRAINING_SERIES_NAME); 77 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].EmptyPointStyle.BorderWidth = 0; 78 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].EmptyPointStyle.MarkerStyle = MarkerStyle.None; 79 80 81 this.chart.Series.Add(ESTIMATEDVALUES_TEST_SERIES_NAME); 82 this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].LegendText = ESTIMATEDVALUES_TEST_SERIES_NAME; 83 this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].ChartType = SeriesChartType.FastLine; 84 this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Points.DataBindXY(Content.ProblemData.TestIndizes.ToArray(), Content.EstimatedTestValues.ToArray()); 85 this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Tag = Content; 86 87 88 int[] allIndizes = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).Except(Content.ProblemData.TrainingIndizes).Except(Content.ProblemData.TestIndizes).ToArray(); 89 var estimatedValues = Content.EstimatedValues.ToArray(); 90 List<double> allEstimatedValues = allIndizes.Select(index => estimatedValues[index]).ToList(); 91 92 this.chart.Series.Add(ESTIMATEDVALUES_ALL_SERIES_NAME); 93 this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].LegendText = ESTIMATEDVALUES_ALL_SERIES_NAME; 94 this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].ChartType = SeriesChartType.FastLine; 95 this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].Points.DataBindXY(allIndizes, allEstimatedValues); 96 this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].Tag = Content; 97 this.chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, ESTIMATEDVALUES_ALL_SERIES_NAME); 98 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].EmptyPointStyle.BorderWidth = 0; 99 this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].EmptyPointStyle.MarkerStyle = MarkerStyle.None; 100 this.ToggleSeriesData(this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME]); 101 102 UpdateCursorInterval(); 63 103 this.UpdateStripLines(); 64 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 104 } 72 105 } 73 106 74 107 private void UpdateCursorInterval() { 75 var estimatedValues = this.chart.Series[ESTIMATEDVALUES_ SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0);108 var estimatedValues = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0); 76 109 var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0); 77 110 double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min(); … … 95 128 } 96 129 97 private void Content_ProblemDataChanged(object sender, EventArgs e) {98 RedrawChart();99 }100 101 private void Content_ModelChanged(object sender, EventArgs e) {102 UpdateEstimatedValuesLineChart();103 }104 105 130 protected override void OnContentChanged() { 106 131 base.OnContentChanged(); 107 132 RedrawChart(); 108 133 } 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(); 119 UpdateCursorInterval(); 120 } 121 } 122 } 123 } 134 private void Content_ProblemDataChanged(object sender, EventArgs e) { 135 RedrawChart(); 136 } 137 private void Content_ModelChanged(object sender, EventArgs e) { 138 RedrawChart(); 139 } 140 141 124 142 125 143 private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) { … … 136 154 private void UpdateStripLines() { 137 155 this.chart.ChartAreas[0].AxisX.StripLines.Clear(); 138 this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green), 139 Content.ProblemData.TrainingPartition.Start, 140 Content.ProblemData.TrainingPartition.End); 141 this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red), 142 Content.ProblemData.TestPartition.Start, 143 Content.ProblemData.TestPartition.End); 144 } 145 146 private void CreateAndAddStripLine(string title, Color c, int start, int end) { 156 157 int[] attr = new int[Content.ProblemData.Dataset.Rows + 1]; // add a virtual last row that is again empty to simplify loop further down 158 foreach (var row in Content.ProblemData.TrainingIndizes) { 159 attr[row] += 1; 160 } 161 foreach (var row in Content.ProblemData.TestIndizes) { 162 attr[row] += 2; 163 } 164 int start = 0; 165 int curAttr = attr[start]; 166 for (int row = 0; row < attr.Length; row++) { 167 if (attr[row] != curAttr) { 168 switch (curAttr) { 169 case 0: break; 170 case 1: 171 this.CreateAndAddStripLine("Training", start, row, Color.FromArgb(40, Color.Green), Color.Transparent); 172 break; 173 case 2: 174 this.CreateAndAddStripLine("Test", start, row, Color.FromArgb(40, Color.Red), Color.Transparent); 175 break; 176 case 3: 177 this.CreateAndAddStripLine("Training and Test", start, row, Color.FromArgb(40, Color.Green), Color.FromArgb(40, Color.Red), ChartHatchStyle.WideUpwardDiagonal); 178 break; 179 default: 180 // should not happen 181 break; 182 } 183 curAttr = attr[row]; 184 start = row; 185 } 186 } 187 } 188 189 private void CreateAndAddStripLine(string title, int start, int end, Color color, Color secondColor, ChartHatchStyle hatchStyle = ChartHatchStyle.None) { 147 190 StripLine stripLine = new StripLine(); 148 stripLine.BackColor = c; 191 stripLine.BackColor = color; 192 stripLine.BackSecondaryColor = secondColor; 193 stripLine.BackHatchStyle = hatchStyle; 149 194 stripLine.Text = title; 150 195 stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold); 196 // strip range is [start .. end] inclusive, but we evaluate [start..end[ (end is exclusive) 197 // the strip should be by one longer (starting at start - 0.5 and ending at end + 0.5) 151 198 stripLine.StripWidth = end - start; 152 stripLine.IntervalOffset = start ;199 stripLine.IntervalOffset = start - 0.5; // start slightly to the left of the first point to clearly indicate the first point in the partition 153 200 this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine); 201 } 202 203 private void ToggleSeriesData(Series series) { 204 if (series.Points.Count > 0) { //checks if series is shown 205 if (this.chart.Series.Any(s => s != series && s.Points.Count > 0)) { 206 series.Points.Clear(); 207 } 208 } else if (Content != null) { 209 string targetVariableName = Content.ProblemData.TargetVariable; 210 211 IEnumerable<int> indizes = null; 212 IEnumerable<double> predictedValues = null; 213 switch (series.Name) { 214 case ESTIMATEDVALUES_ALL_SERIES_NAME: 215 indizes = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).Except(Content.ProblemData.TrainingIndizes).Except(Content.ProblemData.TestIndizes).ToArray(); 216 var estimatedValues = Content.EstimatedValues.ToArray(); 217 predictedValues = indizes.Select(index => estimatedValues[index]).ToList(); 218 break; 219 case ESTIMATEDVALUES_TRAINING_SERIES_NAME: 220 indizes = Content.ProblemData.TrainingIndizes.ToArray(); 221 predictedValues = Content.EstimatedTrainingValues.ToArray(); 222 break; 223 case ESTIMATEDVALUES_TEST_SERIES_NAME: 224 indizes = Content.ProblemData.TestIndizes.ToArray(); 225 predictedValues = Content.EstimatedTestValues.ToArray(); 226 break; 227 } 228 series.Points.DataBindXY(indizes, predictedValues); 229 chart.DataManipulator.InsertEmptyPoints(1, IntervalType.Number, series.Name); 230 chart.Legends[series.Legend].ForeColor = Color.Black; 231 UpdateCursorInterval(); 232 } 233 } 234 235 private void chart_MouseMove(object sender, MouseEventArgs e) { 236 HitTestResult result = chart.HitTest(e.X, e.Y); 237 if (result.ChartElementType == ChartElementType.LegendItem && result.Series.Name != TARGETVARIABLE_SERIES_NAME) 238 Cursor = Cursors.Hand; 239 else 240 Cursor = Cursors.Default; 241 } 242 private void chart_MouseDown(object sender, MouseEventArgs e) { 243 HitTestResult result = chart.HitTest(e.X, e.Y); 244 if (result.ChartElementType == ChartElementType.LegendItem && result.Series.Name != TARGETVARIABLE_SERIES_NAME) { 245 ToggleSeriesData(result.Series); 246 } 247 } 248 249 private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) { 250 if (chart.Series.Count != 4) return; 251 e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black; 252 e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[ESTIMATEDVALUES_TRAINING_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black; 253 e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[ESTIMATEDVALUES_TEST_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black; 254 e.LegendItems[3].Cells[1].ForeColor = this.chart.Series[ESTIMATEDVALUES_ALL_SERIES_NAME].Points.Count == 0 ? Color.Gray : Color.Black; 154 255 } 155 256 }
Note: See TracChangeset
for help on using the changeset viewer.