Skip to content

ColorVertexLabels

vtk-examples/Cxx/Graphs/ColorVertexLabels


Other languages

See (Python)

Question

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

Code

ColorVertexLabels.cxx

#include <vtkCamera.h>
#include <vtkDataSetAttributes.h>
#include <vtkGraphLayoutView.h>
#include <vtkIntArray.h>
#include <vtkMutableDirectedGraph.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderedGraphRepresentation.h>
#include <vtkRenderer.h>
#include <vtkTextProperty.h>

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

  // Create a graph
  vtkNew<vtkMutableDirectedGraph> graph;

  vtkIdType v1 = graph->AddVertex();
  vtkIdType v2 = graph->AddVertex();
  graph->AddEdge(v1, v2);

  // Create an array for the vertex labels
  vtkNew<vtkIntArray> vertexIDs;
  vertexIDs->SetNumberOfComponents(1);
  vertexIDs->SetName("VertexIDs");

  // Set the vertex labels
  vertexIDs->InsertNextValue(0);
  vertexIDs->InsertNextValue(1);

  // Add the array to the graph
  graph->GetVertexData()->AddArray(vertexIDs);

  vtkNew<vtkGraphLayoutView> graphLayoutView;
  graphLayoutView->AddRepresentationFromInput(graph);
  graphLayoutView->SetVertexLabelVisibility(true);
  dynamic_cast<vtkRenderedGraphRepresentation*>(
      graphLayoutView->GetRepresentation())
      ->GetVertexLabelTextProperty()
      ->SetColor(colors->GetColor3d("Red").GetData());
  graphLayoutView->SetLayoutStrategyToSimple2D();
  graphLayoutView->SetVertexLabelArrayName("VertexIDs");
  graphLayoutView->ResetCamera();
  graphLayoutView->GetRenderer()->GetActiveCamera()->Zoom(0.8);
  graphLayoutView->GetRenderWindow()->SetWindowName("ColorVertexLabels");
  graphLayoutView->Render();
  graphLayoutView->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ColorVertexLabels)

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

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

Download and Build ColorVertexLabels

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

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

./ColorVertexLabels

WINDOWS USERS

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