QtBarChart
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
QtBarChart.cxx
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkChartXY.h>
#include <vtkPlot.h>
#include <vtkTable.h>
#include <vtkIntArray.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
#include <vtkRenderWindowInteractor.h>
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
// Monthly circulation data
static int data_2008[] = {10822, 10941, 9979, 10370, 9460, 11228, 15093, 12231, 10160, 9816, 9384, 7892};
static int data_2009[] = {9058, 9474, 9979, 9408, 8900, 11569, 14688, 12231, 10294, 9585, 8957, 8590};
static int data_2010[] = {9058, 10941, 9979, 10270, 8900, 11228, 14688, 12231, 10160, 9585, 9384, 8590};
int main(int, char*[] )
{
// Set up a 2D scene, add an XY chart to it
VTK_CREATE(vtkContextView, view);
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(400, 300);
VTK_CREATE(vtkChartXY, chart);
view->GetScene()->AddItem(chart);
// Create a table with some points in it...
VTK_CREATE(vtkTable, table);
VTK_CREATE(vtkIntArray, arrMonth);
arrMonth->SetName("Month");
table->AddColumn(arrMonth);
VTK_CREATE(vtkIntArray, arr2008);
arr2008->SetName("2008");
table->AddColumn(arr2008);
VTK_CREATE(vtkIntArray, arr2009);
arr2009->SetName("2009");
table->AddColumn(arr2009);
VTK_CREATE(vtkIntArray, arr2010);
arr2010->SetName("2010");
table->AddColumn(arr2010);
table->SetNumberOfRows(12);
for (int i = 0; i < 12; i++)
{
table->SetValue(i,0,i+1);
table->SetValue(i,1,data_2008[i]);
table->SetValue(i,2,data_2009[i]);
table->SetValue(i,3,data_2010[i]);
}
// Add multiple line plots, setting the colors etc
vtkPlot *line = 0;
line = chart->AddPlot(vtkChart::BAR);
line->SetInputData(table, 0, 1);
line->SetColor(0, 255, 0, 255);
line = chart->AddPlot(vtkChart::BAR);
line->SetInputData(table, 0, 2);
line->SetColor(255, 0, 0, 255);
line = chart->AddPlot(vtkChart::BAR);
line->SetInputData(table, 0, 3);
line->SetColor(0, 0, 255, 255);
// Finally render the scene and compare the image to a reference image
view->GetRenderWindow()->SetMultiSamples(0);
view->GetRenderWindow()->SetWindowName("QtBarChart");
view->GetRenderWindow()->Render();
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
if(POLICY CMP0020)
cmake_policy(SET CMP0020 NEW)
cmake_policy(SET CMP0071 NEW)
endif()
PROJECT(QtBarChart)
find_package(VTK COMPONENTS
vtkChartsCore
vtkCommonCore
vtkCommonDataModel
vtkInteractionStyle
vtkRenderingContext2D
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
vtkViewsContext2D
QUIET
)
if (NOT VTK_FOUND)
message("Skipping QtBarChart: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if(vtkGUISupportQt_LOADED OR TARGET VTK::GUISupportQt)
if (VTK_VERSION VERSION_LESS "8.90.0")
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
else()
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
endif()
# We have ui files, this will bring in the macro: qt5_wrap_ui
find_package(Qt5Widgets REQUIRED QUIET)
else()
message(STATUS "GuiSupportQt not found.")
return()
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
file(GLOB UI_FILES *.ui)
file(GLOB QT_WRAP *.h)
file(GLOB CXX_FILES *.cxx)
qt5_wrap_ui(UISrcs ${UI_FILES})
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
# CMAKE_AUTOMOC in ON so the MOC headers will be automatically wrapped.
add_executable(QtBarChart MACOSX_BUNDLE
${CXX_FILES} ${UISrcs} ${QT_WRAP})
qt5_use_modules(QtBarChart Core Gui)
target_link_libraries(QtBarChart ${VTK_LIBRARIES})
else()
# CMAKE_AUTOMOC in ON so the MOC headers will be automatically wrapped.
add_executable(QtBarChart MACOSX_BUNDLE
${CXX_FILES} ${UISrcs} ${QT_WRAP})
qt5_use_modules(QtBarChart Core Gui)
target_link_libraries(QtBarChart ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS QtBarChart
MODULES ${VTK_LIBRARIES}
)
endif()
Download and Build QtBarChart¶
Click here to download QtBarChart and its CMakeLists.txt file. Once the tarball QtBarChart.tar has been downloaded and extracted,
cd QtBarChart/build
This example requires Qt and VTK.
If VTK and Qt are 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 ..
If Qt is not found on your system, you will need to tell CMake where to find qmake:
cmake -DQT_QMAKE_EXECUTABLE:FILEPATH=/usr/something/qmake ..
Build the project:
make
and run it:
./QtBarChart
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time. You may also need to add a Qt related path.