#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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; using System.Windows.Forms.DataVisualization.Charting; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; namespace HeuristicLab.DatastreamAnalysis.Views { [View("DataBarSet")] [Content(typeof(DataBarSet), false)] public partial class DataBarSetView : ItemView { private bool updateInProgress; public virtual Image ViewImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.Graph; } } public new DataBarSet Content { get { return (DataBarSet) base.Content; } set { base.Content = value; } } public DataBarSetView() : base() { InitializeComponent(); updateInProgress = false; this.chart.CustomizeAllChartAreas(); this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; this.chart.ChartAreas[0].AxisX.Title = "Ensembles"; this.chart.ChartAreas[0].AxisX.Maximum = 0.0; this.chart.ChartAreas[0].AxisX.Maximum = Content.Bars.Count; //AddCustomLabelsToAxis(this.chart.ChartAreas[0].AxisX); this.chart.ChartAreas[0].AxisY.Title = "Estimated Values"; this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true; this.chart.ChartAreas[0].AxisY.Minimum = 0.0; this.chart.ChartAreas[0].AxisY.Maximum = 1.0; this.chart.ChartAreas[0].AxisY.Interval = 0.1; this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; } private void AddCustomLabelsToAxis(Axis axis) { axis.CustomLabels.Clear(); for(int i = 0; i < Content.Bars.Count; i++) { var bar = Content.Bars.Keys.ToList()[i].Value; CustomLabel barLabel = new CustomLabel(); barLabel.Text = bar; barLabel.FromPosition = i; barLabel.ToPosition = i + 1; axis.CustomLabels.Add(barLabel); } } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.BarsPropertyChanged += new EventHandler(Content_BarsChanged); Content.BarValuesChanged += new EventHandler(Content_BarValuesChanged); Content.ThresholdsPropertyChanged += new EventHandler(Content_ThresholdsChanged); } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.BarsPropertyChanged -= new EventHandler(Content_BarsChanged); Content.BarValuesChanged -= new EventHandler(Content_BarValuesChanged); Content.ThresholdsPropertyChanged -= new EventHandler(Content_ThresholdsChanged); } private void Content_BarsChanged(object sender, EventArgs e) { UpdateChart(); } private void Content_BarValuesChanged(object sender, EventArgs e) { UpdateChartValues(); } private void Content_ThresholdsChanged(object sender, EventArgs e) { UpdateChart(); } protected override void OnContentChanged() { base.OnContentChanged(); UpdateChart(); } private void UpdateChart() { if (updateInProgress) { Invoke((Action) UpdateChart); } updateInProgress = true; // TODO // clear the whole chart and redraw everything updateInProgress = false; } private void UpdateChartValues() { if (updateInProgress) { Invoke((Action)UpdateChart); } updateInProgress = true; // TODO // simply update the bars' values updateInProgress = false; } private void AddThresholds() { foreach (var threshold in Content.Thresholds) { // TODO: add threshold as red bold line to each of bars' conlumns } } private TextAnnotation CreateTextAnnotation(string name, int classIndex, Axis axisX, Axis axisY, double x, double y, ContentAlignment alignment) { TextAnnotation annotation = new TextAnnotation(); annotation.Text = name; annotation.AllowMoving = true; annotation.AllowResizing = false; annotation.AllowSelecting = false; annotation.IsSizeAlwaysRelative = true; annotation.ClipToChartArea = chart.ChartAreas[0].Name; annotation.Tag = classIndex; annotation.AxisX = axisX; annotation.AxisY = axisY; annotation.Alignment = alignment; annotation.X = x; annotation.Y = y; return annotation; } #region user interaction events private void chart_MouseMove(object sender, MouseEventArgs e) { HitTestResult result = chart.HitTest(e.X, e.Y); if (result.ChartElementType == ChartElementType.LegendItem) this.Cursor = Cursors.Hand; else this.Cursor = Cursors.Default; } private void chart_MouseDown(object sender, MouseEventArgs e) { HitTestResult result = chart.HitTest(e.X, e.Y); if (result.ChartElementType == ChartElementType.LegendItem) { if (result.Series != null) ToggleSeries(result.Series); } } private void ToggleSeries(Series series) { if (series.Points.Count == 0) { // TODO } else { series.Points.Clear(); } } private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) { int classIndex = (int)e.Annotation.Tag; //double[] thresholds = Content.Model.Thresholds.ToArray(); //thresholds[classIndex] = e.NewLocationY; //Array.Sort(thresholds); //Content.Model.SetThresholdsAndClassValues(thresholds, Content.Model.ClassValues); } private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) { foreach (LegendItem legendItem in e.LegendItems) { var series = chart.Series[legendItem.SeriesName]; if (series != null) { bool seriesIsInvisible = series.Points.Count == 0; foreach (LegendCell cell in legendItem.Cells) cell.ForeColor = seriesIsInvisible ? Color.Gray : Color.Black; } } } #endregion } }