14 #include <Wt/WApplication>
16 #include <Wt/WEnvironment>
17 #include <Wt/WItemDelegate>
18 #include <Wt/WStandardItemModel>
21 #include <Wt/WBorderLayout>
22 #include <Wt/WFitLayout>
24 #include <Wt/WStandardItem>
25 #include <Wt/WTableView>
27 #include <Wt/Chart/WCartesianChart>
28 #include <Wt/Chart/WPieChart>
38 class NumericItem :
public WStandardItem {
40 virtual NumericItem *clone()
const {
41 return new NumericItem();
44 virtual void setData(
const boost::any &data,
int role = UserRole) {
47 if (role == EditRole) {
48 std::string s = Wt::asString(data).toUTF8();
50 double d = strtod(s.c_str(), &endptr);
57 WStandardItem::setData(data, role);
64 WAbstractItemModel *readCsvFile(
const std::string &fname,
65 WContainerWidget *parent)
67 WStandardItemModel *model =
new WStandardItemModel(0, 0, parent);
68 model->setItemPrototype(
new NumericItem());
69 std::ifstream f(fname.c_str());
74 for (
int row = 0; row < model->rowCount(); ++row)
75 for (
int col = 0; col < model->columnCount(); ++col) {
76 model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
92 WString error(WString::tr(
"error-missing-data"));
93 error.arg(fname, UTF8);
94 new WText(error, parent);
101 : WContainerWidget(root)
103 new WText(WString::tr(
"introduction"),
this);
112 WContainerWidget(parent)
114 new WText(WString::tr(
"category chart"),
this);
116 WAbstractItemModel *model
117 = readCsvFile(WApplication::appRoot() +
"category.csv",
this);
123 WContainerWidget *w =
new WContainerWidget(
this);
124 WTableView *table =
new WTableView(w);
126 table->setMargin(10, Top | Bottom);
127 table->setMargin(WLength::Auto, Left | Right);
129 table->setModel(model);
130 table->setSortingEnabled(
true);
131 table->setColumnResizeEnabled(
true);
133 table->setAlternatingRowColors(
true);
134 table->setColumnAlignment(0, AlignCenter);
135 table->setHeaderAlignment(0, AlignCenter);
136 table->setRowHeight(22);
140 if (WApplication::instance()->environment().ajax()) {
141 table->resize(600, 20 + 5*22);
142 table->setEditTriggers(WAbstractItemView::SingleClicked);
144 table->resize(600, WLength::Auto);
145 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
150 WItemDelegate *delegate =
new WItemDelegate(
this);
151 delegate->setTextFormat(
"%.f");
152 table->setItemDelegate(delegate);
154 table->setColumnWidth(0, 80);
155 for (
int i = 1; i < model->columnCount(); ++i)
156 table->setColumnWidth(i, 120);
161 WCartesianChart *chart =
new WCartesianChart(
this);
163 chart->setModel(model);
164 chart->setXSeriesColumn(0);
165 chart->setLegendEnabled(
true);
168 chart->setPlotAreaPadding(80, Left);
169 chart->setPlotAreaPadding(40, Top | Bottom);
174 for (
int i = 1; i < model->columnCount(); ++i) {
175 WDataSeries s(i, BarSeries);
176 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
180 chart->resize(800, 400);
182 chart->setMargin(10, Top | Bottom);
183 chart->setMargin(WLength::Auto, Left | Right);
192 WContainerWidget(parent)
194 new WText(WString::tr(
"scatter plot"),
this);
196 WAbstractItemModel *model = readCsvFile(
197 WApplication::appRoot() +
"timeseries.csv",
this);
205 for (
int i = 0; i < model->rowCount(); ++i) {
206 WString s = asString(model->data(i, 0));
207 WDate d = WDate::fromString(s,
"dd/MM/yy");
208 model->setData(i, 0, d);
212 WContainerWidget *w =
new WContainerWidget(
this);
213 WTableView *table =
new WTableView(w);
215 table->setMargin(10, Top | Bottom);
216 table->setMargin(WLength::Auto, Left | Right);
218 table->setModel(model);
219 table->setSortingEnabled(
false);
220 table->setColumnResizeEnabled(
true);
221 table->setSelectionMode(NoSelection);
222 table->setAlternatingRowColors(
true);
223 table->setColumnAlignment(0, AlignCenter);
224 table->setHeaderAlignment(0, AlignCenter);
225 table->setRowHeight(22);
229 if (WApplication::instance()->environment().ajax()) {
230 table->resize(800, 20 + 5*22);
231 table->setEditTriggers(WAbstractItemView::SingleClicked);
233 table->resize(800, 20 + 5*22 + 25);
234 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
237 WItemDelegate *delegate =
new WItemDelegate(
this);
238 delegate->setTextFormat(
"%.1f");
239 table->setItemDelegate(delegate);
240 table->setItemDelegateForColumn(0,
new WItemDelegate(
this));
242 table->setColumnWidth(0, 80);
243 for (
int i = 1; i < model->columnCount(); ++i)
244 table->setColumnWidth(i, 90);
249 WCartesianChart *chart =
new WCartesianChart(
this);
252 chart->setModel(model);
253 chart->setXSeriesColumn(0);
254 chart->setLegendEnabled(
true);
256 chart->setType(ScatterPlot);
257 chart->axis(XAxis).setScale(DateScale);
260 chart->setPlotAreaPadding(80, Left);
261 chart->setPlotAreaPadding(40, Top | Bottom);
266 for (
int i = 1; i < 3; ++i) {
267 WDataSeries s(i, LineSeries);
268 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
272 chart->resize(800, 400);
274 chart->setMargin(10, Top | Bottom);
275 chart->setMargin(WLength::Auto, Left | Right);
281 WContainerWidget(parent)
283 new WText(WString::tr(
"scatter plot 2"),
this);
285 WStandardItemModel *model =
new WStandardItemModel(40, 2,
this);
286 model->setItemPrototype(
new NumericItem());
287 model->setHeaderData(0, WString(
"X"));
288 model->setHeaderData(1, WString(
"Y = sin(X)"));
290 for (
unsigned i = 0; i < 40; ++i) {
291 double x = (
static_cast<double>(i) - 20) / 4;
293 model->setData(i, 0, x);
294 model->setData(i, 1, sin(x));
300 WCartesianChart *chart =
new WCartesianChart(
this);
301 chart->setModel(model);
302 chart->setXSeriesColumn(0);
303 chart->setLegendEnabled(
true);
305 chart->setType(ScatterPlot);
309 chart->axis(XAxis).setLocation(ZeroValue);
310 chart->axis(YAxis).setLocation(ZeroValue);
313 chart->setPlotAreaPadding(80, Left);
314 chart->setPlotAreaPadding(40, Top | Bottom);
317 WDataSeries s(1, CurveSeries);
318 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
321 chart->resize(800, 300);
323 chart->setMargin(10, Top | Bottom);
324 chart->setMargin(WLength::Auto, Left | Right);
331 WContainerWidget(parent)
333 new WText(WString::tr(
"pie chart"),
this);
335 WStandardItemModel *model =
new WStandardItemModel(
this);
336 model->setItemPrototype(
new NumericItem());
339 model->insertColumns(model->columnCount(), 2);
340 model->setHeaderData(0, WString(
"Item"));
341 model->setHeaderData(1, WString(
"Sales"));
344 model->insertRows(model->rowCount(), 6);
346 model->setData(row, 0, WString(
"Blueberry"));
347 model->setData(row, 1, 120);
350 model->setData(row, 0, WString(
"Cherry"));
351 model->setData(row, 1, 30);
353 model->setData(row, 0, WString(
"Apple"));
354 model->setData(row, 1, 260);
356 model->setData(row, 0, WString(
"Boston Cream"));
357 model->setData(row, 1, 160);
359 model->setData(row, 0, WString(
"Other"));
360 model->setData(row, 1, 40);
362 model->setData(row, 0, WString(
"Vanilla Cream"));
363 model->setData(row, 1, 120);
367 for (
int row = 0; row < model->rowCount(); ++row)
368 for (
int col = 0; col < model->columnCount(); ++col)
369 model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
371 WContainerWidget *w =
new WContainerWidget(
this);
372 WTableView* table =
new WTableView(w);
374 table->setMargin(10, Top | Bottom);
375 table->setMargin(WLength::Auto, Left | Right);
376 table->setSortingEnabled(
true);
377 table->setModel(model);
378 table->setColumnWidth(1, 100);
379 table->setRowHeight(22);
381 if (WApplication::instance()->environment().ajax()) {
382 table->resize(150 + 100 + 14, 20 + 6 * 22);
383 table->setEditTriggers(WAbstractItemView::SingleClicked);
385 table->resize(150 + 100 + 14, WLength::Auto);
386 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
392 WPieChart *chart =
new WPieChart(
this);
393 chart->setModel(model);
394 chart->setLabelsColumn(0);
395 chart->setDataColumn(1);
398 chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
401 chart->setPerspectiveEnabled(
true, 0.2);
402 chart->setShadowEnabled(
true);
405 chart->setExplode(0, 0.3);
407 chart->resize(800, 300);
409 chart->setMargin(10, Top | Bottom);
410 chart->setMargin(WLength::Auto, Left | Right);
A Widget that demonstrates a scatter plot.
ChartsExample(Wt::WContainerWidget *root)
Constructor.
A Widget that demonstrates a Pie chart.
CategoryExample(Wt::WContainerWidget *parent)
Creates the category chart example.
A Widget that demonstrates a category chart.
A widget that demonstrates a times series chart.
TimeSeriesExample(Wt::WContainerWidget *parent)
Creates the time series scatter plot example.
void setValueFill(Wt::Chart::FillRangeType fill)
PieExample(Wt::WContainerWidget *parent)
Creates the pie chart example.
A class that allows configuration of a cartesian chart.
void readFromCsv(std::istream &f, Wt::WAbstractItemModel *model, int numRows, bool firstLineIsHeaders)
Utility function that reads a model from a CSV file.
ScatterPlotExample(Wt::WContainerWidget *parent)
Creates the scatter plot example.