ctkVTKChartViewTest1.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QSharedPointer>
  17. #include <QTimer>
  18. // CTK includes
  19. #include "ctkVTKChartView.h"
  20. // VTK includes
  21. #include <vtkIntArray.h>
  22. #include <vtkNew.h>
  23. #include <vtkPlotBar.h>
  24. #include <vtkTable.h>
  25. #include <vtkVersion.h>
  26. #if CTK_USE_QVTKOPENGLWIDGET
  27. #include <QVTKOpenGLWidget.h>
  28. #endif
  29. // STD includes
  30. #include <iostream>
  31. // Monthly circulation data
  32. static int data_2008[] = {10822, 10941, 9979, 10370, 9460, 11228, 15093, 12231, 10160, 9816, 9384, 7892};
  33. static int data_2009[] = {9058, 9474, 9979, 9408, 8900, 11569, 14688, 12231, 10294, 9585, 8957, 8590};
  34. static int data_2010[] = {9058, 10941, 9979, 10270, 8900, 11228, 14688, 12231, 10160, 9585, 9384, 8590};
  35. //-----------------------------------------------------------------------------
  36. int ctkVTKChartViewTest1(int argc, char * argv [] )
  37. {
  38. #if CTK_USE_QVTKOPENGLWIDGET
  39. QSurfaceFormat format = QVTKOpenGLWidget::defaultFormat();
  40. format.setSamples(0);
  41. QSurfaceFormat::setDefaultFormat(format);
  42. #endif
  43. QApplication app(argc, argv);
  44. ctkVTKChartView view(0);
  45. // Create a table with some points in it...
  46. vtkNew<vtkTable> table;
  47. vtkNew<vtkIntArray> arrMonth;
  48. arrMonth->SetName("Month");
  49. table->AddColumn(arrMonth.GetPointer());
  50. vtkNew<vtkIntArray> arr2008;
  51. arr2008->SetName("2008");
  52. table->AddColumn(arr2008.GetPointer());
  53. vtkNew<vtkIntArray> arr2009;
  54. arr2009->SetName("2009");
  55. table->AddColumn(arr2009.GetPointer());
  56. vtkNew<vtkIntArray> arr2010;
  57. arr2010->SetName("2010");
  58. table->AddColumn(arr2010.GetPointer());
  59. table->SetNumberOfRows(12);
  60. for (int i = 0; i < 12; i++)
  61. {
  62. table->SetValue(i,0,i+1);
  63. table->SetValue(i,1,data_2008[i]);
  64. table->SetValue(i,2,data_2009[i]);
  65. table->SetValue(i,3,data_2010[i]);
  66. }
  67. // Add multiple line plots, setting the colors etc
  68. vtkPlotBar* bar = vtkPlotBar::New();
  69. #if (VTK_MAJOR_VERSION <= 5)
  70. bar->SetInput(table.GetPointer(), 0, 1);
  71. #else
  72. bar->SetInputData(table.GetPointer(), 0, 1);
  73. #endif
  74. bar->SetColor(0, 255, 0, 255);
  75. view.addPlot(bar);
  76. bar->Delete();
  77. bar = vtkPlotBar::New();
  78. #if (VTK_MAJOR_VERSION <= 5)
  79. bar->SetInput(table.GetPointer(), 0, 2);
  80. #else
  81. bar->SetInputData(table.GetPointer(), 0, 2);
  82. #endif
  83. bar->SetColor(255, 0, 0, 255);
  84. view.addPlot(bar);
  85. bar->Delete();
  86. bar = vtkPlotBar::New();
  87. #if (VTK_MAJOR_VERSION <= 5)
  88. bar->SetInput(table.GetPointer(), 0, 3);
  89. #else
  90. bar->SetInputData(table.GetPointer(), 0, 3);
  91. #endif
  92. bar->SetColor(0, 0, 255, 255);
  93. view.addPlot(bar);
  94. bar->Delete();
  95. view.show();
  96. QTimer autoExit;
  97. if (argc < 2 || QString(argv[1]) != "-I")
  98. {
  99. QObject::connect(&autoExit, SIGNAL(timeout()), &app, SLOT(quit()));
  100. autoExit.start(1000);
  101. }
  102. return app.exec();
  103. }