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.

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

The documentation for this class was generated from the following files:
Generated on Fri May 31 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.3.1