TimeSeriesExample Class Reference
[Charts example]

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

#include <ChartsExample.h>

Inheritance diagram for TimeSeriesExample:

Inheritance graph
[legend]

List of all members.

Public Member Functions

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


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

00149                                                               :
00150   WContainerWidget(parent)
00151 {
00152   new WText(WString::tr("scatter plot"), this);
00153 
00154   WAbstractItemModel *model = readCsvFile("timeseries.csv", this);
00155 
00156   if (!model)
00157     return;
00158 
00159   /*
00160    * Parses the first column as dates, to be able to use a date scale
00161    */
00162   for (int i = 0; i < model->rowCount(); ++i) {
00163     WString s = asString(model->data(i, 0));
00164     WDate d = WDate::fromString(s, "dd/MM/yy");
00165     model->setData(i, 0, boost::any(d));
00166   }
00167 
00168   // Show a view that allows editing of the model.
00169   WContainerWidget *w = new WContainerWidget(this);
00170   WTableView *table = new WTableView(w);
00171 
00172   table->setMargin(10, Top | Bottom);
00173   table->setMargin(WLength::Auto, Left | Right);
00174 
00175   table->setModel(model);
00176   table->setSortingEnabled(false); // Does not make much sense for time series
00177   table->setColumnResizeEnabled(true);
00178   table->setSelectionMode(NoSelection);
00179   table->setAlternatingRowColors(true);
00180   table->setColumnAlignment(0, AlignCenter);
00181   table->setHeaderAlignment(0, AlignCenter);
00182   table->setRowHeight(22);
00183 
00184   // Editing does not really work without Ajax, it would require an
00185   // additional button somewhere to confirm the edited value.
00186   if (WApplication::instance()->environment().ajax()) {
00187     table->resize(800, 20 + 5*22);
00188     table->setEditTriggers(WAbstractItemView::SingleClicked);
00189   } else {
00190     table->resize(800, 20 + 5*22 + 25);
00191     table->setEditTriggers(WAbstractItemView::NoEditTrigger);
00192   }
00193 
00194   WItemDelegate *delegate = new WItemDelegate(this);
00195   delegate->setTextFormat("%.1f");
00196   table->setItemDelegate(delegate);
00197   table->setItemDelegateForColumn(0, new WItemDelegate(this));
00198 
00199   table->setColumnWidth(0, 80);
00200   for (int i = 1; i < model->columnCount(); ++i)
00201     table->setColumnWidth(i, 90);
00202 
00203   /*
00204    * Create the scatter plot.
00205    */
00206   WCartesianChart *chart = new WCartesianChart(this);
00207   chart->setModel(model);        // set the model
00208   chart->setXSeriesColumn(0);    // set the column that holds the X data
00209   chart->setLegendEnabled(true); // enable the legend
00210 
00211   chart->setType(ScatterPlot);            // set type to ScatterPlot
00212   chart->axis(XAxis).setScale(DateScale); // set scale of X axis to DateScale
00213 
00214   // Provide space for the X and Y axis and title. 
00215   chart->setPlotAreaPadding(100, Left);
00216   chart->setPlotAreaPadding(50, Top | Bottom);
00217 
00218   /*
00219    * Add first two columns as line series
00220    */
00221   for (int i = 1; i < 3; ++i) {
00222     WDataSeries s(i, LineSeries);
00223     s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00224     chart->addSeries(s);
00225   }
00226 
00227   chart->resize(800, 400); // WPaintedWidget must be given explicit size
00228 
00229   chart->setMargin(10, Top | Bottom);            // add margin vertically
00230   chart->setMargin(WLength::Auto, Left | Right); // center horizontally
00231 
00232   new ChartConfig(chart, this);
00233 }


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

Generated on Thu May 20 18:14:58 2010 for Wt by doxygen 1.5.6