#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Drawing; using System.Linq; using System.Windows.Forms.DataVisualization.Charting; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views { [View("Sliding Window View")] [Content(typeof(SlidingWindowData), IsDefaultView = true)] public partial class SlidingWindowDataView : ItemView { private const string TARGETVARIABLE_SERIES_NAME = "Target Variable"; private const string ESTIMATEDVAlUES_SERIES_NAME = "Estimated Values"; public new SlidingWindowData Content { get { return (SlidingWindowData)base.Content; } set { base.Content = value; } } public SlidingWindowDataView() { InitializeComponent(); chart.CustomizeAllChartAreas(); chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; chart.ChartAreas[0].AxisX.IsStartedFromZero = true; chart.ChartAreas[0].CursorX.Interval = 1; chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; chart.ChartAreas[0].CursorY.Interval = 0; StripLine stripLine = new StripLine(); stripLine.BackColor = Color.Gold; stripLine.Font = new Font("Times New Roman", 12, FontStyle.Bold); stripLine.Text = "Sliding Window"; chart.ChartAreas[0].AxisX.StripLines.Add(stripLine); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.SlidingWindowPosition.ValueChanged += new EventHandler(SlidingWindowPosition_ValueChanged); Content.EstimatedValuesChanged += new EventHandler(EstimatedValuesChanged); } protected override void DeregisterContentEvents() { Content.SlidingWindowPosition.ValueChanged -= new EventHandler(SlidingWindowPosition_ValueChanged); Content.EstimatedValuesChanged -= new EventHandler(EstimatedValuesChanged); base.DeregisterContentEvents(); } protected override void OnContentChanged() { base.OnContentChanged(); RedrawChart(); } private void SlidingWindowPosition_ValueChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke((Action)SlidingWindowPosition_ValueChanged, sender, e); else UpdateStripLine(); } private void EstimatedValuesChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke((Action)EstimatedValuesChanged, sender, e); else RedrawChart(); } private void RedrawChart() { chart.Series.Clear(); if (Content == null) return; chart.Series.Add(TARGETVARIABLE_SERIES_NAME); chart.Series[TARGETVARIABLE_SERIES_NAME].ChartType = SeriesChartType.FastLine; chart.Series[TARGETVARIABLE_SERIES_NAME].Points.DataBindY(Content.TargetValues.ToArray()); if (Content.EstimatedValues.Any()) { chart.Series.Add(ESTIMATEDVAlUES_SERIES_NAME); chart.Series[ESTIMATEDVAlUES_SERIES_NAME].ChartType = SeriesChartType.FastLine; chart.Series[ESTIMATEDVAlUES_SERIES_NAME].Points.DataBindY(Content.EstimatedValues.ToArray()); } UpdateStripLine(); UpdateCursorInterval(); } private void UpdateStripLine() { chart.ChartAreas[0].AxisX.StripLines[0].StripWidth = Content.SlidingWindowPosition.End - Content.SlidingWindowPosition.Start; chart.ChartAreas[0].AxisX.StripLines[0].IntervalOffset = Content.SlidingWindowPosition.Start - 0.5; } private void UpdateCursorInterval() { var targetValues = this.chart.Series[TARGETVARIABLE_SERIES_NAME].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0); double targetValuesRange = targetValues.Max() - targetValues.Min(); double interestingValuesRange = Math.Max(targetValuesRange, 1.0); double digits = (int)Math.Log10(interestingValuesRange) - 3; double yZoomInterval = Math.Max(Math.Pow(10, digits), 10E-5); this.chart.ChartAreas[0].CursorY.Interval = yZoomInterval; } } }