1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestBoxPlot.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 // Hide VTK_DEPRECATED_IN_9_0_0() warnings for this class.
17 #define VTK_DEPRECATION_LEVEL 0
18 
19 #include "vtkAxis.h"
20 #include "vtkChartBox.h"
21 #include "vtkContextScene.h"
22 #include "vtkContextView.h"
23 #include "vtkFloatArray.h"
24 #include "vtkIntArray.h"
25 #include "vtkLookupTable.h"
26 #include "vtkNew.h"
27 #include "vtkPlotBox.h"
28 #include "vtkRenderWindow.h"
29 #include "vtkRenderWindowInteractor.h"
30 #include "vtkRenderer.h"
31 #include "vtkStringArray.h"
32 #include "vtkTable.h"
33 
34 //------------------------------------------------------------------------------
TestBoxPlot(int,char * [])35 int TestBoxPlot(int, char*[])
36 {
37   // Set up a 2D scene, add an XY chart to it
38   vtkNew<vtkContextView> view;
39   view->GetRenderWindow()->SetSize(400, 400);
40   view->GetRenderWindow()->SetMultiSamples(0);
41 
42   vtkNew<vtkChartBox> chart;
43   view->GetScene()->AddItem(chart);
44 
45   // Creates a vtkPlotBox input table
46   // The vtkPlotBox object will display 4 (arbitrary) box plots
47   int numParam = 5;
48   vtkNew<vtkTable> inputBoxPlotTable;
49 
50   for (int i = 0; i < numParam; i++)
51   {
52     char num[10];
53     snprintf(num, sizeof(num), "P%d", i);
54     vtkNew<vtkIntArray> arrIndex;
55     arrIndex->SetName(num);
56     inputBoxPlotTable->AddColumn(arrIndex);
57   }
58 
59   inputBoxPlotTable->SetNumberOfRows(5);
60 
61   // This scaling parameter can be used to test Y axis positioning
62   const double scale = 1e02;
63   for (int i = 0; i < numParam; i++)
64   {
65     inputBoxPlotTable->SetValue(0, i, (i / 2) * scale);         // Q0
66     inputBoxPlotTable->SetValue(1, i, (2 * i + 2 - i) * scale); // Q1
67     inputBoxPlotTable->SetValue(2, i, (2 * i + 4) * scale);     // Q2
68     inputBoxPlotTable->SetValue(3, i, (2 * i + 7) * scale);     // Q3
69     inputBoxPlotTable->SetValue(4, i, (2 * i + 8) * scale);     // Q4
70   }
71 
72   vtkNew<vtkLookupTable> lookup;
73   lookup->SetNumberOfColors(5);
74   lookup->SetRange(0, 4);
75   lookup->Build();
76 
77   chart->GetPlot(0)->SetInputData(inputBoxPlotTable);
78   chart->SetColumnVisibilityAll(true);
79   chart->SetShowLegend(true);
80 
81   // Hide one box plot
82   chart->SetColumnVisibility(3, false);
83 
84   // Set the labels
85   vtkNew<vtkStringArray> labels;
86   labels->SetNumberOfValues(5);
87   labels->SetValue(0, "Param 0");
88   labels->SetValue(1, "Param 1");
89   labels->SetValue(2, "Param 2");
90   labels->SetValue(3, "Param 3");
91   labels->SetValue(4, "Param 4");
92   chart->GetPlot(0)->SetLabels(labels);
93 
94   // Manually change the color of one serie
95   double rgb[3] = { 0.5, 0.5, 0.5 };
96   vtkPlotBox::SafeDownCast(chart->GetPlot(0))->SetColumnColor("P1", rgb);
97 
98   // Render the scene
99   view->GetRenderWindow()->SetMultiSamples(0);
100   view->GetInteractor()->Initialize();
101   view->Render();
102   view->GetInteractor()->Start();
103 
104   return EXIT_SUCCESS;
105 }
106