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 <vtkDataRepresentation.h>
#include <vtkNew.h>
#include <vtkRegressionTestImage.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.3 FATAL_ERROR)

project(TreeMapView)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkIOInfovis
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  vtkTestingRendering
  vtkViewsCore
  vtkViewsInfovis
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping TreeMapView: ${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(TreeMapView MACOSX_BUNDLE TreeMapView.cxx )
  target_link_libraries(TreeMapView PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  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}
    )
endif ()

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.