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