Wt examples  4.2.2
Public Member Functions | List of all members
CategoryExample Class Reference

A Widget that demonstrates a category chart. More...

#include <ChartsExample.h>

Inheritance diagram for CategoryExample:
Inheritance graph
[legend]

Public Member Functions

 CategoryExample ()
 Creates the category chart example. More...
 

Detailed Description

A Widget that demonstrates a category chart.

Definition at line 39 of file ChartsExample.h.

Constructor & Destructor Documentation

◆ CategoryExample()

CategoryExample::CategoryExample ( )

Creates the category chart example.

Definition at line 114 of file ChartsExample.C.

114  :
115  WContainerWidget()
116 {
117  this->addWidget(cpp14::make_unique<WText>(WString::tr("category chart")));
118 
119  std::shared_ptr<WAbstractItemModel> model
120  = readCsvFile(WApplication::appRoot() + "category.csv", this);
121 
122  if (!model)
123  return;
124 
125  // Show a view that allows editing of the model.
126  auto *w = this->addWidget(cpp14::make_unique<WContainerWidget>());
127  auto *table = w->addWidget(cpp14::make_unique<WTableView>());
128 
129  table->setMargin(10, Side::Top | Side::Bottom);
130  table->setMargin(WLength::Auto, Side::Left | Side::Right);
131 
132  table->setModel(model);
133  table->setSortingEnabled(true);
134  table->setColumnResizeEnabled(true);
135  // table->setSelectionMode(SelectionMode::Extended);
136  table->setAlternatingRowColors(true);
137  table->setColumnAlignment(0, AlignmentFlag::Center);
138  table->setHeaderAlignment(0, AlignmentFlag::Center);
139  table->setRowHeight(22);
140 
141  // Editing does not really work without Ajax, it would require an
142  // additional button somewhere to confirm the edited value.
143  if (WApplication::instance()->environment().ajax()) {
144  table->resize(600, 20 + 5*22);
145  table->setEditTriggers(EditTrigger::SingleClicked);
146  } else {
147  table->resize(600, WLength::Auto);
148  table->setEditTriggers(EditTrigger::None);
149  }
150 
151  // We use a single delegate for all items which rounds values to
152  // the closest integer value.
153  std::shared_ptr<WItemDelegate> delegate
154  = std::make_shared<WItemDelegate>();
155  delegate->setTextFormat("%.f");
156  table->setItemDelegate(delegate);
157 
158  table->setColumnWidth(0, 80);
159  for (int i = 1; i < model->columnCount(); ++i)
160  table->setColumnWidth(i, 120);
161 
162  /*
163  * Create the category chart.
164  */
165  WCartesianChart *chart = this->addWidget(cpp14::make_unique<WCartesianChart>());
166  chart->setModel(model); // set the model
167  chart->setXSeriesColumn(0); // set the column that holds the categories
168  chart->setLegendEnabled(true); // enable the legend
169  chart->setZoomEnabled(true);
170  chart->setPanEnabled(true);
171 
172  // Automatically layout chart (space for axes, legend, ...)
173  chart->setAutoLayoutEnabled(true);
174 
175  chart->setBackground(WColor(200,200,200));
176 
177  /*
178  * Add all (but first) column as bar series
179  */
180  for (int i = 1; i < model->columnCount(); ++i) {
181  std::unique_ptr<WDataSeries> s
182  = cpp14::make_unique<WDataSeries>(i, SeriesType::Bar);
183  s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
184  chart->addSeries(std::move(s));
185  }
186 
187  chart->resize(800, 400);
188 
189  chart->setMargin(10, Side::Top | Side::Bottom);
190  chart->setMargin(WLength::Auto, Side::Left | Side::Right);
191 
192  /*
193  * Provide a widget to manipulate chart properties
194  */
195  this->addWidget(cpp14::make_unique<ChartConfig>(chart));
196 }

The documentation for this class was generated from the following files:

Generated on Fri Mar 20 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.15