# Diagram

VTKExamples/Cxx/Plotting/Diagram

### Code

Diagram.cxx

    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkSmartPointer.h>
    #include <vtkObjectFactory.h>
    #include <vtkContext2D.h>
    #include <vtkContextItem.h>
    #include <vtkContextActor.h>
    #include <vtkContextScene.h>
    #include <vtkPen.h>
    #include <vtkBrush.h>
    #include <vtkTextProperty.h>
    #include <vtkOpenGLContextDevice2D.h>
    #include <vtkStdString.h>

    #define VTK_CREATE(type, name) \
      vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

    //----------------------------------------------------------------------------
    class APIDiagram : public vtkContextItem
    {
    public:
      static APIDiagram *New();
      vtkTypeMacro(APIDiagram, vtkContextItem);
      // Paint event for the chart, called whenever the chart needs to be drawn
      virtual bool Paint(vtkContext2D *painter);
    };

    //----------------------------------------------------------------------------
    int main( int, char *[] )
    {
      // Set up a 2D chart actor, APIDiagram object andn add them to the renderer
      VTK_CREATE(vtkContextActor, actor);
      VTK_CREATE(APIDiagram, diagram);
      actor->GetScene()->AddItem(diagram);
      VTK_CREATE(vtkRenderer, renderer);
      renderer->SetBackground(.2, .3, .4);
      VTK_CREATE(vtkRenderWindow, renderWindow);
      renderWindow->SetSize(800, 600);
      renderWindow->AddRenderer(renderer);
      renderer->AddActor(actor);

      VTK_CREATE(vtkRenderWindowInteractor, interactor);
      interactor->SetRenderWindow(renderWindow);
      renderWindow->SetMultiSamples(0);
      renderWindow->Render();

      interactor->Start();

      return true;
    }

    // Make our new derived class to draw a diagram
    vtkStandardNewMacro(APIDiagram);
    // This function draws our API diagram
    bool APIDiagram::Paint(vtkContext2D *painter)
    {
      // Drawing a hard wired diagram 800x600 as a demonstration of the 2D API
      painter->GetTextProp()->SetVerticalJustificationToCentered();
      painter->GetTextProp()->SetJustificationToCentered();
      painter->GetTextProp()->SetColor(0.0, 0.0, 0.0);
      painter->GetTextProp()->SetFontSize(24);
      painter->GetPen()->SetColor(0, 0, 0);

      painter->GetBrush()->SetColor(100, 255, 100);
      painter->DrawRect(100, 50, 200, 100);
      painter->DrawString(200, 100, "OpenGL");

      painter->GetBrush()->SetColor(255, 100, 0);
      painter->DrawRect(300, 50, 200, 100);
      painter->DrawString(400, 100, "Others?");

      painter->GetBrush()->SetColor(100, 0, 255);
      painter->DrawRect(500, 50, 200, 100);
      painter->DrawString(600, 100, "Others?");

      painter->GetBrush()->SetColor(180, 180, 255);
      painter->DrawRect(100, 150, 600, 100);
      painter->DrawString(400, 200, "2D API");

      painter->GetBrush()->SetColor(255, 255, 180);
      painter->DrawRect(100, 250, 600, 200);
      painter->DrawString(400, 400, "Canvas API");

      painter->GetBrush()->SetColor(180, 255, 180);
      painter->DrawRect(100, 250, 300, 100);
      painter->DrawString(250, 300, "Point Mark");

      painter->GetBrush()->SetColor(255, 255, 255);
      painter->DrawRect(100, 450, 600, 100);
      painter->DrawString(400, 500, "Canvas View");

      return true;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(Diagram)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(Diagram MACOSX_BUNDLE Diagram.cxx )

    target_link_libraries(Diagram ${VTK_LIBRARIES})

### Download and Build Diagram

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

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

    ./Diagram

WINDOWS USERS

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