Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading.Views/3.4/SolutionLineChartView.cs @ 9796

Last change on this file since 9796 was 9796, checked in by gkronber, 11 years ago

#1508 created a separate plug-in for views.

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
21using System;
22using System.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.DataAnalysis.Views;
29
30namespace HeuristicLab.Problems.DataAnalysis.Trading.Views {
31  [View("Line Chart")]
32  [Content(typeof(ISolution))]
33  public partial class SolutionLineChartView : DataAnalysisSolutionEvaluationView, ISolutionEvaluationView {
34    private const string PRICEVARIABLE_SERIES_NAME = "Price";
35    private const string SIGNALS_SERIES_NAME = "Signals";
36    private const string ASSET_SERIES_NAME = "Asset";
37
38
39    public new ISolution Content {
40      get { return (ISolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public SolutionLineChartView()
45      : base() {
46      InitializeComponent();
47      //configure axis
48      this.chart.CustomizeAllChartAreas();
49      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
50      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
51      this.chart.ChartAreas[0].CursorX.Interval = 1;
52
53      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
54      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
55      this.chart.ChartAreas[0].CursorY.Interval = 0;
56    }
57
58    private void RedrawChart() {
59      this.chart.Series.Clear();
60      if (Content != null) {
61        this.chart.Series.Add(SIGNALS_SERIES_NAME);
62        this.chart.Series[SIGNALS_SERIES_NAME].LegendText = SIGNALS_SERIES_NAME;
63        this.chart.Series[SIGNALS_SERIES_NAME].ChartType = SeriesChartType.FastLine;
64        this.chart.Series[SIGNALS_SERIES_NAME].Points.DataBindY(Content.Signals.ToArray());
65        this.chart.Series[SIGNALS_SERIES_NAME].Tag = Content;
66
67        IEnumerable<double> accumulatedPrice = GetAccumulatedPrices(Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceVariable));
68        this.chart.Series.Add(PRICEVARIABLE_SERIES_NAME);
69        this.chart.Series[PRICEVARIABLE_SERIES_NAME].LegendText = PRICEVARIABLE_SERIES_NAME;
70        this.chart.Series[PRICEVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine;
71        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Points.DataBindY(accumulatedPrice.ToArray());
72        this.chart.Series[PRICEVARIABLE_SERIES_NAME].Tag = Content;
73
74        IEnumerable<double> profit = OnlineProfitCalculator.GetProfits(Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.PriceVariable), Content.Signals, Content.ProblemData.TransactionCosts);
75        IEnumerable<double> accumulatedProfits = GetAccumulatedPrices(profit);
76        this.chart.Series.Add(ASSET_SERIES_NAME);
77        this.chart.Series[ASSET_SERIES_NAME].LegendText = ASSET_SERIES_NAME;
78        this.chart.Series[ASSET_SERIES_NAME].ChartType = SeriesChartType.FastLine;
79        this.chart.Series[ASSET_SERIES_NAME].Points.DataBindY(accumulatedProfits.ToArray());
80        this.chart.Series[ASSET_SERIES_NAME].Tag = Content;
81
82        this.UpdateStripLines();
83      }
84    }
85
86    private IEnumerable<double> GetAccumulatedPrices(IEnumerable<double> xs) {
87      double sum = 0;
88      foreach (var x in xs) {
89        sum += x;
90        yield return sum;
91      }
92    }
93
94    #region events
95    protected override void RegisterContentEvents() {
96      base.RegisterContentEvents();
97      Content.ModelChanged += new EventHandler(Content_ModelChanged);
98      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
99    }
100    protected override void DeregisterContentEvents() {
101      base.DeregisterContentEvents();
102      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
103      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
104    }
105
106    private void Content_ProblemDataChanged(object sender, EventArgs e) {
107      RedrawChart();
108    }
109
110    private void Content_ModelChanged(object sender, EventArgs e) {
111      RedrawChart();
112    }
113
114    protected override void OnContentChanged() {
115      base.OnContentChanged();
116      RedrawChart();
117    }
118
119    private void Chart_MouseDoubleClick(object sender, MouseEventArgs e) {
120      HitTestResult result = chart.HitTest(e.X, e.Y);
121      if (result.ChartArea != null && (result.ChartElementType == ChartElementType.PlottingArea ||
122                                       result.ChartElementType == ChartElementType.Gridlines) ||
123                                       result.ChartElementType == ChartElementType.StripLines) {
124        foreach (var axis in result.ChartArea.Axes)
125          axis.ScaleView.ZoomReset(int.MaxValue);
126      }
127    }
128    #endregion
129
130    private void UpdateStripLines() {
131      this.chart.ChartAreas[0].AxisX.StripLines.Clear();
132      this.CreateAndAddStripLine("Training", Color.FromArgb(20, Color.Green),
133        Content.ProblemData.TrainingPartition.Start,
134        Content.ProblemData.TrainingPartition.End);
135      this.CreateAndAddStripLine("Test", Color.FromArgb(20, Color.Red),
136        Content.ProblemData.TestPartition.Start,
137        Content.ProblemData.TestPartition.End);
138    }
139
140    private void CreateAndAddStripLine(string title, Color c, int start, int end) {
141      StripLine stripLine = new StripLine();
142      stripLine.BackColor = c;
143      stripLine.Text = title;
144      stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold);
145      stripLine.StripWidth = end - start;
146      stripLine.IntervalOffset = start;
147      this.chart.ChartAreas[0].AxisX.StripLines.Add(stripLine);
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.