Skip to content

ScalarBarActor

vtk-examples/Cxx/Visualization/ScalarBarActor


Description

This example demonstrates how to make a color bar to that reflects the range of values associated with a data set.

Other languages

See (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ScalarBarActor.cxx

#include <vtkActor.h>
#include <vtkFloatArray.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkScalarBarActor.h>
#include <vtkSphereSource.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create a sphere for some geometry
  vtkNew<vtkSphereSource> sphere;
  sphere->SetCenter(0, 0, 0);
  sphere->SetRadius(1);
  sphere->Update();

  // Create scalar data to associate with the vertices of the sphere
  int numPts = sphere->GetOutput()->GetPoints()->GetNumberOfPoints();
  vtkNew<vtkFloatArray> scalars;
  scalars->SetNumberOfValues(numPts);
  for (int i = 0; i < numPts; ++i)
  {
    scalars->SetValue(i, static_cast<float>(i) / numPts);
  }
  vtkNew<vtkPolyData> poly;
  poly->DeepCopy(sphere->GetOutput());
  poly->GetPointData()->SetScalars(scalars);

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(poly);
  mapper->ScalarVisibilityOn();
  mapper->SetScalarModeToUsePointData();
  mapper->SetColorModeToMapScalars();

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkScalarBarActor> scalarBar;
  scalarBar->SetLookupTable(mapper->GetLookupTable());
  scalarBar->SetTitle("Title");
  scalarBar->SetNumberOfLabels(4);

  // Create a lookup table to share between the mapper and the scalarbar
  vtkNew<vtkLookupTable> hueLut;
  hueLut->SetTableRange(0, 1);
  hueLut->SetHueRange(0, 1);
  hueLut->SetSaturationRange(1, 1);
  hueLut->SetValueRange(1, 1);
  hueLut->Build();

  mapper->SetLookupTable(hueLut);
  scalarBar->SetLookupTable(hueLut);

  // Create a renderer and render window
  vtkNew<vtkRenderer> renderer;

  renderer->GradientBackgroundOn();
  renderer->SetBackground(colors->GetColor3d("Indigo").GetData());
  renderer->SetBackground2(colors->GetColor3d("LightBlue").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("ScalarBarActor");

  // Create an interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene
  renderer->AddActor(actor);
  renderer->AddActor2D(scalarBar);

  // Render the scene (lights and cameras are created automatically)
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ScalarBarActor)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingAnnotation
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ScalarBarActor: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(ScalarBarActor MACOSX_BUNDLE ScalarBarActor.cxx )
  target_link_libraries(ScalarBarActor PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ScalarBarActor MACOSX_BUNDLE ScalarBarActor.cxx )
  target_link_libraries(ScalarBarActor PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ScalarBarActor
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ScalarBarActor

Click here to download ScalarBarActor and its CMakeLists.txt file. Once the tarball ScalarBarActor.tar has been downloaded and extracted,

cd ScalarBarActor/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ScalarBarActor

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.