Wt examples  3.3.0
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...
 

Additional Inherited Members

- Protected Member Functions inherited from Wt::WWidget
virtual void enableAjax ()=0
 
virtual void propagateSetEnabled (bool enabled)=0
 
virtual void render (WFlags< RenderFlag > flags)
 

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 163 of file ChartsExample.C.

163  :
164  WContainerWidget(parent)
165 {
166  new WText(WString::tr("scatter plot"), this);
167 
168  WAbstractItemModel *model = readCsvFile(
169  WApplication::appRoot() + "timeseries.csv", this);
170 
171  if (!model)
172  return;
173 
174  /*
175  * Parses the first column as dates, to be able to use a date scale
176  */
177  for (int i = 0; i < model->rowCount(); ++i) {
178  WString s = asString(model->data(i, 0));
179  WDate d = WDate::fromString(s, "dd/MM/yy");
180  model->setData(i, 0, d);
181  }
182 
183  // Show a view that allows editing of the model.
184  WContainerWidget *w = new WContainerWidget(this);
185  WTableView *table = new WTableView(w);
186 
187  table->setMargin(10, Top | Bottom);
188  table->setMargin(WLength::Auto, Left | Right);
189 
190  table->setModel(model);
191  table->setSortingEnabled(false); // Does not make much sense for time series
192  table->setColumnResizeEnabled(true);
193  table->setSelectionMode(NoSelection);
194  table->setAlternatingRowColors(true);
195  table->setColumnAlignment(0, AlignCenter);
196  table->setHeaderAlignment(0, AlignCenter);
197  table->setRowHeight(22);
198 
199  // Editing does not really work without Ajax, it would require an
200  // additional button somewhere to confirm the edited value.
201  if (WApplication::instance()->environment().ajax()) {
202  table->resize(800, 20 + 5*22);
203  table->setEditTriggers(WAbstractItemView::SingleClicked);
204  } else {
205  table->resize(800, 20 + 5*22 + 25);
206  table->setEditTriggers(WAbstractItemView::NoEditTrigger);
207  }
208 
209  WItemDelegate *delegate = new WItemDelegate(this);
210  delegate->setTextFormat("%.1f");
211  table->setItemDelegate(delegate);
212  table->setItemDelegateForColumn(0, new WItemDelegate(this));
213 
214  table->setColumnWidth(0, 80);
215  for (int i = 1; i < model->columnCount(); ++i)
216  table->setColumnWidth(i, 90);
217 
218  /*
219  * Create the scatter plot.
220  */
221  WCartesianChart *chart = new WCartesianChart(this);
222  //chart->setPreferredMethod(WPaintedWidget::PngImage);
223  //chart->setBackground(gray);
224  chart->setModel(model); // set the model
225  chart->setXSeriesColumn(0); // set the column that holds the X data
226  chart->setLegendEnabled(true); // enable the legend
227 
228  chart->setType(ScatterPlot); // set type to ScatterPlot
229  chart->axis(XAxis).setScale(DateScale); // set scale of X axis to DateScale
230 
231  // Provide space for the X and Y axis and title.
232  chart->setPlotAreaPadding(80, Left);
233  chart->setPlotAreaPadding(40, Top | Bottom);
234 
235  /*
236  * Add first two columns as line series
237  */
238  for (int i = 1; i < 3; ++i) {
239  WDataSeries s(i, LineSeries);
240  s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
241  chart->addSeries(s);
242  }
243 
244  chart->resize(800, 400); // WPaintedWidget must be given explicit size
245 
246  chart->setMargin(10, Top | Bottom); // add margin vertically
247  chart->setMargin(WLength::Auto, Left | Right); // center horizontally
248 
249  new ChartConfig(chart, this);
250 }

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

Generated on Thu Oct 24 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.4