Subclassing WAbstractTableModel for WCartesianChart
Added by Neel Basu almost 8 years ago
I have a Chart Model that subclasses from WAbstractTableModel
ChartModel::ChartModel(unsigned int history, Wt::WObject* parent):Wt::WAbstractTableModel(parent), _history(history){
}
int ChartModel::columnCount(const Wt::WModelIndex& parent) const{
if(parent.isValid())
return 0;
std::cout << "columnCount" << std::endl;
return 2;
}
int ChartModel::rowCount(const Wt::WModelIndex& parent) const{
if(parent.isValid())
return 0;
std::cout << "rowCount" << " " << parent.row() << " " << parent.column() << " " << _history << std::endl;
return _history;
}
boost::any ChartModel::data(const Wt::WModelIndex& index, int role) const{
std::cout << "data: " << index.row() << " " << index.column() << " " << role << std::endl;
switch(role){
case Wt::DisplayRole:
case Wt::ToolTipRole:{
pair_type point = _elements.at(index.row());
if(index.column() == 0) return point.first;
else return point.second;
}
default:
return boost::any();
}
}
When I run this application I see nothing on the chart, only rowCount() is called repeatedly with ModelIndex(0,0) but columnCount is not being called at all.
Replies (2)
RE: Subclassing WAbstractTableModel for WCartesianChart - Added by Neel Basu almost 8 years ago
This is how I am passing the model to WCartesianChart
chart = new Wt::Chart::WCartesianChart(Wt::Chart::ChartType::ScatterPlot, parent);
model = new ChartModel(history, chart);
series = new Wt::Chart::WDataSeries(1, Wt::Chart::SeriesType::LineSeries);
chart->addSeries(*series);
series->bindToAxis(Wt::Chart::Axis::YAxis);
series->setModelColumn(1);
chart->setRubberBandEffectEnabled(false);
chart->resize(600, 200);
chart->setStyleClass("col-sm-6 chart");
chart->setTitle(name);
chart->setTextPen(Wt::WColor(255, 255, 255));
chart->setModel(model);
chart->setXSeriesColumn(0);
chart->setType(Wt::Chart::ScatterPlot);
chart->setBackground(Wt::WColor(0, 0, 0));
chart->axis(Wt::Chart::Axis::XAxis).setScale(Wt::Chart::DateTimeScale);
chart->axis(Wt::Chart::Axis::XAxis).setPen(Wt::WColor(255, 255, 255));
chart->axis(Wt::Chart::Axis::YAxis).setPen(Wt::WColor(255, 255, 255));
for(int i = 0; i < history; i++){
model->append(1);
}
RE: Subclassing WAbstractTableModel for WCartesianChart - Added by Aaron Wright almost 8 years ago
For both rowCount() and columnCount() you return 0 when the index is valid. Isn't that logic backwards?