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> 40 virtual std::unique_ptr<WStandardItem> clone()
const override {
41 return cpp14::make_unique<NumericItem>();
50 double d = strtod(s.c_str(), &endptr);
64 std::shared_ptr<WAbstractItemModel> readCsvFile(
const std::string &fname,
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) {
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);
180 for (
int i = 1; i < model->columnCount(); ++i) {
181 std::unique_ptr<WDataSeries> s
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) {
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);
279 for (
int i = 1; i < 3; ++i) {
280 std::unique_ptr<WDataSeries> s
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));
338 std::unique_ptr<WDataSeries> s
345 chart->
setMargin(10, Side::Top | Side::Bottom);
346 chart->
setMargin(WLength::Auto, Side::Left | Side::Right);
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)
399 table->
setMargin(10, Side::Top | Side::Bottom);
400 table->
setMargin(WLength::Auto, Side::Left | Side::Right);
406 if (WApplication::instance()->environment().ajax()) {
407 table->
resize(150 + 100 + 14, 20 + 6 * 22);
410 table->
resize(150 + 100 + 14, WLength::Auto);
434 chart->
setMargin(10, Side::Top | Side::Bottom);
435 chart->
setMargin(WLength::Auto, Side::Left | Side::Right);
static constexpr int Edit
void setExplode(int modelRow, double factor)
ChartsExample()
Constructor.
virtual void resize(const WLength &width, const WLength &height) override
void setPanEnabled(bool pan=true)
TimeSeriesExample()
Creates the time series scatter plot example.
void setLabelsColumn(int column)
void addSeries(std::unique_ptr< WDataSeries > series)
static constexpr int User
void setLocation(AxisValue value)
PieExample()
Creates the pie chart example.
void setDisplayLabels(WFlags< LabelOption > options)
virtual void setModel(const std::shared_ptr< WAbstractItemModel > &model) override
void setType(ChartType type)
virtual void setData(const cpp17::any &data, ItemDataRole role=ItemDataRole::User)
void setPerspectiveEnabled(bool enabled, double height=1.0)
void setCrosshairEnabled(bool crosshair=true)
std::string toUTF8() const
void setZoomEnabled(bool zoom=true)
void setBackground(const WBrush &background)
void setScale(AxisScale scale)
static WString tr(const char *key)
void setXSeriesColumn(int modelColumn)
void readFromCsv(std::istream &f, std::shared_ptr< WAbstractItemModel > model, int numRows, bool firstLineIsHeaders)
WString asString(const cpp17::any &v, const WString &formatString=WString())
void setModel(const std::shared_ptr< WAbstractItemModel > &model)
void setValueFill(Wt::Chart::FillRangeType fill)
void setEditTriggers(WFlags< EditTrigger > editTriggers)
void setShadowEnabled(bool enabled)
virtual void setColumnWidth(int column, const WLength &width) override
void setAutoLayoutEnabled(bool enabled=true)
void setDataColumn(int modelColumn)
void setLegendEnabled(bool enabled)
void setSortingEnabled(bool enabled)
A class that allows configuration of a cartesian chart.
CategoryExample()
Creates the category chart example.
ScatterPlotExample()
Creates the scatter plot example.
virtual void setRowHeight(const WLength &rowHeight) override