#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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers { [Item("ScatterPlotHelper", "Helper class for creating scatter plots.")] [StorableClass] public class ScatterPlotHelper : Item { [Storable] private ResultCollection resultsCol; [Storable] private string chartName; [Storable] private string xAxisTitle; [Storable] private string yAxisTitle; [Storable] public bool StoreHistory { get; set; } [Storable] public bool CreateDataTable { get; set; } [Storable] public DataTableHelper dataTableHelper; [Storable] private string lastRowName; [Storable] private ScatterPlot plot; public ScatterPlot Plot { get { return plot; } private set { plot = value; } } [StorableConstructor] private ScatterPlotHelper(bool deserializing) : base(deserializing) { } private ScatterPlotHelper(ScatterPlotHelper original, Cloner cloner) : base(original, cloner) { StoreHistory = original.StoreHistory; CreateDataTable = original.CreateDataTable; } public ScatterPlotHelper() : base() { StoreHistory = true; CreateDataTable = false; } public ScatterPlotHelper(bool storeHistory, bool createDataTable) : base() { StoreHistory = storeHistory; CreateDataTable = createDataTable; } public override IDeepCloneable Clone(Cloner cloner) { return new ScatterPlotHelper(this, cloner); } public void InitializePlot(ResultCollection results, string chartName, string xAxisTitle, string yAxisTitle) { if (CreateDataTable) { if (dataTableHelper == null) { dataTableHelper = new DataTableHelper(); } dataTableHelper.InitializeChart(results, chartName + " Chart", new string[] { yAxisTitle }); } if (!results.ContainsKey(chartName)) { this.chartName = chartName; this.resultsCol = results; this.xAxisTitle = xAxisTitle; this.yAxisTitle = yAxisTitle; Initialize(); lastRowName = null; results.Add(new Result(chartName, Plot)); if (StoreHistory) results.Add(new Result(chartName + " History", new ScatterPlotHistory())); } } public void AddPoint(string rowName, Point2D point) { if (!Plot.Rows.ContainsKey(rowName)) { if (StoreHistory) ((ScatterPlotHistory)resultsCol[chartName + " History"].Value).Add(Plot); if (CreateDataTable && lastRowName != null && lastRowName != string.Empty) { double avg = Plot.Rows[lastRowName].Points.Average(x => x.Y); dataTableHelper.AddPoint(avg); } Initialize(); resultsCol[chartName].Value = Plot; var points = new List>(); points.Add(point); ScatterPlotDataRow row = new ScatterPlotDataRow(rowName, null, points); row.VisualProperties.PointStyle = ScatterPlotDataRowVisualProperties.ScatterPlotDataRowPointStyle.Circle; row.VisualProperties.PointSize = 5; Plot.Rows.Add(row); lastRowName = rowName; } else { Plot.Rows[rowName].Points.Add(point); } } private void Initialize() { Plot = new ScatterPlot(chartName, null); Plot.VisualProperties.XAxisTitle = xAxisTitle; Plot.VisualProperties.YAxisTitle = yAxisTitle; } } }