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/WContainerWidget>
00015 #include <Wt/WDate>
00016 #include <Wt/WEnvironment>
00017 #include <Wt/WStandardItemModel>
00018 #include <Wt/WText>
00019
00020 #include <Wt/WBorderLayout>
00021 #include <Wt/WFitLayout>
00022
00023 #include <Wt/Ext/Calendar>
00024 #include <Wt/Ext/Container>
00025 #include <Wt/Ext/DateField>
00026 #include <Wt/Ext/LineEdit>
00027 #include <Wt/Ext/NumberField>
00028 #include <Wt/Ext/Panel>
00029 #include <Wt/Ext/TableView>
00030
00031 #include <Wt/Chart/WCartesianChart>
00032 #include <Wt/Chart/WPieChart>
00033
00034 using namespace Wt;
00035 using namespace Wt::Chart;
00036
00037 ChartsExample::ChartsExample(const WEnvironment& env)
00038 : WApplication(env)
00039 {
00040 setTitle("Charts example");
00041
00042 messageResourceBundle().use("charts");
00043
00044 root()->setPadding(10);
00045
00046 new WText(WString::tr("introduction"), root());
00047
00048 categoryExample();
00049 timeSeriesExample();
00050 scatterPlotExample();
00051 pieExample();
00052
00053
00054
00055
00056 useStyleSheet("charts.css");
00057 }
00058
00059 void ChartsExample::categoryExample()
00060 {
00061 new WText(WString::tr("category chart"), root());
00062
00063 WAbstractItemModel *model = readCsvFile("category.csv");
00064
00065 if (!model)
00066 return;
00067
00068
00069
00070
00071
00072 if (environment().javaScript()) {
00073 WContainerWidget *w = new WContainerWidget(root());
00074 Ext::TableView *table = new Ext::TableView(w);
00075 table->setMargin(10, Top | Bottom);
00076 table->setMargin(WLength(), Left | Right);
00077 table->resize(500, 175);
00078 table->setModel(model);
00079 table->setAutoExpandColumn(0);
00080
00081 table->setEditor(0, new Ext::LineEdit());
00082
00083 for (int i = 1; i < model->columnCount(); ++i) {
00084 Ext::NumberField *nf = new Ext::NumberField();
00085 table->setEditor(i, nf);
00086 }
00087 }
00088
00089
00090
00091
00092 WCartesianChart *chart = new WCartesianChart(root());
00093 chart->setModel(model);
00094 chart->setXSeriesColumn(0);
00095 chart->setLegendEnabled(true);
00096
00097
00098 chart->setPlotAreaPadding(100, Left);
00099 chart->setPlotAreaPadding(50, Top | Bottom);
00100
00101 chart->axis(YAxis).setBreak(70, 110);
00102
00103
00104
00105
00106 for (int i = 1; i < model->columnCount(); ++i) {
00107 WDataSeries s(i, BarSeries);
00108 chart->addSeries(s);
00109 }
00110
00111 chart->resize(800, 400);
00112
00113 chart->setMargin(10, Top | Bottom);
00114 chart->setMargin(WLength(), Left | Right);
00115
00116 new ChartConfig(chart, root());
00117 }
00118
00119 void ChartsExample::timeSeriesExample()
00120 {
00121 new WText(WString::tr("scatter plot"), root());
00122
00123 WAbstractItemModel *model = readCsvFile("timeseries.csv");
00124
00125 if (!model)
00126 return;
00127
00128
00129
00130
00131 for (int i = 0; i < model->rowCount(); ++i) {
00132 WString s = asString(model->data(i, 0));
00133 WDate d = WDate::fromString(s, "dd/MM/yy");
00134 model->setData(i, 0, boost::any(d));
00135 }
00136
00137
00138
00139
00140 WCartesianChart *chart = new WCartesianChart(root());
00141 chart->setModel(model);
00142 chart->setXSeriesColumn(0);
00143 chart->setLegendEnabled(true);
00144
00145 chart->setType(ScatterPlot);
00146 chart->axis(XAxis).setScale(DateScale);
00147
00148
00149 chart->setPlotAreaPadding(100, Left);
00150 chart->setPlotAreaPadding(50, Top | Bottom);
00151
00152
00153
00154
00155 for (int i = 1; i < 3; ++i) {
00156 WDataSeries s(i, LineSeries);
00157 chart->addSeries(s);
00158 }
00159
00160 chart->resize(800, 400);
00161
00162 chart->setMargin(10, Top | Bottom);
00163 chart->setMargin(WLength(), Left | Right);
00164
00165 new ChartConfig(chart, root());
00166 }
00167
00168 void ChartsExample::scatterPlotExample()
00169 {
00170 new WText(WString::tr("scatter plot 2"), root());
00171
00172 WStandardItemModel *model = new WStandardItemModel(100, 2, this);
00173 model->setHeaderData(0, boost::any(WString("X")));
00174 model->setHeaderData(1, boost::any(WString("Y = sin(X)")));
00175
00176 for (unsigned i = 0; i < 40; ++i) {
00177 double x = (static_cast<double>(i) - 20) / 4;
00178
00179 model->setData(i, 0, boost::any(x));
00180 model->setData(i, 1, boost::any(sin(x)));
00181 }
00182
00183
00184
00185
00186 WCartesianChart *chart = new WCartesianChart(root());
00187 chart->setModel(model);
00188 chart->setXSeriesColumn(0);
00189 chart->setLegendEnabled(true);
00190
00191 chart->setType(ScatterPlot);
00192
00193
00194
00195 chart->axis(XAxis).setLocation(ZeroValue);
00196 chart->axis(YAxis).setLocation(ZeroValue);
00197
00198
00199 chart->setPlotAreaPadding(100, Left);
00200 chart->setPlotAreaPadding(50, Top | Bottom);
00201
00202
00203 chart->addSeries(WDataSeries(1, CurveSeries));
00204
00205 chart->resize(800, 300);
00206
00207 chart->setMargin(10, Top | Bottom);
00208 chart->setMargin(WLength(), Left | Right);
00209
00210 ChartConfig *config = new ChartConfig(chart, root());
00211 config->setValueFill(ZeroValueFill);
00212 }
00213
00214 void ChartsExample::pieExample()
00215 {
00216 new WText(WString::tr("pie chart"), root());
00217
00218 WAbstractItemModel *model = readCsvFile("pie.csv");
00219
00220 if (!model)
00221 return;
00222
00223
00224
00225
00226
00227 if (environment().javaScript()) {
00228 WContainerWidget *w = new WContainerWidget(root());
00229 Ext::TableView *table = new Ext::TableView(w);
00230 table->setMargin(10, Top | Bottom);
00231 table->setMargin(WLength(), Left | Right);
00232 table->resize(300, 175);
00233 table->setModel(model);
00234 table->setAutoExpandColumn(0);
00235
00236 table->setEditor(0, new Ext::LineEdit());
00237
00238 for (int i = 1; i < model->columnCount(); ++i)
00239 table->setEditor(i, new Ext::NumberField());
00240 }
00241
00242
00243
00244
00245 WPieChart *chart = new WPieChart(root());
00246 chart->setModel(model);
00247 chart->setLabelsColumn(0);
00248 chart->setDataColumn(1);
00249
00250
00251 chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
00252
00253
00254 chart->setPerspectiveEnabled(true, 0.2);
00255
00256
00257 chart->setExplode(0, 0.3);
00258
00259 chart->resize(800, 300);
00260
00261 chart->setMargin(10, Top | Bottom);
00262 chart->setMargin(WLength(), Left | Right);
00263 }
00264
00265 WAbstractItemModel *ChartsExample::readCsvFile(const char *fname)
00266 {
00267 WStandardItemModel *model = new WStandardItemModel(0, 0, this);
00268 std::ifstream f(fname);
00269
00270 if (f) {
00271 readFromCsv(f, model);
00272 return model;
00273 } else {
00274 WString error(WString::tr("error-missing-data"));
00275 error.arg(fname, UTF8);
00276 new WText(error, root());
00277 return 0;
00278 }
00279 }
00280
00281 WApplication *createApplication(const WEnvironment& env)
00282 {
00283 WApplication *app = new ChartsExample(env);
00284
00285 return app;
00286 }
00287
00288 int main(int argc, char **argv)
00289 {
00290 return WRun(argc, argv, &createApplication);
00291 }