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->setAutoLayoutEnabled(
true);
173 for (
int i = 1; i < model->columnCount(); ++i) {
174 WDataSeries s(i, BarSeries);
175 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
179 chart->resize(800, 400);
181 chart->setMargin(10, Top | Bottom);
182 chart->setMargin(WLength::Auto, Left | Right);
191 WContainerWidget(parent)
193 new WText(WString::tr(
"scatter plot"),
this);
195 WAbstractItemModel *model = readCsvFile(
196 WApplication::appRoot() +
"timeseries.csv",
this);
204 for (
int i = 0; i < model->rowCount(); ++i) {
205 WString s = asString(model->data(i, 0));
206 WDate d = WDate::fromString(s,
"dd/MM/yy");
207 model->setData(i, 0, d);
211 WContainerWidget *w =
new WContainerWidget(
this);
212 WTableView *table =
new WTableView(w);
214 table->setMargin(10, Top | Bottom);
215 table->setMargin(WLength::Auto, Left | Right);
217 table->setModel(model);
218 table->setSortingEnabled(
false);
219 table->setColumnResizeEnabled(
true);
220 table->setSelectionMode(NoSelection);
221 table->setAlternatingRowColors(
true);
222 table->setColumnAlignment(0, AlignCenter);
223 table->setHeaderAlignment(0, AlignCenter);
224 table->setRowHeight(22);
228 if (WApplication::instance()->environment().ajax()) {
229 table->resize(800, 20 + 5*22);
230 table->setEditTriggers(WAbstractItemView::SingleClicked);
232 table->resize(800, 20 + 5*22 + 25);
233 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
236 WItemDelegate *delegate =
new WItemDelegate(
this);
237 delegate->setTextFormat(
"%.1f");
238 table->setItemDelegate(delegate);
239 table->setItemDelegateForColumn(0,
new WItemDelegate(
this));
241 table->setColumnWidth(0, 80);
242 for (
int i = 1; i < model->columnCount(); ++i)
243 table->setColumnWidth(i, 90);
248 WCartesianChart *chart =
new WCartesianChart(
this);
251 chart->setModel(model);
252 chart->setXSeriesColumn(0);
253 chart->setLegendEnabled(
true);
255 chart->setType(ScatterPlot);
256 chart->axis(XAxis).setScale(DateScale);
259 chart->setAutoLayoutEnabled();
264 for (
int i = 1; i < 3; ++i) {
265 WDataSeries s(i, LineSeries);
266 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
270 chart->resize(800, 400);
272 chart->setMargin(10, Top | Bottom);
273 chart->setMargin(WLength::Auto, Left | Right);
279 WContainerWidget(parent)
281 new WText(WString::tr(
"scatter plot 2"),
this);
283 WStandardItemModel *model =
new WStandardItemModel(40, 2,
this);
284 model->setItemPrototype(
new NumericItem());
285 model->setHeaderData(0, WString(
"X"));
286 model->setHeaderData(1, WString(
"Y = sin(X)"));
288 for (
unsigned i = 0; i < 40; ++i) {
289 double x = (
static_cast<double>(i) - 20) / 4;
291 model->setData(i, 0, x);
292 model->setData(i, 1, sin(x));
298 WCartesianChart *chart =
new WCartesianChart(
this);
299 chart->setModel(model);
300 chart->setXSeriesColumn(0);
301 chart->setLegendEnabled(
true);
303 chart->setType(ScatterPlot);
307 chart->axis(XAxis).setLocation(ZeroValue);
308 chart->axis(YAxis).setLocation(ZeroValue);
311 chart->setAutoLayoutEnabled();
314 WDataSeries s(1, CurveSeries);
315 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
318 chart->resize(800, 300);
320 chart->setMargin(10, Top | Bottom);
321 chart->setMargin(WLength::Auto, Left | Right);
328 WContainerWidget(parent)
330 new WText(WString::tr(
"pie chart"),
this);
332 WStandardItemModel *model =
new WStandardItemModel(
this);
333 model->setItemPrototype(
new NumericItem());
336 model->insertColumns(model->columnCount(), 2);
337 model->setHeaderData(0, WString(
"Item"));
338 model->setHeaderData(1, WString(
"Sales"));
341 model->insertRows(model->rowCount(), 6);
343 model->setData(row, 0, WString(
"Blueberry"));
344 model->setData(row, 1, 120);
347 model->setData(row, 0, WString(
"Cherry"));
348 model->setData(row, 1, 30);
350 model->setData(row, 0, WString(
"Apple"));
351 model->setData(row, 1, 260);
353 model->setData(row, 0, WString(
"Boston Cream"));
354 model->setData(row, 1, 160);
356 model->setData(row, 0, WString(
"Other"));
357 model->setData(row, 1, 40);
359 model->setData(row, 0, WString(
"Vanilla Cream"));
360 model->setData(row, 1, 120);
364 for (
int row = 0; row < model->rowCount(); ++row)
365 for (
int col = 0; col < model->columnCount(); ++col)
366 model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
368 WContainerWidget *w =
new WContainerWidget(
this);
369 WTableView* table =
new WTableView(w);
371 table->setMargin(10, Top | Bottom);
372 table->setMargin(WLength::Auto, Left | Right);
373 table->setSortingEnabled(
true);
374 table->setModel(model);
375 table->setColumnWidth(1, 100);
376 table->setRowHeight(22);
378 if (WApplication::instance()->environment().ajax()) {
379 table->resize(150 + 100 + 14, 20 + 6 * 22);
380 table->setEditTriggers(WAbstractItemView::SingleClicked);
382 table->resize(150 + 100 + 14, WLength::Auto);
383 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
389 WPieChart *chart =
new WPieChart(
this);
390 chart->setModel(model);
391 chart->setLabelsColumn(0);
392 chart->setDataColumn(1);
395 chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
398 chart->setPerspectiveEnabled(
true, 0.2);
399 chart->setShadowEnabled(
true);
402 chart->setExplode(0, 0.3);
404 chart->resize(800, 300);
406 chart->setMargin(10, Top | Bottom);
407 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.