StackedBar
Repository source: StackedBar
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
StackedBar.cxx
#include <vtkAxis.h>
#include <vtkChartLegend.h>
#include <vtkChartXY.h>
#include <vtkColorSeries.h>
#include <vtkContextView.h>
#include <vtkDoubleArray.h>
#include <vtkIntArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlotBar.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStringArray.h>
#include <vtkTable.h>
#include <vtkTextProperty.h>
namespace {
constexpr int num_months = 12;
constexpr int month[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
constexpr int book_2008[] = {5675, 5902, 6388, 5990, 5575, 7393,
9878, 8082, 6417, 5946, 5526, 5166};
constexpr int new_popular_2008[] = {701, 687, 736, 696, 750, 814,
923, 860, 786, 735, 680, 741};
constexpr int periodical_2008[] = {184, 176, 166, 131, 171, 191,
231, 166, 197, 162, 152, 143};
constexpr int audiobook_2008[] = {903, 1038, 987, 1073, 1144, 1203,
1173, 1196, 1213, 1076, 926, 874};
constexpr int video_2008[] = {1524, 1565, 1627, 1445, 1179, 1816,
2293, 1811, 1588, 1561, 1542, 1563};
constexpr int book_2009[] = {6388, 5990, 5575, 9878, 8082, 5675,
7393, 5902, 5526, 5166, 5946, 6417};
constexpr int new_popular_2009[] = {696, 735, 786, 814, 736, 860,
750, 687, 923, 680, 741, 701};
constexpr int periodical_2009[] = {197, 166, 176, 231, 171, 152,
166, 131, 184, 191, 143, 162};
constexpr int audiobook_2009[] = {1213, 1076, 926, 987, 903, 1196,
1073, 1144, 1203, 1038, 874, 1173};
constexpr int video_2009[] = {2293, 1561, 1542, 1627, 1588, 1179,
1563, 1445, 1811, 1565, 1524, 1816};
void build_array(const char* name, vtkIntArray* array, const int c_array[])
{
array->SetName(name);
for (int i = 0; i < num_months; ++i)
{
array->InsertNextValue(c_array[i]);
}
}
} // namespace
//----------------------------------------------------------------------------
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Set up a 2D scene, add an XY chart to it.
vtkNew<vtkContextView> view;
view->GetRenderWindow()->SetSize(500, 350);
vtkNew<vtkChartXY> chart;
view->GetScene()->AddItem(chart);
// Create a table with some points in it...
vtkNew<vtkTable> table;
vtkNew<vtkIntArray> arrMonth;
build_array("Month", arrMonth, month);
table->AddColumn(arrMonth);
vtkNew<vtkIntArray> arrBooks2008;
build_array("Books 2008", arrBooks2008, book_2008);
table->AddColumn(arrBooks2008);
vtkNew<vtkIntArray> arrNewPopular2008;
build_array("New / Popular 2008", arrNewPopular2008, new_popular_2008);
table->AddColumn(arrNewPopular2008);
vtkNew<vtkIntArray> arrPeriodical2008;
build_array("Periodical 2008", arrPeriodical2008, periodical_2008);
table->AddColumn(arrPeriodical2008);
vtkNew<vtkIntArray> arrAudiobook2008;
build_array("Audiobook 2008", arrAudiobook2008, audiobook_2008);
table->AddColumn(arrAudiobook2008);
vtkNew<vtkIntArray> arrVideo2008;
build_array("Video 2008", arrVideo2008, video_2008);
table->AddColumn(arrVideo2008);
vtkNew<vtkIntArray> arrBooks2009;
build_array("Books 2009", arrBooks2009, book_2009);
table->AddColumn(arrBooks2009);
vtkNew<vtkIntArray> arrNewPopular2009;
build_array("New / Popular 2009", arrNewPopular2009, new_popular_2009);
table->AddColumn(arrNewPopular2009);
vtkNew<vtkIntArray> arrPeriodical2009;
build_array("Periodical 2009", arrPeriodical2009, periodical_2009);
table->AddColumn(arrPeriodical2009);
vtkNew<vtkIntArray> arrAudiobook2009;
build_array("Audiobook 2009", arrAudiobook2009, audiobook_2009);
table->AddColumn(arrAudiobook2009);
vtkNew<vtkIntArray> arrVideo2009;
build_array("Video 2009", arrVideo2009, video_2009);
table->AddColumn(arrVideo2009);
// Create a color series to use with our stacks.
vtkNew<vtkColorSeries> colorSeries1;
colorSeries1->SetColorScheme(vtkColorSeries::WILD_FLOWER);
// Add multiple line plots, setting the colors etc.
vtkPlotBar* bar = 0;
bar = dynamic_cast<vtkPlotBar*>(chart->AddPlot(vtkChart::BAR));
bar->SetColorSeries(colorSeries1);
bar->SetInputData(table, "Month", "Books 2008");
bar->SetInputArray(2, "New / Popular 2008");
bar->SetInputArray(3, "Periodical 2008");
bar->SetInputArray(4, "Audiobook 2008");
bar->SetInputArray(5, "Video 2008");
vtkNew<vtkColorSeries> colorSeries2;
colorSeries2->SetColorScheme(vtkColorSeries::WILD_FLOWER);
bar = dynamic_cast<vtkPlotBar*>(chart->AddPlot(vtkChart::BAR));
bar->SetColorSeries(colorSeries2);
bar->SetInputData(table, "Month", "Books 2009");
bar->SetInputArray(2, "New / Popular 2009");
bar->SetInputArray(3, "Periodical 2009");
bar->SetInputArray(4, "Audiobook 2009");
bar->SetInputArray(5, "Video 2009");
chart->SetShowLegend(true);
vtkAxis* axis = chart->GetAxis(vtkAxis::BOTTOM);
axis->SetBehavior(1);
axis->SetMaximum(13.0);
axis->SetTitle("Month");
chart->GetAxis(vtkAxis::LEFT)->SetTitle("");
chart->SetTitle("Circulation 2008, 2009");
// Set up the legend to be off to the top right of the viewport.
chart->GetLegend()->SetInline(false);
chart->GetLegend()->SetHorizontalAlignment(vtkChartLegend::RIGHT);
chart->GetLegend()->SetVerticalAlignment(vtkChartLegend::TOP);
// Set up some custom labels for months.
vtkNew<vtkDoubleArray> dates;
vtkNew<vtkStringArray> strings;
dates->InsertNextValue(1);
strings->InsertNextValue("January");
dates->InsertNextValue(2);
strings->InsertNextValue("February");
dates->InsertNextValue(3);
strings->InsertNextValue("March");
dates->InsertNextValue(4);
strings->InsertNextValue("April");
dates->InsertNextValue(5);
strings->InsertNextValue("May");
dates->InsertNextValue(6);
strings->InsertNextValue("June");
dates->InsertNextValue(7);
strings->InsertNextValue("July");
dates->InsertNextValue(8);
strings->InsertNextValue("August");
dates->InsertNextValue(9);
strings->InsertNextValue("September");
dates->InsertNextValue(10);
strings->InsertNextValue("October");
dates->InsertNextValue(11);
strings->InsertNextValue("November");
dates->InsertNextValue(12);
strings->InsertNextValue("December");
axis->SetCustomTickPositions(dates, strings);
axis->GetLabelProperties()->SetOrientation(90);
axis->GetLabelProperties()->SetVerticalJustification(VTK_TEXT_CENTERED);
axis->GetLabelProperties()->SetJustification(VTK_TEXT_RIGHT);
// Finally render the scene and compare the image to a reference image.
view->GetRenderer()->SetBackground(colors->GetColor3d("Cornsilk").GetData());
view->GetRenderWindow()->SetMultiSamples(0);
view->GetRenderWindow()->Render();
view->GetRenderWindow()->SetSize(600, 400);
view->GetRenderWindow()->SetWindowName("StackedBar");
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(StackedBar)
find_package(VTK COMPONENTS
ChartsCore
CommonColor
CommonCore
CommonDataModel
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
ViewsContext2D
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "StackedBar: 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(StackedBar MACOSX_BUNDLE StackedBar.cxx )
target_link_libraries(StackedBar PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS StackedBar
MODULES ${VTK_LIBRARIES}
)
Download and Build StackedBar¶
Click here to download StackedBar and its CMakeLists.txt file. Once the tarball StackedBar.tar has been downloaded and extracted,
cd StackedBar/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:
./StackedBar
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.