#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.Collections.Generic; using System.Drawing; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common.Resources; namespace HeuristicLab.Analysis.FitnessLandscape { public class PointsEventArgs : EventArgs { public IEnumerable NewPoints { get; private set; } public PointsEventArgs(IEnumerable points) { NewPoints = points.ToList(); } } [Item("ScatterPlot", "A scatter plot of 2D data")] [StorableClass] public class ScatterPlot : NamedItem { public static new Image StaticItemImage { get { return VSImageLibrary.Image; } } public event EventHandler PixelsChanged; public event EventHandler AxisNameChanged; protected virtual void OnPixelsChanged(IEnumerable p) { EventHandler handler = PixelsChanged; if (handler != null) handler(this, new PointsEventArgs(p)); } protected virtual void OnAxesNameChanged() { EventHandler handler = AxisNameChanged; if (handler != null) handler(this, EventArgs.Empty); } private IEnumerable GetPointCoordinates() { foreach (var p in points) { yield return p.X; yield return p.Y; } } [Storable] private List PointCoordinateList { get { return new List(GetPointCoordinates()); } set { points = new HashSet(); var iter = value.GetEnumerator(); while (iter.MoveNext()) { float x = iter.Current; iter.MoveNext(); float y = iter.Current; points.Add(new PointF(x, y)); } } } private HashSet points; public IEnumerable Points { get { return points; } } [Storable] private string xAxisName; public string XAxisName { get { return xAxisName; } set { if (value == xAxisName) return; xAxisName = value; OnAxesNameChanged(); } } [Storable] private string yAxisName; public string YAxisName { get { return yAxisName; } set { if (value == yAxisName) return; yAxisName = value; OnAxesNameChanged(); } } public void Add(IEnumerable points) { List newPoints = new List(); foreach (var p in points) { if (this.points.Add(p)) newPoints.Add(p); } if (newPoints.Count > 0) OnPixelsChanged(newPoints); } [StorableConstructor] protected ScatterPlot(bool deserializing) : base(deserializing) { } protected ScatterPlot(ScatterPlot original, Cloner cloner) : base(original, cloner) { points = new HashSet(original.points.Select(p => new PointF(p.X, p.Y))); xAxisName = original.xAxisName; yAxisName = original.yAxisName; } public ScatterPlot() { points = new HashSet(); Name = "Scatter Plot"; xAxisName = "Fitness"; yAxisName = "Distance"; } public override IDeepCloneable Clone(Cloner cloner) { return new ScatterPlot(this, cloner); } internal void Clear() { var oldPoints = points; points = new HashSet(); OnPixelsChanged(oldPoints); } } }