00001
00002
00003
00004
00005
00006
00007 #include <math.h>
00008 #include <fstream>
00009
00010 #include "ChartsExample.h"
00011 #include "ChartConfig.h"
00012 #include "CsvUtil.h"
00013
00014 #include <Wt/WApplication>
00015 #include <Wt/WDate>
00016 #include <Wt/WEnvironment>
00017 #include <Wt/WItemDelegate>
00018 #include <Wt/WStandardItemModel>
00019 #include <Wt/WText>
00020
00021 #include <Wt/WBorderLayout>
00022 #include <Wt/WFitLayout>
00023
00024 #include <Wt/WStandardItem>
00025 #include <Wt/WTableView>
00026
00027 #include <Wt/Chart/WCartesianChart>
00028 #include <Wt/Chart/WPieChart>
00029
00030 using namespace Wt;
00031 using namespace Wt::Chart;
00032 namespace {
00033
00034
00035
00036
00037 WAbstractItemModel *readCsvFile(const std::string &fname,
00038 WContainerWidget *parent)
00039 {
00040 WStandardItemModel *model = new WStandardItemModel(0, 0, parent);
00041 std::ifstream f(fname.c_str());
00042
00043 if (f) {
00044 readFromCsv(f, model);
00045
00046 for (int row = 0; row < model->rowCount(); ++row)
00047 for (int col = 0; col < model->columnCount(); ++col) {
00048 model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 }
00061
00062 return model;
00063 } else {
00064 WString error(WString::tr("error-missing-data"));
00065 error.arg(fname, UTF8);
00066 new WText(error, parent);
00067 return 0;
00068 }
00069 }
00070 }
00071
00072 ChartsExample::ChartsExample(WContainerWidget *root)
00073 : WContainerWidget(root)
00074 {
00075 new WText(WString::tr("introduction"), this);
00076
00077 new CategoryExample(this);
00078 new TimeSeriesExample(this);
00079 new ScatterPlotExample(this);
00080 new PieExample(this);
00081 }
00082
00083 CategoryExample::CategoryExample(Wt::WContainerWidget *parent):
00084 WContainerWidget(parent)
00085 {
00086 new WText(WString::tr("category chart"), this);
00087
00088 WAbstractItemModel *model
00089 = readCsvFile(WApplication::appRoot() + "category.csv", this);
00090
00091 if (!model)
00092 return;
00093
00094
00095 WContainerWidget *w = new WContainerWidget(this);
00096 WTableView *table = new WTableView(w);
00097
00098 table->setMargin(10, Top | Bottom);
00099 table->setMargin(WLength::Auto, Left | Right);
00100
00101 table->setModel(model);
00102 table->setSortingEnabled(true);
00103 table->setColumnResizeEnabled(true);
00104 table->setSelectionMode(NoSelection);
00105 table->setAlternatingRowColors(true);
00106 table->setColumnAlignment(0, AlignCenter);
00107 table->setHeaderAlignment(0, AlignCenter);
00108 table->setRowHeight(22);
00109
00110
00111
00112 if (WApplication::instance()->environment().ajax()) {
00113 table->resize(600, 20 + 5*22);
00114 table->setEditTriggers(WAbstractItemView::SingleClicked);
00115 } else {
00116 table->resize(600, WLength::Auto);
00117 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
00118 }
00119
00120
00121
00122 WItemDelegate *delegate = new WItemDelegate(this);
00123 delegate->setTextFormat("%.f");
00124 table->setItemDelegate(delegate);
00125
00126 table->setColumnWidth(0, 80);
00127 for (int i = 1; i < model->columnCount(); ++i)
00128 table->setColumnWidth(i, 120);
00129
00130
00131
00132
00133 WCartesianChart *chart = new WCartesianChart(this);
00134 chart->setModel(model);
00135 chart->setXSeriesColumn(0);
00136 chart->setLegendEnabled(true);
00137
00138
00139 chart->setPlotAreaPadding(100, Left);
00140 chart->setPlotAreaPadding(50, Top | Bottom);
00141
00142
00143
00144
00145 for (int i = 1; i < model->columnCount(); ++i) {
00146 WDataSeries s(i, BarSeries);
00147 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00148 chart->addSeries(s);
00149 }
00150
00151 chart->resize(800, 400);
00152
00153 chart->setMargin(10, Top | Bottom);
00154 chart->setMargin(WLength::Auto, Left | Right);
00155
00156
00157
00158
00159 new ChartConfig(chart, this);
00160 }
00161
00162 TimeSeriesExample::TimeSeriesExample(Wt::WContainerWidget *parent):
00163 WContainerWidget(parent)
00164 {
00165 new WText(WString::tr("scatter plot"), this);
00166
00167 WAbstractItemModel *model = readCsvFile(
00168 WApplication::appRoot() + "timeseries.csv", this);
00169
00170 if (!model)
00171 return;
00172
00173
00174
00175
00176 for (int i = 0; i < model->rowCount(); ++i) {
00177 WString s = asString(model->data(i, 0));
00178 WDate d = WDate::fromString(s, "dd/MM/yy");
00179 model->setData(i, 0, d);
00180 }
00181
00182
00183 WContainerWidget *w = new WContainerWidget(this);
00184 WTableView *table = new WTableView(w);
00185
00186 table->setMargin(10, Top | Bottom);
00187 table->setMargin(WLength::Auto, Left | Right);
00188
00189 table->setModel(model);
00190 table->setSortingEnabled(false);
00191 table->setColumnResizeEnabled(true);
00192 table->setSelectionMode(NoSelection);
00193 table->setAlternatingRowColors(true);
00194 table->setColumnAlignment(0, AlignCenter);
00195 table->setHeaderAlignment(0, AlignCenter);
00196 table->setRowHeight(22);
00197
00198
00199
00200 if (WApplication::instance()->environment().ajax()) {
00201 table->resize(800, 20 + 5*22);
00202 table->setEditTriggers(WAbstractItemView::SingleClicked);
00203 } else {
00204 table->resize(800, 20 + 5*22 + 25);
00205 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
00206 }
00207
00208 WItemDelegate *delegate = new WItemDelegate(this);
00209 delegate->setTextFormat("%.1f");
00210 table->setItemDelegate(delegate);
00211 table->setItemDelegateForColumn(0, new WItemDelegate(this));
00212
00213 table->setColumnWidth(0, 80);
00214 for (int i = 1; i < model->columnCount(); ++i)
00215 table->setColumnWidth(i, 90);
00216
00217
00218
00219
00220 WCartesianChart *chart = new WCartesianChart(this);
00221
00222
00223 chart->setModel(model);
00224 chart->setXSeriesColumn(0);
00225 chart->setLegendEnabled(true);
00226
00227 chart->setType(ScatterPlot);
00228 chart->axis(XAxis).setScale(DateScale);
00229
00230
00231 chart->setPlotAreaPadding(100, Left);
00232 chart->setPlotAreaPadding(50, Top | Bottom);
00233
00234
00235
00236
00237 for (int i = 1; i < 3; ++i) {
00238 WDataSeries s(i, LineSeries);
00239 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00240 chart->addSeries(s);
00241 }
00242
00243 chart->resize(800, 400);
00244
00245 chart->setMargin(10, Top | Bottom);
00246 chart->setMargin(WLength::Auto, Left | Right);
00247
00248 new ChartConfig(chart, this);
00249 }
00250
00251 ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
00252 WContainerWidget(parent)
00253 {
00254 new WText(WString::tr("scatter plot 2"), this);
00255
00256 WStandardItemModel *model = new WStandardItemModel(40, 2, this);
00257 model->setHeaderData(0, WString("X"));
00258 model->setHeaderData(1, WString("Y = sin(X)"));
00259
00260 for (unsigned i = 0; i < 40; ++i) {
00261 double x = (static_cast<double>(i) - 20) / 4;
00262
00263 model->setData(i, 0, x);
00264 model->setData(i, 1, sin(x));
00265 }
00266
00267
00268
00269
00270 WCartesianChart *chart = new WCartesianChart(this);
00271 chart->setModel(model);
00272 chart->setXSeriesColumn(0);
00273 chart->setLegendEnabled(true);
00274
00275 chart->setType(ScatterPlot);
00276
00277
00278
00279 chart->axis(XAxis).setLocation(ZeroValue);
00280 chart->axis(YAxis).setLocation(ZeroValue);
00281
00282
00283 chart->setPlotAreaPadding(100, Left);
00284 chart->setPlotAreaPadding(50, Top | Bottom);
00285
00286
00287 WDataSeries s(1, CurveSeries);
00288 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00289 chart->addSeries(s);
00290
00291 chart->resize(800, 300);
00292
00293 chart->setMargin(10, Top | Bottom);
00294 chart->setMargin(WLength::Auto, Left | Right);
00295
00296 ChartConfig *config = new ChartConfig(chart, this);
00297 config->setValueFill(ZeroValueFill);
00298 }
00299
00300 PieExample::PieExample(WContainerWidget *parent):
00301 WContainerWidget(parent)
00302 {
00303 new WText(WString::tr("pie chart"), this);
00304
00305 WStandardItemModel *model = new WStandardItemModel(this);
00306
00307
00308 model->insertColumns(model->columnCount(), 2);
00309 model->setHeaderData(0, WString("Item"));
00310 model->setHeaderData(1, WString("Sales"));
00311
00312
00313 model->insertRows(model->rowCount(), 6);
00314 int row = 0;
00315 model->setData(row, 0, WString("Blueberry"));
00316 model->setData(row, 1, 120);
00317
00318 row++;
00319 model->setData(row, 0, WString("Cherry"));
00320 model->setData(row, 1, 30);
00321 row++;
00322 model->setData(row, 0, WString("Apple"));
00323 model->setData(row, 1, 260);
00324 row++;
00325 model->setData(row, 0, WString("Boston Cream"));
00326 model->setData(row, 1, 160);
00327 row++;
00328 model->setData(row, 0, WString("Other"));
00329 model->setData(row, 1, 40);
00330 row++;
00331 model->setData(row, 0, WString("Vanilla Cream"));
00332 model->setData(row, 1, 120);
00333 row++;
00334
00335
00336 for (int row = 0; row < model->rowCount(); ++row)
00337 for (int col = 0; col < model->columnCount(); ++col)
00338 model->item(row, col)->setFlags(ItemIsSelectable | ItemIsEditable);
00339
00340 WContainerWidget *w = new WContainerWidget(this);
00341 WTableView* table = new WTableView(w);
00342
00343 table->setMargin(10, Top | Bottom);
00344 table->setMargin(WLength::Auto, Left | Right);
00345 table->setSortingEnabled(true);
00346 table->setModel(model);
00347 table->setColumnWidth(1, 100);
00348 table->setRowHeight(22);
00349
00350 if (WApplication::instance()->environment().ajax()) {
00351 table->resize(150 + 100 + 14, 20 + 6 * 22);
00352 table->setEditTriggers(WAbstractItemView::SingleClicked);
00353 } else {
00354 table->resize(150 + 100 + 14, WLength::Auto);
00355 table->setEditTriggers(WAbstractItemView::NoEditTrigger);
00356 }
00357
00358
00359
00360
00361 WPieChart *chart = new WPieChart(this);
00362 chart->setModel(model);
00363 chart->setLabelsColumn(0);
00364 chart->setDataColumn(1);
00365
00366
00367 chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
00368
00369
00370 chart->setPerspectiveEnabled(true, 0.2);
00371 chart->setShadowEnabled(true);
00372
00373
00374 chart->setExplode(0, 0.3);
00375
00376 chart->resize(800, 300);
00377
00378 chart->setMargin(10, Top | Bottom);
00379 chart->setMargin(WLength::Auto, Left | Right);
00380 }
00381