Skip to content

BoxChart

Repository source: BoxChart


Description

This example demonstrates how to generate a box plot.

The example uses data from the Michelson Morley experiment. Compare the wiki example output with the Wikipedia results

Warning

The ChartBox expects an input table with exactly 5 rows per column. The 5 rows are minimum, 1st quartile, median, 3rd quartile and maximum. vtkComputeQuartiles is used to create a valid input table from the original data.

Other languages

See (PythonicAPI)

Question

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

Code

BoxChart.cxx

#include <vtkAxis.h>
#include <vtkChartBox.h>
#include <vtkComputeQuartiles.h>
#include <vtkContextView.h>
#include <vtkIntArray.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlotBox.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStatisticsAlgorithm.h>
#include <vtkStringArray.h>
#include <vtkTable.h>
#include <vtkTextProperty.h>

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

  // Set up a 2D scene, add an XY chart to it.
  vtkNew<vtkContextView> view;
  view->GetRenderWindow()->SetSize(400, 400);
  view->GetRenderWindow()->SetMultiSamples(0);

  vtkNew<vtkChartBox> chart;
  view->GetScene()->AddItem(chart);

  // Creates a vtkPlotBox input table.
  int numParam = 5;
  vtkNew<vtkTable> inputBoxPlotTable;

  for (int i = 0; i < numParam; i++)
  {
    char num[10];
    sprintf(num, "Run %d", i + 1);
    vtkNew<vtkIntArray> arrIndex;
    arrIndex->SetName(num);
    inputBoxPlotTable->AddColumn(arrIndex);
  }

  inputBoxPlotTable->SetNumberOfRows(20);
  double values[20][5] = {
      {850, 960, 880, 890, 890},  {740, 940, 880, 810, 840},
      {900, 960, 880, 810, 780},  {1070, 940, 860, 820, 810},
      {930, 880, 720, 800, 760},  {850, 800, 720, 770, 810},
      {950, 850, 620, 760, 790},  {980, 880, 860, 740, 810},
      {980, 900, 970, 750, 820},  {880, 840, 950, 760, 850},
      {1000, 830, 880, 910, 870}, {980, 790, 910, 920, 870},
      {930, 810, 850, 890, 810},  {650, 880, 870, 860, 740},
      {760, 880, 840, 880, 810},  {810, 830, 840, 720, 940},
      {1000, 800, 850, 840, 950}, {1000, 790, 840, 850, 800},
      {960, 760, 840, 850, 810},  {960, 800, 840, 780, 870}};
  for (int j = 0; j < 20; ++j)
  {
    for (int i = 0; i < 5; ++i)
    {
      inputBoxPlotTable->SetValue(j, i, values[j][i]);
    }
  }
  vtkNew<vtkComputeQuartiles> quartiles;
  quartiles->SetInputData(vtkStatisticsAlgorithm::INPUT_DATA,
                          inputBoxPlotTable);
  quartiles->Update();

  vtkTable* outTable = quartiles->GetOutput();
  vtkNew<vtkLookupTable> lookup;
  lookup->SetNumberOfColors(5);
  lookup->SetRange(0, 4);
  lookup->Build();

  chart->GetPlot(0)->SetInputData(outTable);
  chart->SetShowLegend(true);
  chart->SetColumnVisibilityAll(true);
  chart->SetTitle("Michelson-Morley experiment");
  chart->GetTitleProperties()->SetFontSize(16);
  chart->GetYAxis()->SetTitle("Speed of Light (km/s - 299000)");

  // Set the labels.
  vtkNew<vtkStringArray> labels;
  labels->SetNumberOfValues(numParam);
  for (int i = 0; i < numParam; i++)
  {
    char num[10];
    sprintf(num, "Run %d", i + 1);
    labels->SetValue(i, num);
  }

  chart->GetPlot(0)->SetLabels(labels);

  // Render the scene.
  view->GetRenderWindow()->SetMultiSamples(0);
  view->GetRenderer()->SetBackground(colors->GetColor3d("LightGrey").GetData());
  view->GetInteractor()->Initialize();
  view->Render();
  view->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(BoxChart)

find_package(VTK COMPONENTS 
  ChartsCore
  CommonCore
  CommonDataModel
  FiltersStatistics
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  ViewsContext2D
)

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

Download and Build BoxChart

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

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

./BoxChart

WINDOWS USERS

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