#region License Information /* HeuristicLab * Copyright (C) 2002-2013 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, minMaxDataTableHelper; [Storable] private string lastRowName; [Storable] private bool trackMinMaxValues; [Storable] private bool compressData; [Storable] private double min; public double Min { get { return min; } set { min = value; } } [Storable] private double max; public double Max { get { return max; } set { max = value; } } [Storable] private ResultCollection Results; [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; min = original.min; max = original.max; compressData = original.compressData; chartName = original.chartName; chartName = original.chartName; xAxisTitle = original.xAxisTitle; yAxisTitle = original.yAxisTitle; } public ScatterPlotHelper() : base() { StoreHistory = true; CreateDataTable = false; min = double.MaxValue; max = double.MinValue; compressData = false; } public ScatterPlotHelper(bool storeHistory, bool createDataTable, bool trackMinMax = false, bool compress = false) : base() { StoreHistory = storeHistory; CreateDataTable = createDataTable; trackMinMaxValues = trackMinMax; compressData = compress; min = double.MaxValue; max = double.MinValue; } public override IDeepCloneable Clone(Cloner cloner) { return new ScatterPlotHelper(this, cloner); } public void InitializePlot(ResultCollection results, string chartName, string xAxisTitle, string yAxisTitle) { Results = results; if (CreateDataTable) { if (dataTableHelper == null) { dataTableHelper = new DataTableHelper(); minMaxDataTableHelper = 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; } public void CleanUp() { if (CreateDataTable && trackMinMaxValues) { minMaxDataTableHelper.InitializeChart(Results, chartName + " Scaled Chart", new string[] { yAxisTitle }); dataTableHelper.GetFirstDataRow().ForEach(x => minMaxDataTableHelper.AddPoint((x - min) / (max - min))); dataTableHelper.CleanUp(); //just use the scaled chart and remove the original if (compressData) { minMaxDataTableHelper.CleanUpAndCompressData(); } } if (CreateDataTable && compressData && !trackMinMaxValues) { dataTableHelper.CleanUpAndCompressData(); } if (!StoreHistory && CreateDataTable) { resultsCol[chartName].Value = null; resultsCol.Remove(chartName); plot = null; } min = double.MaxValue; max = double.MinValue; } } }