diff --git a/Charts/Core/Testing/Cxx/CMakeLists.txt b/Charts/Core/Testing/Cxx/CMakeLists.txt index e6015f5148a5b3ea88689c8f6897596359d7ac89..a9c1f3da5692911ac72e2905bad9003962baefe2 100644 --- a/Charts/Core/Testing/Cxx/CMakeLists.txt +++ b/Charts/Core/Testing/Cxx/CMakeLists.txt @@ -57,6 +57,7 @@ vtk_add_test_cxx(vtkChartsCoreCxxTests tests TestChartDouble.cxx TestChartDoubleColors.cxx TestChartDoubleColorsOpaque.cxx + TestChartLogScaleUpdates.cxx TestChartMatrix.cxx TestChartMatrix2.cxx TestChartMatrix3.cxx diff --git a/Charts/Core/Testing/Cxx/TestChartLogScaleUpdates.cxx b/Charts/Core/Testing/Cxx/TestChartLogScaleUpdates.cxx new file mode 100644 index 0000000000000000000000000000000000000000..ce4063789fbfd3994c2067db4dff5cfa8f300e59 --- /dev/null +++ b/Charts/Core/Testing/Cxx/TestChartLogScaleUpdates.cxx @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen +// SPDX-License-Identifier: BSD-3-Clause + +#include "vtkAxis.h" +#include "vtkChartLegend.h" +#include "vtkChartXY.h" +#include "vtkContextView.h" +#include "vtkFloatArray.h" +#include "vtkPlot.h" +#include "vtkRenderWindow.h" +#include "vtkRenderWindowInteractor.h" +#include "vtkTable.h" + +//------------------------------------------------------------------------------ +// This unit test verifies that log scale can be turned on at a later time and +// also checks that updating other parameters after turning on log scale does +// not reset the bounds of the axis which uses log scale. +int TestChartLogScaleUpdates(int, char*[]) +{ + // Set up a 2D scene, add an XY chart to it + vtkNew<vtkContextView> view; + view->GetRenderWindow()->SetSize(400, 300); + vtkNew<vtkChartXY> chart; + view->GetScene()->AddItem(chart); + + // Create a table with some points in it... + vtkNew<vtkTable> table; + vtkNew<vtkFloatArray> arrX; + arrX->SetName("X Axis"); + table->AddColumn(arrX); + vtkNew<vtkFloatArray> arrY1; + arrY1->SetName("y=x"); + table->AddColumn(arrY1); + vtkNew<vtkFloatArray> arrY2; + arrY2->SetName("y=-x"); + table->AddColumn(arrY2); + // Test charting with a few more points... + int numPoints = 10; + float inc = 7.5 / (numPoints - 1); + table->SetNumberOfRows(numPoints); + for (int i = 0; i < numPoints; ++i) + { + float x = 1.0e-5 + i * inc; + table->SetValue(i, 0, x); + table->SetValue(i, 1, x); + table->SetValue(i, 2, -x); + } + chart->SetShowLegend(true); + chart->GetLegend()->SetHorizontalAlignment(vtkChartLegend::CENTER); + + // Add a bar plot + vtkPlot* bar = chart->AddPlot(vtkChart::BAR); + bar->SetInputData(table, 0, 1); + bar->SetColor(255, 0, 0, 255); + + // Add a line plot + vtkPlot* line = chart->AddPlot(vtkChart::LINE); + line->SetInputData(table, 0, 2); + line->SetColor(255, 0, 255, 255); + line->SetWidth(4.0); + + // Render the scene and compare the image to a reference image + view->GetRenderWindow()->SetMultiSamples(0); + view->GetInteractor()->Initialize(); + view->Render(); + + // turn on log scale. + double xRange[2] = { 1, -1 }; + arrX->GetRange(xRange); + // Initialize unscaled min/max to fit x axis data so that LogScaleActive is correctly brought + // up to date. + chart->GetAxis(vtkAxis::BOTTOM)->SetUnscaledMinimum(xRange[0]); + chart->GetAxis(vtkAxis::BOTTOM)->SetUnscaledMaximum(xRange[1]); + chart->GetAxis(vtkAxis::BOTTOM)->LogScaleOn(); + chart->GetAxis(vtkAxis::BOTTOM)->Update(); + chart->Update(); + chart->RecalculateBounds(); + view->Render(); + + // Change the line color to navy blue. + line->SetColor(0, 0, 255, 255); + chart->GetAxis(vtkAxis::BOTTOM)->SetCustomTickPositions(nullptr); + chart->RecalculateBounds(); + view->GetInteractor()->Start(); + + return EXIT_SUCCESS; +} diff --git a/Charts/Core/Testing/Data/Baseline/TestChartLogScaleUpdates.png.sha512 b/Charts/Core/Testing/Data/Baseline/TestChartLogScaleUpdates.png.sha512 new file mode 100644 index 0000000000000000000000000000000000000000..13b9669e2a423eb42adefb5c8c4e1f8cfc2f68b1 --- /dev/null +++ b/Charts/Core/Testing/Data/Baseline/TestChartLogScaleUpdates.png.sha512 @@ -0,0 +1 @@ +19a8ce4b51d64feae1faa4cc76d863fe5bb61fcb4ded1b12fb688db50102ee745f813777200163714e9a9c1c0d2b73e79a4d3163c1da96bdf8c654d044da7d08 diff --git a/Charts/Core/vtkPlotBar.cxx b/Charts/Core/vtkPlotBar.cxx index de43f2c25f469467c2ff1bb7f787dacf7ce42cbc..487b02fc1dc9b0504e7c3ed7874aa4204f3a191f 100644 --- a/Charts/Core/vtkPlotBar.cxx +++ b/Charts/Core/vtkPlotBar.cxx @@ -651,7 +651,7 @@ void vtkPlotBar::GetBounds(double bounds[4], bool unscaled) bounds[valuesHigh] = 0.0; } - if (unscaled) + if (!unscaled) { vtkAxis* axes[2]; axes[seriesLow / 2] = this->GetXAxis(); @@ -674,13 +674,13 @@ void vtkPlotBar::GetBounds(double bounds[4], bool unscaled) //------------------------------------------------------------------------------ void vtkPlotBar::GetBounds(double bounds[4]) { - this->GetBounds(bounds, false); + this->GetBounds(bounds, /*unscaled=*/false); } //------------------------------------------------------------------------------ void vtkPlotBar::GetUnscaledInputBounds(double bounds[4]) { - this->GetBounds(bounds, true); + this->GetBounds(bounds, /*unscaled=*/true); } //------------------------------------------------------------------------------ diff --git a/Documentation/release/dev/fix-vtkPlotBar-GetBounds.md b/Documentation/release/dev/fix-vtkPlotBar-GetBounds.md new file mode 100644 index 0000000000000000000000000000000000000000..206eacfd4f922a28f1d74c87335396b97e1194dc --- /dev/null +++ b/Documentation/release/dev/fix-vtkPlotBar-GetBounds.md @@ -0,0 +1,4 @@ +# Fix vtkPlotBar::GetBounds logic when log scale is enabled + +The `vtkPlotBar::GetBounds(double*, bool unscaled)` now correctly returns +unscaled bounds when `unscaled` is true and scaled bounds when `unscaled` is false.