Skip to content

PieChartActor

Repository source: PieChartActor


Question

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

Code

PieChartActor.cxx

#include <vtkColorSeries.h>
#include <vtkDataObject.h>
#include <vtkFieldData.h>
#include <vtkFloatArray.h>
#include <vtkLegendBoxActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPieChartActor.h>
#include <vtkPoints.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTextProperty.h>

#include <map>

int main(int /*argc*/, char* /*argv*/[])
{
  typedef std::map<std::string, int> DataContainer;
  DataContainer movies;

  movies["Comedy"] = 27;
  movies["Action"] = 18;
  movies["Romance"] = 14;
  movies["Drama"] = 14;
  movies["Horror"] = 11;
  movies["Foreign"] = 8;
  movies["Scifi"] = 8;

  int numTuples = static_cast<int>(movies.size());

  vtkNew<vtkFloatArray> bitter;
  bitter->SetNumberOfTuples(numTuples);

  DataContainer::iterator m;
  int i = 0;
  for (m = movies.begin(); m != movies.end(); ++m)
  {
    bitter->SetTuple1(i++, m->second);
  }

  vtkNew<vtkDataObject> dobj;
  dobj->GetFieldData()->AddArray(bitter);

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkPieChartActor> actor;
  actor->SetInputData(dobj);
  actor->SetTitle("Movie Watching");
  actor->GetPositionCoordinate()->SetValue(0.05, 0.1, 0.0);
  actor->GetPosition2Coordinate()->SetValue(0.95, 0.85, 0.0);
  actor->GetProperty()->SetColor(0, 0, 0);
  actor->GetProperty()->SetLineWidth(2);
  actor->GetLabelTextProperty()->SetFontSize(18);
  actor->GetLegendActor()->SetNumberOfEntries(numTuples);

  vtkNew<vtkColorSeries> colorSeries;
  colorSeries->SetColorScheme(vtkColorSeries::BREWER_QUALITATIVE_PASTEL2);
  i = 0;
  for (m = movies.begin(); m != movies.end(); ++m)
  {
    vtkColor3ub rgb = colorSeries->GetColorRepeating(i);
    actor->SetPieceColor(i, static_cast<double>(rgb.GetRed()) / 255.0,
                         static_cast<double>(rgb.GetGreen()) / 255.0,
                         static_cast<double>(rgb.GetBlue()) / 255.0);
    actor->SetPieceLabel(i++, m->first.c_str());
  }
  actor->LegendVisibilityOn();
  // Set text colors (same as actor for backward compat with test).
  actor->GetTitleTextProperty()->SetColor(
      colors->GetColor3d("Banana").GetData());
  actor->GetTitleTextProperty()->SetFontSize(40);
  actor->GetLabelTextProperty()->SetColor(
      colors->GetColor3d("Bisque").GetData());
  actor->GetLabelTextProperty()->SetFontSize(24);

  // Create the RenderWindow, Renderer and both Actors.
  vtkNew<vtkRenderer> ren1;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  ren1->AddActor(actor);
  ren1->SetBackground(colors->GetColor3d("SlateGray").GetData());
  renWin->SetSize(1024, 512);
  renWin->SetWindowName("PieChartActor");

  // Render the image.
  renWin->Render();

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PieChartActor)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  InteractionStyle
  RenderingAnnotation
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PieChartActor: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(PieChartActor MACOSX_BUNDLE PieChartActor.cxx )
  target_link_libraries(PieChartActor PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS PieChartActor
  MODULES ${VTK_LIBRARIES}
)

Download and Build PieChartActor

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

cd PieChartActor/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:

./PieChartActor

WINDOWS USERS

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