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