Skip to content

TreeMapView

vtk-examples/Cxx/InfoVis/TreeMapView


Description

Fix: use a much simpler tree and generate the tree in the code instead of reading files.

Question

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

Code

TreeMapView.cxx

#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTreeMapView.h>
#include <vtkViewTheme.h>
#include <vtkXMLTreeReader.h>

int main(int argc, char* argv[])
{
  if (argc < 3)
  {
    cout << "Usage: " << argv[0] << " treeFileName graphFileName" << std::endl;
    cout << "where: treeFileName is Infovis-XML-vtkclasses.xml and "
            "graphFileName is Infovis-XML-vtklibrary.xml"
         << endl;
    return EXIT_FAILURE;
  }

  std::string treeFileName(argv[1]);
  std::string graphFileName(argv[2]);

  // We need to put the graph and tree edges in different domains.
  vtkNew<vtkXMLTreeReader> reader1;
  reader1->SetFileName(treeFileName.c_str());
  reader1->SetEdgePedigreeIdArrayName("tree edge");
  reader1->GenerateVertexPedigreeIdsOff();
  reader1->SetVertexPedigreeIdArrayName("id");

  vtkNew<vtkXMLTreeReader> reader2;
  reader2->SetFileName(graphFileName.c_str());
  reader2->SetEdgePedigreeIdArrayName("graph edge");
  reader2->GenerateVertexPedigreeIdsOff();
  reader2->SetVertexPedigreeIdArrayName("id");

  reader1->Update();
  reader2->Update();

  vtkNew<vtkTreeMapView> view;
  view->DisplayHoverTextOff();
  view->SetGraphFromInputConnection(reader1->GetOutputPort());
  view->SetTreeFromInputConnection(reader2->GetOutputPort());

  view->SetAreaColorArrayName("level");
  view->SetEdgeColorToSplineFraction();
  view->SetColorEdges(true);
  view->SetAreaLabelArrayName("id");
  view->SetAreaHoverArrayName("id");
  view->SetAreaLabelVisibility(true);
  view->SetAreaSizeArrayName("VertexDegree");

  // Apply a theme to the views
  // vtkViewTheme* const theme = vtkViewTheme::CreateMellowTheme();
  // view->ApplyViewTheme(theme);
  // theme->Delete();
  vtkNew<vtkViewTheme> theme;
  view->ApplyViewTheme(theme->CreateMellowTheme());

  view->GetRenderWindow()->SetMultiSamples(0);
  view->GetRenderWindow()->SetAlphaBitPlanes(1);
  view->Update();
  view->ResetCamera();

  view->GetRenderWindow()->SetSize(600, 600);
  view->GetRenderWindow()->SetWindowName("TreeMapView");
  view->GetRenderWindow()->Render();
  view->GetInteractor()->Initialize();
  view->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TreeMapView)

find_package(VTK COMPONENTS 
  CommonCore
  IOInfovis
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  ViewsCore
  ViewsInfovis
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "TreeMapView: 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(TreeMapView MACOSX_BUNDLE TreeMapView.cxx )
  target_link_libraries(TreeMapView PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS TreeMapView
  MODULES ${VTK_LIBRARIES}
)

Download and Build TreeMapView

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

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

./TreeMapView

WINDOWS USERS

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