ChartsExample.C 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <math.h>
  7. #include <fstream>
  8. #include "ChartsExample.h"
  9. #include "ChartConfig.h"
  10. #include "CsvUtil.h"
  11. #include <Wt/WApplication>
  12. #include <Wt/WDate>
  13. #include <Wt/WEnvironment>
  14. #include <Wt/WItemDelegate>
  15. #include <Wt/WStandardItemModel>
  16. #include <Wt/WText>
  17. #include <Wt/WBorderLayout>
  18. #include <Wt/WFitLayout>
  19. #include <Wt/WStandardItem>
  20. #include <Wt/WTableView>
  21. #include <Wt/Chart/WCartesianChart>
  22. #include <Wt/Chart/WPieChart>
  23. using namespace Wt;
  24. using namespace Wt::Chart;
  25. namespace {
  26. /*
  27. * A standard item which converts text edits to numbers
  28. */
  29. class NumericItem : public WStandardItem {
  30. public:
  31. virtual NumericItem *clone() const {
  32. return new NumericItem();
  33. }
  34. virtual void setData(const boost::any &data, int role = UserRole) {
  35. boost::any dt;
  36. if (role == EditRole) {
  37. std::string s = Wt::asString(data).toUTF8();
  38. char *endptr;
  39. double d = strtod(s.c_str(), &endptr);
  40. if (*endptr == 0)
  41. dt = boost::any(d);
  42. else
  43. dt = data;
  44. }
  45. WStandardItem::setData(data, role);
  46. }
  47. };
  48. /*
  49. * Reads a CSV file as an (editable) standard item model.
  50. */
  51. WAbstractItemModel *readCsvFile(const std::string &fname,
  52. WContainerWidget *parent)
  53. {
  54. WStandardItemModel *model = new WStandardItemModel(0, 0, parent);
  55. model->setItemPrototype(new NumericItem());
  56. std::ifstream f(fname.c_str());
  57. if (f) {
  58. readFromCsv(f, model);
  59. for (int row = 0; row < model->rowCount(); ++row)
  60. for (int col = 0; col < model->columnCount(); ++col) {
  61. model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
  62. /*
  63. Example of tool tips (disabled here because they are not updated
  64. when editing data)
  65. */
  66. /*
  67. WString toolTip = asString(model->headerData(col)) + ": "
  68. + asString(model->item(row, col)->data(DisplayRole), "%.f");
  69. model->item(row, col)->setToolTip(toolTip);
  70. */
  71. }
  72. return model;
  73. } else {
  74. WString error(WString::tr("error-missing-data"));
  75. error.arg(fname, UTF8);
  76. new WText(error, parent);
  77. return 0;
  78. }
  79. }
  80. }
  81. ChartsExample::ChartsExample(WContainerWidget *root)
  82. : WContainerWidget(root)
  83. {
  84. new WText(WString::tr("introduction"), this);
  85. new CategoryExample(this);
  86. new TimeSeriesExample(this);
  87. new ScatterPlotExample(this);
  88. new PieExample(this);
  89. }
  90. CategoryExample::CategoryExample(Wt::WContainerWidget *parent):
  91. WContainerWidget(parent)
  92. {
  93. new WText(WString::tr("category chart"), this);
  94. WAbstractItemModel *model
  95. = readCsvFile(WApplication::appRoot() + "category.csv", this);
  96. if (!model)
  97. return;
  98. // Show a view that allows editing of the model.
  99. WContainerWidget *w = new WContainerWidget(this);
  100. WTableView *table = new WTableView(w);
  101. table->setMargin(10, Top | Bottom);
  102. table->setMargin(WLength::Auto, Left | Right);
  103. table->setModel(model);
  104. table->setSortingEnabled(true);
  105. table->setColumnResizeEnabled(true);
  106. // table->setSelectionMode(ExtendedSelection);
  107. table->setAlternatingRowColors(true);
  108. table->setColumnAlignment(0, AlignCenter);
  109. table->setHeaderAlignment(0, AlignCenter);
  110. table->setRowHeight(22);
  111. // Editing does not really work without Ajax, it would require an
  112. // additional button somewhere to confirm the edited value.
  113. if (WApplication::instance()->environment().ajax()) {
  114. table->resize(600, 20 + 5*22);
  115. table->setEditTriggers(WAbstractItemView::SingleClicked);
  116. } else {
  117. table->resize(600, WLength::Auto);
  118. table->setEditTriggers(WAbstractItemView::NoEditTrigger);
  119. }
  120. // We use a single delegate for all items which rounds values to
  121. // the closest integer value.
  122. WItemDelegate *delegate = new WItemDelegate(this);
  123. delegate->setTextFormat("%.f");
  124. table->setItemDelegate(delegate);
  125. table->setColumnWidth(0, 80);
  126. for (int i = 1; i < model->columnCount(); ++i)
  127. table->setColumnWidth(i, 120);
  128. /*
  129. * Create the category chart.
  130. */
  131. WCartesianChart *chart = new WCartesianChart(this);
  132. chart->setModel(model); // set the model
  133. chart->setXSeriesColumn(0); // set the column that holds the categories
  134. chart->setLegendEnabled(true); // enable the legend
  135. chart->setZoomEnabled(true);
  136. chart->setPanEnabled(true);
  137. // Automatically layout chart (space for axes, legend, ...)
  138. chart->setAutoLayoutEnabled(true);
  139. chart->setBackground(WColor(200,200,200));
  140. /*
  141. * Add all (but first) column as bar series
  142. */
  143. for (int i = 1; i < model->columnCount(); ++i) {
  144. WDataSeries *s = new WDataSeries(i, BarSeries);
  145. s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
  146. chart->addSeries(s);
  147. }
  148. chart->resize(800, 400);
  149. chart->setMargin(10, Top | Bottom);
  150. chart->setMargin(WLength::Auto, Left | Right);
  151. /*
  152. * Provide a widget to manipulate chart properties
  153. */
  154. new ChartConfig(chart, this);
  155. }
  156. TimeSeriesExample::TimeSeriesExample(Wt::WContainerWidget *parent):
  157. WContainerWidget(parent)
  158. {
  159. new WText(WString::tr("scatter plot"), this);
  160. WAbstractItemModel *model = readCsvFile(
  161. WApplication::appRoot() + "timeseries.csv", this);
  162. if (!model)
  163. return;
  164. /*
  165. * Parses the first column as dates, to be able to use a date scale
  166. */
  167. for (int i = 0; i < model->rowCount(); ++i) {
  168. WString s = asString(model->data(i, 0));
  169. WDate d = WDate::fromString(s, "dd/MM/yy");
  170. model->setData(i, 0, d);
  171. }
  172. // Show a view that allows editing of the model.
  173. WContainerWidget *w = new WContainerWidget(this);
  174. WTableView *table = new WTableView(w);
  175. table->setMargin(10, Top | Bottom);
  176. table->setMargin(WLength::Auto, Left | Right);
  177. table->setModel(model);
  178. table->setSortingEnabled(false); // Does not make much sense for time series
  179. table->setColumnResizeEnabled(true);
  180. table->setSelectionMode(NoSelection);
  181. table->setAlternatingRowColors(true);
  182. table->setColumnAlignment(0, AlignCenter);
  183. table->setHeaderAlignment(0, AlignCenter);
  184. table->setRowHeight(22);
  185. // Editing does not really work without Ajax, it would require an
  186. // additional button somewhere to confirm the edited value.
  187. if (WApplication::instance()->environment().ajax()) {
  188. table->resize(800, 20 + 5*22);
  189. table->setEditTriggers(WAbstractItemView::SingleClicked);
  190. } else {
  191. table->resize(800, 20 + 5*22 + 25);
  192. table->setEditTriggers(WAbstractItemView::NoEditTrigger);
  193. }
  194. WItemDelegate *delegate = new WItemDelegate(this);
  195. delegate->setTextFormat("%.1f");
  196. table->setItemDelegate(delegate);
  197. table->setItemDelegateForColumn(0, new WItemDelegate(this));
  198. table->setColumnWidth(0, 80);
  199. for (int i = 1; i < model->columnCount(); ++i)
  200. table->setColumnWidth(i, 90);
  201. /*
  202. * Create the scatter plot.
  203. */
  204. WCartesianChart *chart = new WCartesianChart(this);
  205. //chart->setPreferredMethod(WPaintedWidget::PngImage);
  206. //chart->setBackground(gray);
  207. chart->setModel(model); // set the model
  208. chart->setXSeriesColumn(0); // set the column that holds the X data
  209. chart->setLegendEnabled(true); // enable the legend
  210. chart->setZoomEnabled(true);
  211. chart->setPanEnabled(true);
  212. chart->setType(ScatterPlot); // set type to ScatterPlot
  213. chart->axis(XAxis).setScale(DateScale); // set scale of X axis to DateScale
  214. // Automatically layout chart (space for axes, legend, ...)
  215. chart->setAutoLayoutEnabled();
  216. chart->setBackground(WColor(200,200,200));
  217. /*
  218. * Add first two columns as line series
  219. */
  220. for (int i = 1; i < 3; ++i) {
  221. WDataSeries *s = new WDataSeries(i, LineSeries);
  222. s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
  223. chart->addSeries(s);
  224. }
  225. chart->resize(800, 400); // WPaintedWidget must be given explicit size
  226. chart->setMargin(10, Top | Bottom); // add margin vertically
  227. chart->setMargin(WLength::Auto, Left | Right); // center horizontally
  228. new ChartConfig(chart, this);
  229. }
  230. ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
  231. WContainerWidget(parent)
  232. {
  233. new WText(WString::tr("scatter plot 2"), this);
  234. WStandardItemModel *model = new WStandardItemModel(40, 2, this);
  235. model->setItemPrototype(new NumericItem());
  236. model->setHeaderData(0, WString("X"));
  237. model->setHeaderData(1, WString("Y = sin(X)"));
  238. for (unsigned i = 0; i < 40; ++i) {
  239. double x = (static_cast<double>(i) - 20) / 4;
  240. model->setData(i, 0, x);
  241. model->setData(i, 1, sin(x));
  242. }
  243. /*
  244. * Create the scatter plot.
  245. */
  246. WCartesianChart *chart = new WCartesianChart(this);
  247. chart->setModel(model); // set the model
  248. chart->setXSeriesColumn(0); // set the column that holds the X data
  249. chart->setLegendEnabled(true); // enable the legend
  250. chart->setZoomEnabled(true);
  251. chart->setPanEnabled(true);
  252. chart->setCrosshairEnabled(true);
  253. chart->setBackground(WColor(200,200,200));
  254. chart->setType(ScatterPlot); // set type to ScatterPlot
  255. // Typically, for mathematical functions, you want the axes to cross
  256. // at the 0 mark:
  257. chart->axis(XAxis).setLocation(ZeroValue);
  258. chart->axis(YAxis).setLocation(ZeroValue);
  259. // Automatically layout chart (space for axes, legend, ...)
  260. chart->setAutoLayoutEnabled();
  261. // Add the curves
  262. WDataSeries *s = new WDataSeries(1, CurveSeries);
  263. s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
  264. chart->addSeries(s);
  265. chart->resize(800, 300); // WPaintedWidget must be given explicit size
  266. chart->setMargin(10, Top | Bottom); // add margin vertically
  267. chart->setMargin(WLength::Auto, Left | Right); // center horizontally
  268. ChartConfig *config = new ChartConfig(chart, this);
  269. config->setValueFill(ZeroValueFill);
  270. }
  271. PieExample::PieExample(WContainerWidget *parent):
  272. WContainerWidget(parent)
  273. {
  274. new WText(WString::tr("pie chart"), this);
  275. WStandardItemModel *model = new WStandardItemModel(this);
  276. model->setItemPrototype(new NumericItem());
  277. //headers
  278. model->insertColumns(model->columnCount(), 2);
  279. model->setHeaderData(0, WString("Item"));
  280. model->setHeaderData(1, WString("Sales"));
  281. //data
  282. model->insertRows(model->rowCount(), 6);
  283. int row = 0;
  284. model->setData(row, 0, WString("Blueberry"));
  285. model->setData(row, 1, 120);
  286. // model->setData(row, 1, WString("Blueberry"), ToolTipRole);
  287. row++;
  288. model->setData(row, 0, WString("Cherry"));
  289. model->setData(row, 1, 30);
  290. row++;
  291. model->setData(row, 0, WString("Apple"));
  292. model->setData(row, 1, 260);
  293. row++;
  294. model->setData(row, 0, WString("Boston Cream"));
  295. model->setData(row, 1, 160);
  296. row++;
  297. model->setData(row, 0, WString("Other"));
  298. model->setData(row, 1, 40);
  299. row++;
  300. model->setData(row, 0, WString("Vanilla Cream"));
  301. model->setData(row, 1, 120);
  302. row++;
  303. //set all items to be editable and selectable
  304. for (int row = 0; row < model->rowCount(); ++row)
  305. for (int col = 0; col < model->columnCount(); ++col)
  306. model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
  307. WContainerWidget *w = new WContainerWidget(this);
  308. WTableView* table = new WTableView(w);
  309. table->setMargin(10, Top | Bottom);
  310. table->setMargin(WLength::Auto, Left | Right);
  311. table->setSortingEnabled(true);
  312. table->setModel(model);
  313. table->setColumnWidth(1, 100);
  314. table->setRowHeight(22);
  315. if (WApplication::instance()->environment().ajax()) {
  316. table->resize(150 + 100 + 14, 20 + 6 * 22);
  317. table->setEditTriggers(WAbstractItemView::SingleClicked);
  318. } else {
  319. table->resize(150 + 100 + 14, WLength::Auto);
  320. table->setEditTriggers(WAbstractItemView::NoEditTrigger);
  321. }
  322. /*
  323. * Create the pie chart.
  324. */
  325. WPieChart *chart = new WPieChart(this);
  326. chart->setModel(model); // set the model
  327. chart->setLabelsColumn(0); // set the column that holds the labels
  328. chart->setDataColumn(1); // set the column that holds the data
  329. // configure location and type of labels
  330. chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
  331. // enable a 3D and shadow effect
  332. chart->setPerspectiveEnabled(true, 0.2);
  333. chart->setShadowEnabled(true);
  334. // explode the first item
  335. chart->setExplode(0, 0.3);
  336. chart->resize(800, 300); // WPaintedWidget must be given an explicit size
  337. chart->setMargin(10, Top | Bottom); // add margin vertically
  338. chart->setMargin(WLength::Auto, Left | Right); // center horizontally
  339. }