Wt examples 4.8.2
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 ()
 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 ( )

Creates the time series scatter plot example.

Definition at line 198 of file ChartsExample.C.

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

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

Generated on Sat Nov 5 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.5