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/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 namespace {
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 return model;
00046 } else {
00047 WString error(WString::tr("error-missing-data"));
00048 error.arg(fname, UTF8);
00049 new WText(error, parent);
00050 return 0;
00051 }
00052 }
00053 }
00054
00055 ChartsExample::ChartsExample(WContainerWidget *root)
00056 : WContainerWidget(root)
00057 {
00058 new WText(WString::tr("introduction"), this);
00059
00060 new CategoryExample(this);
00061 new TimeSeriesExample(this);
00062 new ScatterPlotExample(this);
00063 new PieExample(this);
00064 }
00065
00066 CategoryExample::CategoryExample(Wt::WContainerWidget *parent):
00067 WContainerWidget(parent)
00068 {
00069 new WText(WString::tr("category chart"), this);
00070
00071 WAbstractItemModel *model = readCsvFile("category.csv", this);
00072
00073 if (!model)
00074 return;
00075
00076
00077
00078
00079
00080 if (wApp->environment().javaScript()) {
00081 WContainerWidget *w = new WContainerWidget(this);
00082 Ext::TableView *table = new Ext::TableView(w);
00083 table->setMargin(10, Top | Bottom);
00084 table->setMargin(WLength::Auto, Left | Right);
00085 table->resize(500, 175);
00086 table->setModel(model);
00087 table->setAutoExpandColumn(0);
00088
00089 table->setEditor(0, new Ext::LineEdit());
00090
00091 for (int i = 1; i < model->columnCount(); ++i) {
00092 Ext::NumberField *nf = new Ext::NumberField();
00093 table->setEditor(i, nf);
00094 }
00095 }
00096
00097
00098
00099
00100 WCartesianChart *chart = new WCartesianChart(this);
00101 chart->setModel(model);
00102 chart->setXSeriesColumn(0);
00103 chart->setLegendEnabled(true);
00104
00105
00106 chart->setPlotAreaPadding(100, Left);
00107 chart->setPlotAreaPadding(50, Top | Bottom);
00108
00109
00110
00111
00112
00113
00114 for (int i = 1; i < model->columnCount(); ++i) {
00115 WDataSeries s(i, BarSeries);
00116 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00117 chart->addSeries(s);
00118 }
00119
00120 chart->resize(800, 400);
00121
00122 chart->setMargin(10, Top | Bottom);
00123 chart->setMargin(WLength::Auto, Left | Right);
00124
00125 new ChartConfig(chart, this);
00126 }
00127
00128 TimeSeriesExample::TimeSeriesExample(Wt::WContainerWidget *parent):
00129 WContainerWidget(parent)
00130 {
00131 new WText(WString::tr("scatter plot"), this);
00132
00133 WAbstractItemModel *model = readCsvFile("timeseries.csv", this);
00134
00135 if (!model)
00136 return;
00137
00138
00139
00140
00141 for (int i = 0; i < model->rowCount(); ++i) {
00142 WString s = asString(model->data(i, 0));
00143 WDate d = WDate::fromString(s, "dd/MM/yy");
00144 model->setData(i, 0, boost::any(d));
00145 }
00146
00147
00148
00149
00150 WCartesianChart *chart = new WCartesianChart(this);
00151 chart->setModel(model);
00152 chart->setXSeriesColumn(0);
00153 chart->setLegendEnabled(true);
00154
00155 chart->setType(ScatterPlot);
00156 chart->axis(XAxis).setScale(DateScale);
00157
00158
00159 chart->setPlotAreaPadding(100, Left);
00160 chart->setPlotAreaPadding(50, Top | Bottom);
00161
00162
00163
00164
00165 for (int i = 1; i < 3; ++i) {
00166 WDataSeries s(i, LineSeries);
00167 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00168 chart->addSeries(s);
00169 }
00170
00171 chart->resize(800, 400);
00172
00173 chart->setMargin(10, Top | Bottom);
00174 chart->setMargin(WLength::Auto, Left | Right);
00175
00176 new ChartConfig(chart, this);
00177 }
00178
00179 ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
00180 WContainerWidget(parent)
00181 {
00182 new WText(WString::tr("scatter plot 2"), this);
00183
00184 WStandardItemModel *model = new WStandardItemModel(40, 2, this);
00185 model->setHeaderData(0, boost::any(WString("X")));
00186 model->setHeaderData(1, boost::any(WString("Y = sin(X)")));
00187
00188 for (unsigned i = 0; i < 40; ++i) {
00189 double x = (static_cast<double>(i) - 20) / 4;
00190
00191 model->setData(i, 0, boost::any(x));
00192 model->setData(i, 1, boost::any(sin(x)));
00193 }
00194
00195
00196
00197
00198 WCartesianChart *chart = new WCartesianChart(this);
00199 chart->setModel(model);
00200 chart->setXSeriesColumn(0);
00201 chart->setLegendEnabled(true);
00202
00203 chart->setType(ScatterPlot);
00204
00205
00206
00207 chart->axis(XAxis).setLocation(ZeroValue);
00208 chart->axis(YAxis).setLocation(ZeroValue);
00209
00210
00211 chart->setPlotAreaPadding(100, Left);
00212 chart->setPlotAreaPadding(50, Top | Bottom);
00213
00214
00215 WDataSeries s(1, CurveSeries);
00216 s.setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
00217 chart->addSeries(s);
00218
00219 chart->resize(800, 300);
00220
00221 chart->setMargin(10, Top | Bottom);
00222 chart->setMargin(WLength::Auto, Left | Right);
00223
00224 ChartConfig *config = new ChartConfig(chart, this);
00225 config->setValueFill(ZeroValueFill);
00226 }
00227
00228 PieExample::PieExample(WContainerWidget *parent):
00229 WContainerWidget(parent)
00230 {
00231 new WText(WString::tr("pie chart"), this);
00232
00233 WAbstractItemModel *model = readCsvFile("pie.csv", this);
00234
00235 if (!model)
00236 return;
00237
00238
00239
00240
00241
00242 if (wApp->environment().javaScript()) {
00243 WContainerWidget *w = new WContainerWidget(this);
00244 Ext::TableView *table = new Ext::TableView(w);
00245 table->setMargin(10, Top | Bottom);
00246 table->setMargin(WLength::Auto, Left | Right);
00247 table->resize(300, 175);
00248 table->setModel(model);
00249 table->setAutoExpandColumn(0);
00250
00251 table->setEditor(0, new Ext::LineEdit());
00252
00253 for (int i = 1; i < model->columnCount(); ++i)
00254 table->setEditor(i, new Ext::NumberField());
00255 }
00256
00257
00258
00259
00260 WPieChart *chart = new WPieChart(this);
00261 chart->setModel(model);
00262 chart->setLabelsColumn(0);
00263 chart->setDataColumn(1);
00264
00265
00266 chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
00267
00268
00269 chart->setPerspectiveEnabled(true, 0.2);
00270
00271
00272 chart->setExplode(0, 0.3);
00273
00274 chart->resize(800, 300);
00275
00276 chart->setMargin(10, Top | Bottom);
00277 chart->setMargin(WLength::Auto, Left | Right);
00278 }
00279