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 chart->addSeries(s);
00117 }
00118
00119 chart->resize(800, 400);
00120
00121 chart->setMargin(10, Top | Bottom);
00122 chart->setMargin(WLength::Auto, Left | Right);
00123
00124 new ChartConfig(chart, this);
00125 }
00126
00127 TimeSeriesExample::TimeSeriesExample(Wt::WContainerWidget *parent):
00128 WContainerWidget(parent)
00129 {
00130 new WText(WString::tr("scatter plot"), this);
00131
00132 WAbstractItemModel *model = readCsvFile("timeseries.csv", this);
00133
00134 if (!model)
00135 return;
00136
00137
00138
00139
00140 for (int i = 0; i < model->rowCount(); ++i) {
00141 WString s = asString(model->data(i, 0));
00142 WDate d = WDate::fromString(s, "dd/MM/yy");
00143 model->setData(i, 0, boost::any(d));
00144 }
00145
00146
00147
00148
00149 WCartesianChart *chart = new WCartesianChart(this);
00150 chart->setModel(model);
00151 chart->setXSeriesColumn(0);
00152 chart->setLegendEnabled(true);
00153
00154 chart->setType(ScatterPlot);
00155 chart->axis(XAxis).setScale(DateScale);
00156
00157
00158 chart->setPlotAreaPadding(100, Left);
00159 chart->setPlotAreaPadding(50, Top | Bottom);
00160
00161
00162
00163
00164 for (int i = 1; i < 3; ++i) {
00165 WDataSeries s(i, LineSeries);
00166 chart->addSeries(s);
00167 }
00168
00169 chart->resize(800, 400);
00170
00171 chart->setMargin(10, Top | Bottom);
00172 chart->setMargin(WLength::Auto, Left | Right);
00173
00174 new ChartConfig(chart, this);
00175 }
00176
00177 ScatterPlotExample::ScatterPlotExample(WContainerWidget *parent):
00178 WContainerWidget(parent)
00179 {
00180 new WText(WString::tr("scatter plot 2"), this);
00181
00182 WStandardItemModel *model = new WStandardItemModel(40, 2, this);
00183 model->setHeaderData(0, boost::any(WString("X")));
00184 model->setHeaderData(1, boost::any(WString("Y = sin(X)")));
00185
00186 for (unsigned i = 0; i < 40; ++i) {
00187 double x = (static_cast<double>(i) - 20) / 4;
00188
00189 model->setData(i, 0, boost::any(x));
00190 model->setData(i, 1, boost::any(sin(x)));
00191 }
00192
00193
00194
00195
00196 WCartesianChart *chart = new WCartesianChart(this);
00197 chart->setModel(model);
00198 chart->setXSeriesColumn(0);
00199 chart->setLegendEnabled(true);
00200
00201 chart->setType(ScatterPlot);
00202
00203
00204
00205 chart->axis(XAxis).setLocation(ZeroValue);
00206 chart->axis(YAxis).setLocation(ZeroValue);
00207
00208
00209 chart->setPlotAreaPadding(100, Left);
00210 chart->setPlotAreaPadding(50, Top | Bottom);
00211
00212
00213 chart->addSeries(WDataSeries(1, CurveSeries));
00214
00215 chart->resize(800, 300);
00216
00217 chart->setMargin(10, Top | Bottom);
00218 chart->setMargin(WLength::Auto, Left | Right);
00219
00220 ChartConfig *config = new ChartConfig(chart, this);
00221 config->setValueFill(ZeroValueFill);
00222 }
00223
00224 PieExample::PieExample(WContainerWidget *parent):
00225 WContainerWidget(parent)
00226 {
00227 new WText(WString::tr("pie chart"), this);
00228
00229 WAbstractItemModel *model = readCsvFile("pie.csv", this);
00230
00231 if (!model)
00232 return;
00233
00234
00235
00236
00237
00238 if (wApp->environment().javaScript()) {
00239 WContainerWidget *w = new WContainerWidget(this);
00240 Ext::TableView *table = new Ext::TableView(w);
00241 table->setMargin(10, Top | Bottom);
00242 table->setMargin(WLength::Auto, Left | Right);
00243 table->resize(300, 175);
00244 table->setModel(model);
00245 table->setAutoExpandColumn(0);
00246
00247 table->setEditor(0, new Ext::LineEdit());
00248
00249 for (int i = 1; i < model->columnCount(); ++i)
00250 table->setEditor(i, new Ext::NumberField());
00251 }
00252
00253
00254
00255
00256 WPieChart *chart = new WPieChart(this);
00257 chart->setModel(model);
00258 chart->setLabelsColumn(0);
00259 chart->setDataColumn(1);
00260
00261
00262 chart->setDisplayLabels(Outside | TextLabel | TextPercentage);
00263
00264
00265 chart->setPerspectiveEnabled(true, 0.2);
00266
00267
00268 chart->setExplode(0, 0.3);
00269
00270 chart->resize(800, 300);
00271
00272 chart->setMargin(10, Top | Bottom);
00273 chart->setMargin(WLength::Auto, Left | Right);
00274 }
00275