QCustomPlot Discussion and Comments

QCPBars key to x-axis labelReturn to overview

Hello,

I have a code to dump data from QCPBars :

        for(int i=0; i<plottableCount(); i++) {
            if(QCPBars * bar = qobject_cast<QCPBars *>(plottable(i))) {
                QFile file(directory + "/" + graph(i)->name().replace("/", "_") + ".txt");
                if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    return;
                }
                QTextStream out(&file);
                out.setLocale(c);
                for(QCPBarsDataContainer::const_iterator iter=bar->data()->constBegin(); iter!=bar->data()->constEnd(); ++iter)
                    out << iter->mainKey() << iter->mainValue() << "\n";
            }
        }

But now, instead of dumping the key from the QCPBars, I would like to get the corresponding QString value of the xAxis on which I have installed a QCPAxisTickerText. Could you give me ideas to do that ?

Thanks !

QCPAxisTickerText provides you with the key/string pairs through ticker->ticks(), which returns a QMap<double, QString>.

Check out Qt's documentation for QMap, as it allows you to find the closest available key in the map, which you then just use with the iter->mainKey() of your code.

I missed this function. Thanks DerManu, great features, as usual !

Here is my code, if it can help :

        for(int i=0; i<plottableCount(); i++) {
            if(QCPBars * bar = qobject_cast<QCPBars *>(plottable(i))) {
                QFile file(directory + "/" + plottable(i)->name().replace("/", "_") + ".txt");
                if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    return;
                }
                QTextStream out(&file);
                out.setLocale(c);
                if(QSharedPointer<QCPAxisTickerText> textTicker = qSharedPointerDynamicCast<QCPAxisTickerText>(xAxis->ticker())) {
                    for(QCPBarsDataContainer::const_iterator iter=bar->data()->constBegin(); iter!=bar->data()->constEnd(); ++iter) {
                        if(textTicker->ticks().find(iter->mainKey())!=textTicker->ticks().end()) {
                            QString xLabel = textTicker->ticks().find(iter->mainKey()).value();
                            out << xLabel << "\t" << iter->mainValue() << "\n";
                        }
                        else {
                            out << iter->key << "\t" << iter->mainValue() << "\n";
                        }
                    }
                }
                else {
                    for(QCPBarsDataContainer::const_iterator iter=bar->data()->constBegin(); iter!=bar->data()->constEnd(); ++iter)
                        out << iter->mainKey() << "\t" << iter->mainValue() << "\n";
                }
            }
        }