Index: Core/Components/src/Core.Components.OxyPlot.Forms/ChartControl.cs =================================================================== diff -u -r343449d430f43342f74677ff37ade9332080f966 -r364dfcde1c00a33c19ab33f47373d862dae87067 --- Core/Components/src/Core.Components.OxyPlot.Forms/ChartControl.cs (.../ChartControl.cs) (revision 343449d430f43342f74677ff37ade9332080f966) +++ Core/Components/src/Core.Components.OxyPlot.Forms/ChartControl.cs (.../ChartControl.cs) (revision 364dfcde1c00a33c19ab33f47373d862dae87067) @@ -172,17 +172,19 @@ return CreateEnvelopeForAllVisibleLayers(collection); } - DrawnChartData drawnMapData = drawnChartDataList.FirstOrDefault(dmd => dmd.ChartData.Equals(chartData)); - if (drawnMapData == null) + DrawnChartData drawnChartData = drawnChartDataList.FirstOrDefault(dmd => dmd.ChartData.Equals(chartData)); + if (drawnChartData == null) { throw new ArgumentException($@"Can only zoom to {typeof(ChartData).Name} that is part of this {typeof(ChartControl).Name}s drawn {nameof(chartData)}.", nameof(chartData)); } - Extent extent = new Extent(); - if (drawnMapData.ChartData.IsVisible) + var extent = new Extent(); + + ChartData chartDataDrawn = drawnChartData.ChartData; + if (chartDataDrawn.IsVisible && chartDataDrawn.HasData) { - extent.ExpandToInclude(CreateExtentFor(drawnMapData.ChartDataSeries as XYAxisSeries)); + extent.ExpandToInclude(CreateExtentFor(drawnChartData.ChartDataSeries as XYAxisSeries)); } return extent; } @@ -286,9 +288,9 @@ private void RemoveRedundantChartDataOnCollectionChange(IEnumerable chartDataThatShouldBeDrawn, IDictionary drawnChartDataLookup) { - foreach (ChartData ChartData in drawnChartDataLookup.Keys.Except(chartDataThatShouldBeDrawn)) + foreach (ChartData chartData in drawnChartDataLookup.Keys.Except(chartDataThatShouldBeDrawn)) { - RemoveChartData(drawnChartDataLookup[ChartData]); + RemoveChartData(drawnChartDataLookup[chartData]); } } @@ -297,9 +299,9 @@ { plotView.Model.Series.Clear(); - foreach (ChartData ChartData in chartDataThatShouldBeDrawn) + foreach (ChartData chartData in chartDataThatShouldBeDrawn) { - plotView.Model.Series.Add((Series) drawnChartDataLookup[ChartData].ChartDataSeries); + plotView.Model.Series.Add((Series) drawnChartDataLookup[chartData].ChartDataSeries); } } @@ -313,21 +315,21 @@ private void DrawInitialChartData() { - foreach (ChartData ChartData in GetChartDataRecursively(Data)) + foreach (ChartData chartData in GetChartDataRecursively(Data)) { - DrawChartData(ChartData); + DrawChartData(chartData); } plotView.InvalidatePlot(true); } - private void DrawChartData(ChartData ChartData) + private void DrawChartData(ChartData chartData) { - IChartDataSeries chartDataSeries = ChartDataSeriesFactory.Create(ChartData); + IChartDataSeries chartDataSeries = ChartDataSeriesFactory.Create(chartData); var drawnChartData = new DrawnChartData { - ChartData = ChartData, + ChartData = chartData, ChartDataSeries = chartDataSeries }; @@ -337,7 +339,7 @@ plotView.InvalidatePlot(true); }) { - Observable = ChartData + Observable = chartData }; drawnChartDataList.Add(drawnChartData); Index: Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs =================================================================== diff -u -r65dc82ce9614b05532c86968e3b72aea7764cf4d -r364dfcde1c00a33c19ab33f47373d862dae87067 --- Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision 65dc82ce9614b05532c86968e3b72aea7764cf4d) +++ Core/Components/test/Core.Components.DotSpatial.Forms.Test/MapControlTest.cs (.../MapControlTest.cs) (revision 364dfcde1c00a33c19ab33f47373d862dae87067) @@ -2227,7 +2227,7 @@ [Test] [Apartment(ApartmentState.STA)] - public void ZoomToAllVisibleLayers_MapInFormWithEmptyDataSet_ViewInvalidatedLayersSame() + public void ZoomToAllVisibleLayers_MapInFormWithEmptyDataSet_ViewNotInvalidatedLayersSame() { // Setup using (var map = new MapControl()) @@ -2336,60 +2336,6 @@ } [Test] - [TestCase(5.0, 5.0)] - [TestCase(5.0, 1.0)] - [TestCase(1.0, 5.0)] - [TestCase(double.MaxValue * 0.96, double.MaxValue * 0.96)] - [TestCase(double.MaxValue, double.MaxValue)] - public void ZoomToAllVisibleLayers_LayersOfVariousDimensions_ZoomToVisibleLayersExtent(double xMax, double yMax) - { - // Setup - using (var map = new MapControl()) - { - Map mapView = map.Controls.OfType().First(); - var mapDataCollection = new MapDataCollection("Test data"); - - mapDataCollection.Add(new MapPointData("Test data") - { - Features = new[] - { - new MapFeature(new[] - { - new MapGeometry(new[] - { - new[] - { - new Point2D(0.0, 0.0), - new Point2D(xMax, yMax) - } - }) - }) - } - }); - - map.Data = mapDataCollection; - - var expectedExtent = new Extent(0.0, 0.0, xMax, yMax); - ExtendWithExpectedMargin(expectedExtent); - - // Call - map.ZoomToAllVisibleLayers(); - - // Assert - if (double.IsInfinity(expectedExtent.Height) || double.IsInfinity(expectedExtent.Width)) - { - Assert.AreEqual(mapView.GetMaxExtent(), mapView.ViewExtents); - Assert.AreNotEqual(expectedExtent, mapView.ViewExtents); - } - else - { - Assert.AreNotEqual(mapView.GetMaxExtent(), mapView.ViewExtents); - Assert.AreEqual(expectedExtent, mapView.ViewExtents); - } - } - } - - [Test] [Apartment(ApartmentState.STA)] public void ZoomToAllVisibleLayers_WithNonChildMapData_ThrowArgumentException() { @@ -2414,7 +2360,7 @@ [Test] [Apartment(ApartmentState.STA)] - public void ZoomToAllVisibleLayers_MapInFormWithEmptyDataSetAndForChildMapData_ViewInvalidatedLayersSame() + public void ZoomToAllVisibleLayers_MapInFormWithEmptyDataSetAndForChildMapData_ViewNotInvalidatedLayersSame() { // Setup using (var map = new MapControl()) @@ -2535,8 +2481,8 @@ map.Data = dataCollection; MapData mapData = dataCollection.Collection.ElementAt(2); - Extent expectedExtent = GetExpectedExtent((FeatureBasedMapData) mapData); - ExtendWithExpectedMargin(expectedExtent); + Extent unexpectedExtent = GetExpectedExtent((FeatureBasedMapData) mapData); + ExtendWithExpectedMargin(unexpectedExtent); // Precondition Assert.IsFalse(mapData.IsVisible); @@ -2547,7 +2493,7 @@ map.ZoomToAllVisibleLayers(mapData); // Assert - Assert.AreNotEqual(expectedExtent, mapView.ViewExtents, + Assert.AreNotEqual(unexpectedExtent, mapView.ViewExtents, "Do not set extent based on the invisible layer."); Assert.AreEqual(originalViewExtents, mapView.ViewExtents); } Index: Core/Components/test/Core.Components.OxyPlot.Forms.Test/ChartControlTest.cs =================================================================== diff -u -rc349a104ffe6e2b39878cd0169cace7eea4fddb6 -r364dfcde1c00a33c19ab33f47373d862dae87067 --- Core/Components/test/Core.Components.OxyPlot.Forms.Test/ChartControlTest.cs (.../ChartControlTest.cs) (revision c349a104ffe6e2b39878cd0169cace7eea4fddb6) +++ Core/Components/test/Core.Components.OxyPlot.Forms.Test/ChartControlTest.cs (.../ChartControlTest.cs) (revision 364dfcde1c00a33c19ab33f47373d862dae87067) @@ -19,16 +19,20 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Threading; using System.Windows.Forms; using Core.Common.Base.Geometry; +using Core.Common.TestUtil; using Core.Common.Utils.Reflection; using Core.Components.Charting.Data; using Core.Components.Charting.Forms; using NUnit.Framework; using OxyPlot; +using OxyPlot.Axes; using OxyPlot.Series; using OxyPlot.WindowsForms; @@ -323,12 +327,95 @@ } [Test] - public void ZoomToAll_ChartInForm_ViewInvalidatedSeriesSame() + [TestCase("Title")] + [TestCase("Test")] + [TestCase("Label")] + public void BottomAxisTitle_Always_SetsNewTitleToBottomAxis(string newTitle) { // Setup using (var form = new Form()) { var chart = new ChartControl(); + var view = TypeUtils.GetField(chart, "plotView"); + form.Controls.Add(chart); + + form.Show(); + + var invalidated = 0; + view.Invalidated += (sender, args) => invalidated++; + + // Call + chart.BottomAxisTitle = newTitle; + + // Assert + Assert.AreEqual(chart.BottomAxisTitle, newTitle); + Assert.AreEqual(1, invalidated); + } + } + + [Test] + [TestCase("Title")] + [TestCase("Test")] + [TestCase("Label")] + public void SetLeftAxisTitle_Always_SetsNewTitleToLeftAxis(string newTitle) + { + // Setup + using (var form = new Form()) + { + var chart = new ChartControl(); + var view = TypeUtils.GetField(chart, "plotView"); + form.Controls.Add(chart); + + form.Show(); + + var invalidated = 0; + view.Invalidated += (sender, args) => invalidated++; + + // Call + chart.LeftAxisTitle = newTitle; + + // Assert + Assert.AreEqual(chart.LeftAxisTitle, newTitle); + Assert.AreEqual(1, invalidated); + } + } + + [Test] + [TestCase("Title")] + [TestCase("Test")] + [TestCase("Label")] + public void SetModelTitle_Always_SetsNewTitleToModelAndViewInvalidated(string newTitle) + { + // Setup + using (var form = new Form()) + { + var chart = new ChartControl(); + var view = TypeUtils.GetField(chart, "plotView"); + form.Controls.Add(chart); + + form.Show(); + + var invalidated = 0; + view.Invalidated += (sender, args) => invalidated++; + + // Call + chart.ChartTitle = newTitle; + + // Assert + Assert.AreEqual(chart.ChartTitle, newTitle); + Assert.AreEqual(1, invalidated); + } + } + + #region ZoomToAllVisibleLayers + + [Test] + public void ZoomToAllVisibleLayers_ChartInForm_ViewInvalidatedSeriesSame() + { + // Setup + using (var form = new Form()) + { + var chart = new ChartControl(); var testData = new ChartLineData("test data") { Points = new[] @@ -362,7 +449,7 @@ } [Test] - public void ZoomToAll_ChartWithoutDataInForm_ViewNotInvalidated() + public void ZoomToAllVisibleLayers_ChartWithoutDataInForm_ViewNotInvalidated() { // Setup using (var form = new Form()) @@ -395,84 +482,259 @@ } [Test] - [TestCase("Title")] - [TestCase("Test")] - [TestCase("Label")] - public void BottomAxisTitle_Always_SetsNewTitleToBottomAxis(string newTitle) + public void ZoomToAllVisibleLayers_NotAllLayersVisible_ZoomToVisibleLayersExtent() { // Setup using (var form = new Form()) { var chart = new ChartControl(); var view = TypeUtils.GetField(chart, "plotView"); form.Controls.Add(chart); - form.Show(); - var invalidated = 0; - view.Invalidated += (sender, args) => invalidated++; + var collection = new ChartDataCollection("collection"); + collection.Add(new ChartLineData("test data") + { + Points = new[] + { + new Point2D(1, 5), + new Point2D(4, 3) + } + }); + collection.Add(new ChartPointData("test data") + { + Points = new[] + { + new Point2D(8, 2), + new Point2D(1, 1), + new Point2D(1, 4) + } + }); + collection.Add(new ChartAreaData("test data") + { + IsVisible = false, + Points = new[] + { + new Point2D(1, 2), + new Point2D(2, 3), + new Point2D(3, 3), + new Point2D(2, -1) + } + }); + chart.Data = collection; + chart.Update(); + var expectedExtent = new Extent(1 - 0.07, 8 + 0.07, 1 - 0.04, 5 + 0.04); + + // Precondition + Assert.AreEqual(3, view.Model.Series.Count, "Precondition failed: expected 3 series."); + Assert.IsFalse(view.Model.Series.All(l => l.IsVisible), "Precondition failed: not all series should be visible."); + // Call - chart.BottomAxisTitle = newTitle; + chart.ZoomToAllVisibleLayers(); // Assert - Assert.AreEqual(chart.BottomAxisTitle, newTitle); - Assert.AreEqual(1, invalidated); + AssertExpectedExtent(view.Model.Axes, expectedExtent); } } + private void AssertExpectedExtent(ElementCollection modelAxes, Extent expectedExtent) + { + const double accuracy = 1e-8; + Assert.AreEqual(expectedExtent.XMin, modelAxes[0].ActualMinimum, Math.Abs(expectedExtent.XMin * accuracy)); + Assert.AreEqual(expectedExtent.XMax, modelAxes[0].ActualMaximum, Math.Abs(expectedExtent.XMax * accuracy)); + Assert.AreEqual(expectedExtent.YMin, modelAxes[1].ActualMinimum, Math.Abs(expectedExtent.YMin * accuracy)); + Assert.AreEqual(expectedExtent.YMax, modelAxes[1].ActualMaximum, Math.Abs(expectedExtent.YMax * accuracy)); + } + [Test] - [TestCase("Title")] - [TestCase("Test")] - [TestCase("Label")] - public void SetLeftAxisTitle_Always_SetsNewTitleToLeftAxis(string newTitle) + [TestCase(5.0, 5.0)] + [TestCase(45.3, 1.0)] + [TestCase(1.0, 122.9)] + [TestCase(double.MaxValue * 0.55, double.MaxValue * 0.55)] + public void ZoomToAllVisibleLayers_LayersOfVariousDimensions_ZoomToVisibleLayersExtent(double xMax, double yMax) { // Setup using (var form = new Form()) { var chart = new ChartControl(); var view = TypeUtils.GetField(chart, "plotView"); form.Controls.Add(chart); + form.Show(); + var collection = new ChartDataCollection("collection"); + collection.Add(new ChartLineData("test data") + { + IsVisible = true, + Points = new[] + { + new Point2D(0.0, 0.0), + new Point2D(xMax, yMax) + } + }); + + chart.Data = collection; + chart.Update(); + + double xMargin = xMax * 0.01; + double yMargin = yMax * 0.01; + var expectedExtent = new Extent(-xMargin, xMax + xMargin, -yMargin, yMax + yMargin); + + // Call + chart.ZoomToAllVisibleLayers(); + + // Assert + AssertExpectedExtent(view.Model.Axes, expectedExtent); + } + } + + [Test] + [Apartment(ApartmentState.STA)] + public void ZoomToAllVisibleLayers_WithNonChildChartData_ThrowArgumentException() + { + // Setup + using (var chart = new ChartControl()) + { + var chartDataCollection = new ChartDataCollection("Collection"); + var chartData = new ChartLineData("Test data"); + + chart.Data = chartDataCollection; + chart.Update(); + + // Call + TestDelegate call = () => chart.ZoomToAllVisibleLayers(chartData); + + // Assert + const string message = "Can only zoom to ChartData that is part of this ChartControls drawn chartData."; + string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message).ParamName; + Assert.AreEqual("chartData", paramName); + } + } + + [Test] + [Apartment(ApartmentState.STA)] + public void ZoomToAllVisibleLayers_ChartInFormWithEmptyDataSetAndZoomChildChartData_ViewNotInvalidated() + { + // Setup + using (var form = new Form()) + { + var chart = new ChartControl(); + var view = TypeUtils.GetField(chart, "plotView"); + form.Controls.Add(chart); form.Show(); + var chartDataCollection = new ChartDataCollection("Collection"); + var chartData = new ChartPointData("Test data"); var invalidated = 0; - view.Invalidated += (sender, args) => invalidated++; + chartDataCollection.Add(chartData); + + chart.Data = chartDataCollection; + chart.Update(); + + var expectedExtent = new Extent( + view.Model.Axes[0].ActualMinimum, + view.Model.Axes[0].ActualMaximum, + view.Model.Axes[1].ActualMinimum, + view.Model.Axes[1].ActualMaximum); + + view.Invalidated += (sender, args) => { invalidated++; }; + // Call - chart.LeftAxisTitle = newTitle; + chart.ZoomToAllVisibleLayers(chartData); // Assert - Assert.AreEqual(chart.LeftAxisTitle, newTitle); - Assert.AreEqual(1, invalidated); + Assert.AreEqual(0, invalidated); + AssertExpectedExtent(view.Model.Axes, expectedExtent); } } [Test] - [TestCase("Title")] - [TestCase("Test")] - [TestCase("Label")] - public void SetModelTitle_Always_SetsNewTitleToModelAndViewInvalidated(string newTitle) + [Apartment(ApartmentState.STA)] + public void ZoomToAllVisibleLayers_ChartInFormForChildChartData_ViewInvalidatedLayersSame() { // Setup using (var form = new Form()) { var chart = new ChartControl(); var view = TypeUtils.GetField(chart, "plotView"); form.Controls.Add(chart); - form.Show(); + var chartDataCollection = new ChartDataCollection("Collection"); + var chartData = new ChartLineData("test data") + { + IsVisible = true, + Points = new[] + { + new Point2D(0.0, 0.0), + new Point2D(1.0, 1.0) + } + }; + chartDataCollection.Add(chartData); var invalidated = 0; - view.Invalidated += (sender, args) => invalidated++; + chart.Data = chartDataCollection; + chart.Update(); + + view.Invalidated += (sender, args) => { invalidated++; }; + // Call - chart.ChartTitle = newTitle; + chart.ZoomToAllVisibleLayers(chartData); // Assert - Assert.AreEqual(chart.ChartTitle, newTitle); Assert.AreEqual(1, invalidated); + + var expectedExtent = new Extent( + -0.01, + 1.01, + -0.01, + 1.01); + + AssertExpectedExtent(view.Model.Axes, expectedExtent); } } + + [Test] + public void ZoomToAllVisibleLayers_ForInvisibleChildChartData_DoNotChangeViewExtentsOfChartView() + { + // Setup + using (var form = new Form()) + { + var chart = new ChartControl(); + var view = TypeUtils.GetField(chart, "plotView"); + form.Controls.Add(chart); + form.Show(); + + var chartDataCollection = new ChartDataCollection("Collection"); + var chartData = new ChartLineData("test data") + { + IsVisible = false, + Points = new[] + { + new Point2D(3.2, 4.1), + new Point2D(11.2, 5.8) + } + }; + + chartDataCollection.Add(chartData); + chart.Data = chartDataCollection; + chart.Update(); + + var expectedExtent = new Extent( + view.Model.Axes[0].ActualMinimum, + view.Model.Axes[0].ActualMaximum, + view.Model.Axes[1].ActualMinimum, + view.Model.Axes[1].ActualMaximum); + + // Call + chart.ZoomToAllVisibleLayers(chartData); + + // Assert + AssertExpectedExtent(view.Model.Axes, expectedExtent); + } + } + + #endregion } } \ No newline at end of file Index: Core/Components/test/Core.Components.OxyPlot.Forms.Test/Core.Components.OxyPlot.Forms.Test.csproj =================================================================== diff -u -r6a5d7b40b7ba4dcb73e393075338352d194e97c2 -r364dfcde1c00a33c19ab33f47373d862dae87067 --- Core/Components/test/Core.Components.OxyPlot.Forms.Test/Core.Components.OxyPlot.Forms.Test.csproj (.../Core.Components.OxyPlot.Forms.Test.csproj) (revision 6a5d7b40b7ba4dcb73e393075338352d194e97c2) +++ Core/Components/test/Core.Components.OxyPlot.Forms.Test/Core.Components.OxyPlot.Forms.Test.csproj (.../Core.Components.OxyPlot.Forms.Test.csproj) (revision 364dfcde1c00a33c19ab33f47373d862dae87067) @@ -80,6 +80,10 @@ {F49BD8B2-332A-4C91-A196-8CCE0A2C7D98} Core.Common.Utils + + {D749EE4C-CE50-4C17-BF01-9A953028C126} + Core.Common.TestUtil + {2465cca1-c505-4827-9454-4fd5fd9194cd} Core.Components.Charting.Forms