Wt examples  3.3.6
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::TimeSeriesExample ( Wt::WContainerWidget *  parent)

Creates the time series scatter plot example.

Definition at line 193 of file ChartsExample.C.

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