Wt examples  3.3.3
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 191 of file ChartsExample.C.

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