14 #include <Wt/WApplication.h> 16 #include <Wt/WEnvironment.h> 17 #include <Wt/WItemDelegate.h> 18 #include <Wt/WStandardItemModel.h> 21 #include <Wt/WBorderLayout.h> 22 #include <Wt/WFitLayout.h> 24 #include <Wt/WStandardItem.h> 25 #include <Wt/WTableView.h> 27 #include <Wt/Chart/WCartesianChart.h> 28 #include <Wt/Chart/WPieChart.h> 38 class NumericItem :
public WStandardItem {
40 virtual std::unique_ptr<WStandardItem> clone()
const override {
41 return cpp14::make_unique<NumericItem>();
44 virtual void setData(
const cpp17::any &data, ItemDataRole role = ItemDataRole::User)
override {
47 if (role == ItemDataRole::Edit) {
48 std::string s = asString(data).toUTF8();
50 double d = strtod(s.c_str(), &endptr);
57 WStandardItem::setData(data, role);
64 std::shared_ptr<WAbstractItemModel> readCsvFile(
const std::string &fname,
65 WContainerWidget *parent)
67 std::shared_ptr<WStandardItemModel> model
68 = std::make_shared<WStandardItemModel>(0, 0);
69 std::unique_ptr<NumericItem> prototype
70 = cpp14::make_unique<NumericItem>();
71 model->setItemPrototype(std::move(prototype));
72 std::ifstream f(fname.c_str());
77 for (
int row = 0; row < model->rowCount(); ++row)
78 for (
int col = 0; col < model->columnCount(); ++col) {
79 model->item(row, col)->setFlags(ItemFlag::Selectable | ItemFlag::Editable);
95 WString error(WString::tr(
"error-missing-data"));
96 error.arg(fname, CharEncoding::UTF8);
97 parent->addWidget(cpp14::make_unique<WText>(error));
106 this->addWidget(cpp14::make_unique<WText>(WString::tr(
"introduction")));
108 this->addWidget(cpp14::make_unique<CategoryExample>());
109 this->addWidget(cpp14::make_unique<TimeSeriesExample>());
110 this->addWidget(cpp14::make_unique<ScatterPlotExample>());
111 this->addWidget(cpp14::make_unique<PieExample>());
117 this->addWidget(cpp14::make_unique<WText>(WString::tr(
"category chart")));
119 std::shared_ptr<WAbstractItemModel> model
120 = readCsvFile(WApplication::appRoot() +
"category.csv",
this);
126 auto *w = this->addWidget(cpp14::make_unique<WContainerWidget>());
127 auto *table = w->addWidget(cpp14::make_unique<WTableView>());
129 table->setMargin(10, Side::Top | Side::Bottom);
130 table->setMargin(WLength::Auto, Side::Left | Side::Right);
132 table->setModel(model);
133 table->setSortingEnabled(
true);
134 table->setColumnResizeEnabled(
true);
136 table->setAlternatingRowColors(
true);
137 table->setColumnAlignment(0, AlignmentFlag::Center);
138 table->setHeaderAlignment(0, AlignmentFlag::Center);
139 table->setRowHeight(22);
143 if (WApplication::instance()->environment().ajax()) {
144 table->resize(600, 20 + 5*22);
145 table->setEditTriggers(EditTrigger::SingleClicked);
147 table->resize(600, WLength::Auto);
148 table->setEditTriggers(EditTrigger::None);
153 std::shared_ptr<WItemDelegate> delegate
154 = std::make_shared<WItemDelegate>();
155 delegate->setTextFormat(
"%.f");
156 table->setItemDelegate(delegate);
158 table->setColumnWidth(0, 80);
159 for (
int i = 1; i < model->columnCount(); ++i)
160 table->setColumnWidth(i, 120);
165 WCartesianChart *chart = this->addWidget(cpp14::make_unique<WCartesianChart>());
166 chart->setModel(model);
167 chart->setXSeriesColumn(0);
168 chart->setLegendEnabled(
true);
169 chart->setZoomEnabled(
true);
170 chart->setPanEnabled(
true);
173 chart->setAutoLayoutEnabled(
true);
175 chart->setBackground(WColor(200,200,200));
180 for (
int i = 1; i < model->columnCount(); ++i) {
181 std::unique_ptr<WDataSeries> s
182 = cpp14::make_unique<WDataSeries>(i, SeriesType::Bar);
183 s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
184 chart->addSeries(std::move(s));
187 chart->resize(800, 400);
189 chart->setMargin(10, Side::Top | Side::Bottom);
190 chart->setMargin(WLength::Auto, Side::Left | Side::Right);
195 this->addWidget(cpp14::make_unique<ChartConfig>(chart));
201 this->addWidget(cpp14::make_unique<WText>(WString::tr(
"scatter plot")));
203 std::shared_ptr<WAbstractItemModel> model
204 = readCsvFile(WApplication::appRoot() +
"timeseries.csv",
this);
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);
219 auto *w = this->addWidget(cpp14::make_unique<WContainerWidget>());
220 auto *table = w->addWidget(cpp14::make_unique<WTableView>());
222 table->setMargin(10, Side::Top | Side::Bottom);
223 table->setMargin(WLength::Auto, Side::Left | Side::Right);
225 table->setModel(model);
226 table->setSortingEnabled(
false);
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);
236 if (WApplication::instance()->environment().ajax()) {
237 table->resize(800, 20 + 5*22);
238 table->setEditTriggers(EditTrigger::SingleClicked);
240 table->resize(800, 20 + 5*22 + 25);
241 table->setEditTriggers(EditTrigger::None);
244 std::shared_ptr<WItemDelegate> delegate
245 = std::make_shared<WItemDelegate>();
246 delegate->setTextFormat(
"%.1f");
247 table->setItemDelegate(delegate);
249 std::shared_ptr<WItemDelegate> delegateColumn
250 = std::make_shared<WItemDelegate>();
251 table->setItemDelegateForColumn(0, delegateColumn);
253 table->setColumnWidth(0, 80);
254 for (
int i = 1; i < model->columnCount(); ++i)
255 table->setColumnWidth(i, 90);
260 WCartesianChart *chart = this->addWidget(cpp14::make_unique<WCartesianChart>());
263 chart->setModel(model);
264 chart->setXSeriesColumn(0);
265 chart->setLegendEnabled(
true);
266 chart->setZoomEnabled(
true);
267 chart->setPanEnabled(
true);
269 chart->setType(ChartType::Scatter);
270 chart->axis(Axis::X).setScale(AxisScale::Date);
273 chart->setAutoLayoutEnabled();
275 chart->setBackground(WColor(200,200,200));
279 for (
int i = 1; i < 3; ++i) {
280 std::unique_ptr<WDataSeries> s
281 = cpp14::make_unique<WDataSeries>(i, SeriesType::Line);
282 s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
283 chart->addSeries(std::move(s));
286 chart->resize(800, 400);
288 chart->setMargin(10, Side::Top | Side::Bottom);
289 chart->setMargin(WLength::Auto, Side::Left | Side::Right);
291 this->addWidget(cpp14::make_unique<ChartConfig>(chart));
297 this->addWidget(cpp14::make_unique<WText>(WString::tr(
"scatter plot 2")));
299 std::shared_ptr<WStandardItemModel> model
300 = std::make_shared<WStandardItemModel>(40, 2);
301 std::unique_ptr<NumericItem> prototype
302 = cpp14::make_unique<NumericItem>();
303 model->setItemPrototype(std::move(prototype));
304 model->setHeaderData(0, WString(
"X"));
305 model->setHeaderData(1, WString(
"Y = sin(X)"));
307 for (
unsigned i = 0; i < 40; ++i) {
308 double x = (
static_cast<double>(i) - 20) / 4;
310 model->setData(i, 0, x);
311 model->setData(i, 1, sin(x));
317 WCartesianChart *chart = this->addWidget(cpp14::make_unique<WCartesianChart>());
318 chart->setModel(model);
319 chart->setXSeriesColumn(0);
320 chart->setLegendEnabled(
true);
321 chart->setZoomEnabled(
true);
322 chart->setPanEnabled(
true);
323 chart->setCrosshairEnabled(
true);
325 chart->setBackground(WColor(200,200,200));
327 chart->setType(ChartType::Scatter);
331 chart->axis(Axis::X).setLocation(AxisValue::Zero);
332 chart->axis(Axis::Y).setLocation(AxisValue::Zero);
335 chart->setAutoLayoutEnabled();
338 std::unique_ptr<WDataSeries> s
339 = cpp14::make_unique<WDataSeries>(1, SeriesType::Curve);
340 s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
341 chart->addSeries(std::move(s));
343 chart->resize(800, 300);
345 chart->setMargin(10, Side::Top | Side::Bottom);
346 chart->setMargin(WLength::Auto, Side::Left | Side::Right);
348 ChartConfig *config = this->addWidget(cpp14::make_unique<ChartConfig>(chart));
355 this->addWidget(cpp14::make_unique<WText>(WString::tr(
"pie chart")));
357 std::shared_ptr<WStandardItemModel> model
358 = std::make_shared<WStandardItemModel>();
359 std::unique_ptr<NumericItem> prototype
360 = cpp14::make_unique<NumericItem>();
361 model->setItemPrototype(std::move(prototype));
364 model->insertColumns(model->columnCount(), 2);
365 model->setHeaderData(0, WString(
"Item"));
366 model->setHeaderData(1, WString(
"Sales"));
369 model->insertRows(model->rowCount(), 6);
371 model->setData(row, 0, WString(
"Blueberry"));
372 model->setData(row, 1, 120);
375 model->setData(row, 0, WString(
"Cherry"));
376 model->setData(row, 1, 30);
378 model->setData(row, 0, WString(
"Apple"));
379 model->setData(row, 1, 260);
381 model->setData(row, 0, WString(
"Boston Cream"));
382 model->setData(row, 1, 160);
384 model->setData(row, 0, WString(
"Other"));
385 model->setData(row, 1, 40);
387 model->setData(row, 0, WString(
"Vanilla Cream"));
388 model->setData(row, 1, 120);
392 for (
int row = 0; row < model->rowCount(); ++row)
393 for (
int col = 0; col < model->columnCount(); ++col)
394 model->item(row, col)->setFlags(ItemFlag::Selectable | ItemFlag::Editable);
396 WContainerWidget *w = this->addWidget(cpp14::make_unique<WContainerWidget>());
397 WTableView* table = w->addWidget(cpp14::make_unique<WTableView>());
399 table->setMargin(10, Side::Top | Side::Bottom);
400 table->setMargin(WLength::Auto, Side::Left | Side::Right);
401 table->setSortingEnabled(
true);
402 table->setModel(model);
403 table->setColumnWidth(1, 100);
404 table->setRowHeight(22);
406 if (WApplication::instance()->environment().ajax()) {
407 table->resize(150 + 100 + 14, 20 + 6 * 22);
408 table->setEditTriggers(EditTrigger::SingleClicked);
410 table->resize(150 + 100 + 14, WLength::Auto);
411 table->setEditTriggers(EditTrigger::None);
417 WPieChart *chart = this->addWidget(cpp14::make_unique<WPieChart>());
418 chart->setModel(model);
419 chart->setLabelsColumn(0);
420 chart->setDataColumn(1);
423 chart->setDisplayLabels(LabelOption::Outside | LabelOption::TextLabel | LabelOption::TextPercentage);
426 chart->setPerspectiveEnabled(
true, 0.2);
427 chart->setShadowEnabled(
true);
430 chart->setExplode(0, 0.3);
432 chart->resize(800, 300);
434 chart->setMargin(10, Side::Top | Side::Bottom);
435 chart->setMargin(WLength::Auto, Side::Left | Side::Right);
ChartsExample()
Constructor.
TimeSeriesExample()
Creates the time series scatter plot example.
PieExample()
Creates the pie chart example.
void readFromCsv(std::istream &f, std::shared_ptr< WAbstractItemModel > model, int numRows, bool firstLineIsHeaders)
void setValueFill(Wt::Chart::FillRangeType fill)
A class that allows configuration of a cartesian chart.
CategoryExample()
Creates the category chart example.
ScatterPlotExample()
Creates the scatter plot example.