using System; using System.Collections.Generic; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HeuristicLab.MainForm; using System.Windows.Forms.DataVisualization.Charting; using HeuristicLab.Encodings.RealVectorEncoding; namespace HeuristicLab.Problems.MultiObjectiveTestFunctions.Drawings { [View("Scatter Plot")] [Content(typeof(IMOQualities))] public partial class MOQualitiesScatterPlotView : MOQualitiesView { private const string QUALITIES = "Qualities"; private const string PARETO_FRONT = "Best Known Pareto Front"; //private const string TEST_SERIES = "Test samples"; private int xDim = 0; private int yDim = 1; int objectives = -1; bool optimalKnown = true; public new IMOQualities Content { get { return (IMOQualities)base.Content; } set { base.Content = value; } } public MOQualitiesScatterPlotView() : base() { InitializeComponent(); this.chart.Series.Add(QUALITIES); this.chart.Series[QUALITIES].LegendText = QUALITIES; this.chart.Series[QUALITIES].ChartType = SeriesChartType.FastPoint; this.chart.Series.Add(PARETO_FRONT); this.chart.Series[PARETO_FRONT].LegendText = PARETO_FRONT; this.chart.Series[PARETO_FRONT].ChartType = SeriesChartType.FastPoint; this.chart.TextAntiAliasingQuality = TextAntiAliasingQuality.High; this.chart.AxisViewChanged += new EventHandler(chart_AxisViewChanged); //configure axis this.chart.CustomizeAllChartAreas(); this.chart.ChartAreas[0].AxisX.Title = "XAxis: Objective " + xDim; this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true; this.chart.ChartAreas[0].CursorX.Interval = 1; this.chart.ChartAreas[0].CursorY.Interval = 1; this.chart.ChartAreas[0].AxisY.Title = "YAxis: Objective " + yDim; this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true; this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true; this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true; } protected override void RegisterContentEvents() { base.RegisterContentEvents(); this.chart.GetToolTipText += new System.EventHandler(this.Chart_GetToolTipText); } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); this.chart.GetToolTipText -= new System.EventHandler(this.Chart_GetToolTipText); } private void Chart_GetToolTipText(object sender, ToolTipEventArgs e) { // Check selected chart element and set tooltip text if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { int i = e.HitTestResult.PointIndex; StringBuilder toolTippText = new StringBuilder(); DataPoint qp = e.HitTestResult.Series.Points[i]; toolTippText.Append("Objective " + xDim + " = " + qp.XValue+ "\n"); toolTippText.Append("Objective " + yDim + " = " + qp.YValues[0]); Series s = e.HitTestResult.Series; if (s.Equals(this.chart.Series[QUALITIES])) { RealVector dp = Content.Solutions[i].RealVector(); toolTippText.Append("\nSolution: {"); for (int j = 0; j < dp.Length; j++) { toolTippText.Append(dp[j]); toolTippText.Append(";"); } toolTippText.Remove(toolTippText.Length - 1, 1); toolTippText.Append("}"); e.Text = toolTippText.ToString(); } } } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) return; if (objectives != Content.Objectives) { AddMenuItems(); objectives = Content.Objectives; } if(Content.ParetoFront == null && optimalKnown) { Series s = this.chart.Series[PARETO_FRONT]; this.chart.Series.Remove(s); optimalKnown = false; } else if(Content.ParetoFront != null && !optimalKnown) { this.chart.Series.Add(PARETO_FRONT); this.chart.Series[PARETO_FRONT].LegendText = PARETO_FRONT; this.chart.Series[PARETO_FRONT].ChartType = SeriesChartType.FastPoint; optimalKnown = true; } UpdateChart(); } private void UpdateChart() { if (InvokeRequired) Invoke((Action)UpdateChart); else { if (Content != null) { this.UpdateSeries(); if (!this.chart.Series.Any(s => s.Points.Count > 0)) this.ClearChart(); } } } private void UpdateCursorInterval() { var estimatedValues = this.chart.Series[QUALITIES].Points.Select(x => x.XValue).DefaultIfEmpty(1.0); var targetValues = this.chart.Series[QUALITIES].Points.Select(x => x.YValues[0]).DefaultIfEmpty(1.0); double estimatedValuesRange = estimatedValues.Max() - estimatedValues.Min(); double targetValuesRange = targetValues.Max() - targetValues.Min(); double interestingValuesRange = Math.Min(Math.Max(targetValuesRange, 1.0), Math.Max(estimatedValuesRange, 1.0)); double digits = (int)Math.Log10(interestingValuesRange) - 3; double zoomInterval = Math.Max(Math.Pow(10, digits), 10E-5); this.chart.ChartAreas[0].CursorX.Interval = zoomInterval; this.chart.ChartAreas[0].CursorY.Interval = zoomInterval; this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = zoomInterval; this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = zoomInterval; this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number; this.chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = zoomInterval; this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Number; this.chart.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = zoomInterval; if (digits < 0) { this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F" + (int)Math.Abs(digits); this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F" + (int)Math.Abs(digits); } else { this.chart.ChartAreas[0].AxisX.LabelStyle.Format = "F0"; this.chart.ChartAreas[0].AxisY.LabelStyle.Format = "F0"; } } private double max = 1; private double min = 0; private void UpdateSeries() { if (InvokeRequired) Invoke((Action)UpdateSeries); else { this.max = 1; this.min = 0; if (this.chart.Series[QUALITIES].Points.Count > 0 ) { fillSeries(Content.Qualities, this.chart.Series[QUALITIES]); } if (optimalKnown && this.chart.Series[PARETO_FRONT].Points.Count > 0) { fillSeries(Content.ParetoFront, this.chart.Series[PARETO_FRONT]); } // double max = this.max; // double min = this.min; max = max + 0.2 * Math.Abs(max); min = min - 0.2 * Math.Abs(min); double interestingValuesRange = max - min; int digits = Math.Max(0, 3 - (int)Math.Log10(interestingValuesRange)); max = Math.Round(max, digits); min = Math.Round(min, digits); this.chart.ChartAreas[0].AxisX.Maximum = max; this.chart.ChartAreas[0].AxisX.Minimum = min; this.chart.ChartAreas[0].AxisY.Maximum = max; this.chart.ChartAreas[0].AxisY.Minimum = min; UpdateCursorInterval(); } } private void ClearChart() { this.chart.Series[QUALITIES].Points.Clear(); if(optimalKnown) this.chart.Series[PARETO_FRONT].Points.Clear(); } private void ToggleSeriesData(Series series) { if (series.Points.Count > 0) { //checks if series is shown series.Points.Clear(); } else if (Content != null) { switch (series.Name) { case PARETO_FRONT: fillSeries(Content.ParetoFront, this.chart.Series[PARETO_FRONT]); break; case QUALITIES: fillSeries(Content.Qualities, this.chart.Series[QUALITIES]); break; } } } private void chart_MouseDown(object sender, MouseEventArgs e) { HitTestResult result = chart.HitTest(e.X, e.Y); if (result.ChartElementType == ChartElementType.LegendItem) { this.ToggleSeriesData(result.Series); } } 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_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) { this.chart.ChartAreas[0].AxisX.ScaleView.Size = e.NewSize; this.chart.ChartAreas[0].AxisY.ScaleView.Size = e.NewSize; } private void chart_CustomizeLegend(object sender, CustomizeLegendEventArgs e) { e.LegendItems[0].Cells[1].ForeColor = this.chart.Series[QUALITIES].Points.Count == 0 ? Color.Gray : Color.Black; if (optimalKnown) { e.LegendItems[1].Cells[1].ForeColor = this.chart.Series[PARETO_FRONT].Points.Count == 0 ? Color.Gray : Color.Black; } // e.LegendItems[2].Cells[1].ForeColor = this.chart.Series[TEST_SERIES].Points.Count == 0 ? Color.Gray : Color.Black; } private void AddMenuItems() { chooseDimensionToolStripMenuItem.DropDownItems.Clear(); chooseYDimensionToolStripMenuItem.DropDownItems.Clear(); if (Content == null) { return; } for (int i = 0; i < Content.Objectives; i++) { //add Menu Points ToolStripMenuItem xItem = makeMenuItem("X", i); ToolStripMenuItem yItem = makeMenuItem("Y", i); ; xItem.Click += new System.EventHandler(this.XMenu_Click); yItem.Click += new System.EventHandler(this.YMenu_Click); chooseDimensionToolStripMenuItem.DropDownItems.Add(xItem); chooseYDimensionToolStripMenuItem.DropDownItems.Add(yItem); } } private ToolStripMenuItem makeMenuItem(String axis, int i) { ToolStripMenuItem xItem = new ToolStripMenuItem(); xItem.Name = "obj" + i; xItem.Size = new System.Drawing.Size(269, 38); xItem.Text = "Objective" + i; return xItem; } private void YMenu_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; yDim = Int32.Parse(item.Name.Remove(0, 3)); this.chooseYDimensionToolStripMenuItem.Text = "Objective" + yDim; this.chart.ChartAreas[0].AxisY.Title = "Objective " + yDim; UpdateChart(); } private void XMenu_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; xDim = Int32.Parse(item.Name.Remove(0, 3)); this.chooseDimensionToolStripMenuItem.Text = "Objective" + xDim; this.chart.ChartAreas[0].AxisX.Title = "Objective " + xDim; UpdateChart(); } private void fillSeries(IEnumerable data, Series series) { series.Points.Clear(); foreach (double[] d in data) { //Assumtion: Columnwise if (d[xDim] > max) max = d[xDim]; if (d[yDim] > max) max = d[yDim]; if (d[xDim] < min) min = d[xDim]; if (d[yDim] < min) min = d[yDim]; series.Points.AddXY(d[xDim], d[yDim]); } } } }