CategoryExample.C 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "CategoryExample.h"
  2. #include "Models.h"
  3. #include "CsvUtil.h"
  4. #include "Wt/WApplication"
  5. #include "Wt/Chart/WStandardColorMap"
  6. #include "Wt/Chart/WGridData"
  7. #include "Wt/Chart/WEquidistantGridData"
  8. #include "Wt/WFont"
  9. #include "Wt/WString"
  10. #include <fstream>
  11. #include <string>
  12. #include <iostream>
  13. namespace {
  14. Wt::WStandardItemModel* readCsvFile(const std::string &fname)
  15. {
  16. WStandardItemModel *model = new WStandardItemModel(0, 0);
  17. std::ifstream f(fname.c_str());
  18. if (!f) {
  19. throw std::string("No .csv resource");
  20. }
  21. readFromCsv(f, model, -1, false);
  22. return model;
  23. }
  24. }
  25. CategoryExample::CategoryExample(WContainerWidget *parent)
  26. : WContainerWidget(parent)
  27. {
  28. setContentAlignment(AlignCenter);
  29. chart_ = new WCartesian3DChart(this);
  30. // Disabling server side rendering for Wt website.
  31. chart_->setRenderOptions(WGLWidget::ClientSideRendering | WGLWidget::AntiAliasing);
  32. chart_->setType(CategoryChart);
  33. Wt::WCssDecorationStyle style;
  34. style.setBorder(Wt::WBorder(Wt::WBorder::Solid, Wt::WBorder::Medium, Wt::black));
  35. chart_->setDecorationStyle(style);
  36. chart_->resize(600, 600);
  37. // **** Dataset
  38. isotopeModel_ = readCsvFile(WApplication::appRoot() + "isotope_decay.csv");
  39. // add some color-roles
  40. for (int i=1; i<isotopeModel_->rowCount(); i++) {
  41. for (int j=1; j<isotopeModel_->columnCount(); j++) {
  42. double value = Wt::asNumber(isotopeModel_->data(i, j));
  43. if (value < 3)
  44. isotopeModel_->setData(i, j, WColor(blue), MarkerBrushColorRole);
  45. }
  46. }
  47. WGridData *isotopes = new WGridData(isotopeModel_);
  48. series_.push_back(isotopes);
  49. isotopes->setType(BarSeries3D);
  50. // **** Dataset
  51. planeModel_ = readCsvFile(WApplication::appRoot() + "hor_plane.csv");
  52. WGridData *horPlane = new WGridData(planeModel_);
  53. series_.push_back(horPlane);
  54. horPlane->setType(BarSeries3D);
  55. // **** Dataset
  56. randomModel_ = readCsvFile(WApplication::appRoot() + "hor_plane.csv");
  57. for (int i=1; i<randomModel_->rowCount(); i++) {
  58. for (int j=1; j<randomModel_->columnCount(); j++) {
  59. randomModel_->setData(i, j,Wt::asNumber(randomModel_->data(i, j)) + (rand() % 100)/100.0);
  60. }
  61. }
  62. WGridData *randomPlane = new WGridData(randomModel_);
  63. series_.push_back(randomPlane);
  64. randomPlane->setType(BarSeries3D);
  65. // **** Dataset
  66. yPlaneModel_ = new PlaneData(20, 20, 0, 1, 0, 1, true, 100, 100);
  67. WEquidistantGridData *yPlaneFunc = new WEquidistantGridData(yPlaneModel_,
  68. 0, 1, 0, 1);
  69. series_.push_back(yPlaneFunc);
  70. yPlaneFunc->setType(BarSeries3D);
  71. // **** Dataset
  72. xPlaneModel_ = new PlaneData(20, 20, 0, 1, 0, 1, false, 100, 100);
  73. WEquidistantGridData *xPlaneFunc = new WEquidistantGridData(xPlaneModel_,
  74. 0, 1, 0, 1);
  75. series_.push_back(xPlaneFunc);
  76. xPlaneFunc->setType(BarSeries3D);
  77. // **** Dataset
  78. xPlaneModelColor_ = new PlaneData(20, 20, 0, 1, 0, 1, false, 5, 100);
  79. WEquidistantGridData *xPlaneFuncColor = new WEquidistantGridData(xPlaneModelColor_, 0, 1, 0, 1);
  80. series_.push_back(xPlaneFuncColor);
  81. xPlaneFuncColor->setType(BarSeries3D);
  82. // Data configuration widget
  83. DataConfig *dataconfig = new DataConfig(chart_);
  84. dataconfig->addDataToCollection("Isotope data (10x10) with ColorRoles", isotopes);
  85. dataconfig->addDataToCollection("Horizontal Plane (10x10)", horPlane);
  86. dataconfig->addDataToCollection("Random Data (10x10)", randomPlane);
  87. // dataconfig->addDataToCollection("tilted plane y (20x20)", yPlaneFunc);
  88. // dataconfig->addDataToCollection("tilted plane x (20x20)", xPlaneFunc);
  89. // dataconfig->addDataToCollection("tilted plane x (20x20) with ColorRoles", xPlaneFuncColor);
  90. WTabWidget *configuration_ = new WTabWidget(this);
  91. configuration_->addTab(new ChartSettings(chart_), "General Chart Settings", Wt::WTabWidget::PreLoading);
  92. configuration_->addTab(dataconfig, "Data selection and configuration", Wt::WTabWidget::PreLoading);
  93. }
  94. CategoryExample::~CategoryExample()
  95. {
  96. delete xPlaneModelColor_;
  97. delete xPlaneModel_;
  98. delete yPlaneModel_;
  99. delete randomModel_;
  100. delete planeModel_;
  101. delete isotopeModel_;
  102. std::vector<WAbstractDataSeries3D*> onChart = chart_->dataSeries();
  103. for (unsigned i=0; i < onChart.size(); i++)
  104. chart_->removeDataSeries(onChart[i]);
  105. for (unsigned i=0; i < series_.size(); i++)
  106. delete series_[i];
  107. }