Wt examples  3.3.4
Public Member Functions | List of all members
TimeSeriesExample Class Reference

A widget that demonstrates a times series chart. More...

#include <ChartsExample.h>

Inheritance diagram for TimeSeriesExample:
Inheritance graph
[legend]

Public Member Functions

 TimeSeriesExample (Wt::WContainerWidget *parent)
 Creates the time series scatter plot example. More...
 

Detailed Description

A widget that demonstrates a times series chart.

Definition at line 29 of file ChartsExample.h.

Constructor & Destructor Documentation

TimeSeriesExample::TimeSeriesExample ( Wt::WContainerWidget *  parent)

Creates the time series scatter plot example.

Definition at line 190 of file ChartsExample.C.

190  :
191  WContainerWidget(parent)
192 {
193  new WText(WString::tr("scatter plot"), this);
194 
195  WAbstractItemModel *model = readCsvFile(
196  WApplication::appRoot() + "timeseries.csv", this);
197 
198  if (!model)
199  return;
200 
201  /*
202  * Parses the first column as dates, to be able to use a date scale
203  */
204  for (int i = 0; i < model->rowCount(); ++i) {
205  WString s = asString(model->data(i, 0));
206  WDate d = WDate::fromString(s, "dd/MM/yy");
207  model->setData(i, 0, d);
208  }
209 
210  // Show a view that allows editing of the model.
211  WContainerWidget *w = new WContainerWidget(this);
212  WTableView *table = new WTableView(w);
213 
214  table->setMargin(10, Top | Bottom);
215  table->setMargin(WLength::Auto, Left | Right);
216 
217  table->setModel(model);
218  table->setSortingEnabled(false); // Does not make much sense for time series
219  table->setColumnResizeEnabled(true);
220  table->setSelectionMode(NoSelection);
221  table->setAlternatingRowColors(true);
222  table->setColumnAlignment(0, AlignCenter);
223  table->setHeaderAlignment(0, AlignCenter);
224  table->setRowHeight(22);
225 
226  // Editing does not really work without Ajax, it would require an
227  // additional button somewhere to confirm the edited value.
228  if (WApplication::instance()->environment().ajax()) {
229  table->resize(800, 20 + 5*22);
230  table->setEditTriggers(WAbstractItemView::SingleClicked);
231  } else {
232  table->resize(800, 20 + 5*22 + 25);
233  table->setEditTriggers(WAbstractItemView::NoEditTrigger);
234  }
235 
236  WItemDelegate *delegate = new WItemDelegate(this);
237  delegate->setTextFormat("%.1f");
238  table->setItemDelegate(delegate);
239  table->setItemDelegateForColumn(0, new WItemDelegate(this));
240 
241  table->setColumnWidth(0, 80);
242  for (int i = 1; i < model->columnCount(); ++i)
243  table->setColumnWidth(i, 90);
244 
245  /*
246  * Create the scatter plot.
247  */
248  WCartesianChart *chart = new WCartesianChart(this);
249  //chart->setPreferredMethod(WPaintedWidget::PngImage);
250  //chart->setBackground(gray);
251  chart->setModel(model); // set the model
252  chart->setXSeriesColumn(0); // set the column that holds the X data
253  chart->setLegendEnabled(true); // enable the legend
254 
255  chart->setType(ScatterPlot); // set type to ScatterPlot
256  chart->axis(XAxis).setScale(DateScale); // set scale of X axis to DateScale
257 
258  // Automatically layout chart (space for axes, legend, ...)
259  chart->setAutoLayoutEnabled();
260 
261  /*
262  * Add first two columns as line series
263  */
264  for (int i = 1; i < 3; ++i) {
265  WDataSeries s(i, LineSeries);
266  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
267  chart->addSeries(s);
268  }
269 
270  chart->resize(800, 400); // WPaintedWidget must be given explicit size
271 
272  chart->setMargin(10, Top | Bottom); // add margin vertically
273  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
274 
275  new ChartConfig(chart, this);
276 }
A class that allows configuration of a cartesian chart.
Definition: ChartConfig.h:37

The documentation for this class was generated from the following files:

Generated on Thu Jul 16 2015 for the C++ Web Toolkit (Wt) by doxygen 1.8.9.1