Charts with different x-axis values
Added by John Foster over 10 years ago
The documentation suggests that different x-axis points can be used in a single model, but I cannot see how to do this.
I wish to plot a number of lines in a single chart, which have x-values at different points from each other. How do I structure the modelk to allow this? Can someone give a simple code example?
Thanks
Replies (7)
RE: Charts with different x-axis values - Added by John Foster over 10 years ago
Further to this question. If I set values in a Wt::WStandardItemModel like so, all is OK. I get axes and lines plotted:
int row = 0;
foreach_(const Y& d, map.keysY())
{
setData(row, 0, asWt(d)); // set x-axis column values
int col = 1;
foreach_(const std::string& key, map.keysX())
{
setData(row, col, map(key, d)); // set y values
++col;
}
++row;
}
If however I try the following, I get axes with labels that do not represent the data, and no lines plotted:
Wt::WStandardItem* root = invisibleRootItem();
std::vector<Wt::WStandardItem*> xs;
foreach_(const Y& d, map.keysY())
{
Wt::WStandardItem* xVal = new Wt::WStandardItem();
xVal->setData(asWt(d)); // set x-axis column values
xs.push_back(xVal);
}
root->appendColumn(xs);
foreach_ (const std::string& key, map.keysX())
{
std::vector<Wt::WStandardItem*> ys;
foreach_(const Y& d, map.keysY())
{
Wt::WStandardItem* yVal = new Wt::WStandardItem();
yVal->setData(map(key, d)); // set y values
ys.push_back(yVal);
}
root->appendColumn(ys);
}
Otherwise code is the same. It looks like I am structuring the data incorrectly, but don't know how to do it correctly!
Thanks for any advice.
John
RE: Charts with different x-axis values - Added by Koen Deforche over 10 years ago
Hey John,
To have different X-axis for each series (for scatter plots only!), you need to use WDataSeries::setXSeriesColumn() to provide a different X column for each series.
As to your second question, you need to use:
yVal->setData(v, Wt::DisplayRole)
Regards,
koen
RE: Charts with different x-axis values - Added by John Foster over 10 years ago
Hi Koen
Thanks - that worked a treat!
John
RE: Charts with different x-axis values - Added by John Foster over 10 years ago
Further to the above - how do I access the column vectors - I don't see anywhere in the API to do this.
I have tried the following, but the cast always fails.
for (int col = 0; col < columnCount(); ++col)
{
for (int row = 0; row < rowCount(); ++row)
{
Wt::WStandardItem* x = invisibleRootItem()->child(row, col);
if (x)
{
const boost::any any = x->data(Wt::DisplayRole);
typedef Wt::WDate WT;
if (boost::any_cast<const WT*>(&any)) // cast always fails
{
WT w = boost::any_cast(any);
// etc
}
}
}
}
I assume there is a simple way of getting at the data! Any advice would be appreciated.
John
RE: Charts with different x-axis values - Added by John Foster over 10 years ago
Mea culpa.
Should be:
boost::any_cast<const WT>(&any))
RE: Charts with different x-axis values - Added by Koen Deforche over 10 years ago
Hey,
Note that any_cast is pedantic: the type has to be an exact match. A const versus non-const type is different, for example.
If you put in a WDate, then you should be able to get back a WDate (but not a const WDate).
Regards,
koen
RE: Charts with different x-axis values - Added by John Foster over 10 years ago
Yes, but boost::any_cast has:
T boost::any_cast(boost::any);
and
T* boost::any_cast(boost::any*);
I mistakenly assumed (which would be more logical) that the latter prototype was:
T* boost::any_cast<T*>(boost::any*);
Just one of those little vagaries!
Cheers
John