數(shù)據(jù)選擇粒度
可以通過(guò)函數(shù)QCPAbstractPlottable::setSelectable
(所有的圖表類都繼承自QCPAbstractPlottable)設(shè)置數(shù)據(jù)選擇的粒度昵慌,如下圖所示:
數(shù)據(jù)選擇方式及數(shù)據(jù)讀取
一般來(lái)說(shuō)奔脐,數(shù)據(jù)選擇是通過(guò)鼠標(biāo)來(lái)進(jìn)行的柒凉,即鼠標(biāo)點(diǎn)擊或者鼠標(biāo)框選,鼠標(biāo)點(diǎn)擊選擇通過(guò)函數(shù) QCustomPlot::setInteractions 設(shè)置相應(yīng)的枚舉量即可技竟,如果需要多選,則需要 QCustomPlot::setMultiSelectModifier 設(shè)置多選時(shí)使用的按鍵以及 setInteractions 設(shè)置枚舉量包含 QCP::iMultiSelect 俗壹;而鼠標(biāo)框選則通過(guò) QCustomPlot::setSelectionRectMode 設(shè)置框選時(shí)的枚舉類型為 srmSelect 恰梢,QCustomPlot還給了一個(gè) srmCustom 類型讓我們自定義框選時(shí)的行為,只需要連接 QCPSelectionRect::accepted 信號(hào)即可
已被選擇的數(shù)據(jù)可以通過(guò)QCPAbstractPlottable::selection函數(shù)讀取酿秸,其返回QCPDataSelection類灭翔,QCPDataSelection表現(xiàn)為多個(gè)選擇范圍的集合QList<QCPDataRange>,而QCPDataRange是單個(gè)數(shù)據(jù)選擇的范圍辣苏,包含被選擇數(shù)據(jù)的開始位置以及結(jié)束位置(通俗的說(shuō)就是下標(biāo)index)肝箱,注意這里遵循左閉右開原則
多個(gè)選擇部分的對(duì)象
在QCustomPlot中軸QCPAxis和圖例QCPLegend等可以有多個(gè)部分可以被選擇,由枚舉SelectablePart
決定可以被選擇的部分稀蟋,如圖所示:
數(shù)據(jù)選擇的風(fēng)格
QCustomPlot引入了QCPSelectionDecorator
來(lái)決定數(shù)據(jù)被選擇時(shí)的風(fēng)格煌张,主要有畫筆、畫刷和散點(diǎn)圖三種風(fēng)格
全部源碼
void MainWindow::setupLineStyleDemo(QCustomPlot *customPlot)
{
customPlot->legend->setVisible(true);
customPlot->legend->setFont(QFont("Helvetica", 9));
QPen pen;
QStringList lineNames;
lineNames << "lsNone" << "lsLine" << "lsStepLeft" << "lsStepRight" << "lsStepCenter" << "lsImpulse";
for (int i = QCPGraph::lsNone; i <= QCPGraph::lsImpulse; ++i)
{
customPlot->addGraph();
pen.setColor(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));
customPlot->graph()->setPen(pen); // 設(shè)置圖表的畫筆
customPlot->graph()->setName(lineNames.at(i-QCPGraph::lsNone));
customPlot->graph()->setLineStyle((QCPGraph::LineStyle)i); // 設(shè)置圖表線段的風(fēng)格
customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5)); // 設(shè)置圖表散點(diǎn)圖的樣式
QVector<double> x(15), y(15);
for (int j=0; j<15; ++j)
{
x[j] = j/15.0 * 5*3.14 + 0.01;
y[j] = 7*qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5 + (QCPGraph::lsImpulse)*5 + 2;
}
customPlot->graph()->setData(x, y);
customPlot->graph()->rescaleAxes(true);
}
// 放大一點(diǎn)
customPlot->yAxis->scaleRange(1.1, customPlot->yAxis->range().center());
customPlot->xAxis->scaleRange(1.1, customPlot->xAxis->range().center());
customPlot->xAxis->setTicks(true);
customPlot->yAxis->setTicks(true);
customPlot->xAxis->setTickLabels(true);
customPlot->yAxis->setTickLabels(true);
customPlot->axisRect()->setupFullAxesBox();
}
void MainWindow::setupSelectionDemo(QCustomPlot *customPlot)
{
setupLineStyleDemo(customPlot);
customPlot->setInteractions(QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables | QCP::iMultiSelect); // 軸退客、圖例骏融、圖表可以被選擇,并且是多選的方式
customPlot->setSelectionRectMode(QCP::srmSelect); // 鼠標(biāo)框選
customPlot->setMultiSelectModifier(Qt::ControlModifier); // 使用ctrl鍵來(lái)多選
customPlot->xAxis->setSelectableParts(QCPAxis::spAxis | QCPAxis::spAxisLabel | QCPAxis::spTickLabels); // 軸的三個(gè)部分都可以被選擇
customPlot->yAxis->setSelectableParts(QCPAxis::spAxis | QCPAxis::spAxisLabel | QCPAxis::spTickLabels);
customPlot->xAxis->setLabel("xAxis");
customPlot->yAxis->setLabel("yAxis");
customPlot->legend->setSelectableParts(QCPLegend::spItems); // 圖例本身不能被選擇萌狂,只有里面的項(xiàng)可以被選擇
customPlot->legend->setSelectedIconBorderPen(Qt::NoPen); // 設(shè)置圖例里的項(xiàng)被選擇時(shí)不顯示Icon的邊框
for (int i=0; i < customPlot->graphCount(); ++i) {
QCPGraph *graph = customPlot->graph(i);
graph->setSelectable(QCP::stDataRange);
}
// 連接QCustomPlot的信號(hào)档玻,selectionChangedByUser表明是由鼠標(biāo)點(diǎn)擊進(jìn)行的選擇
// 這里主要就是同步圖表和圖例的顯示
connect(customPlot, &QCustomPlot::selectionChangedByUser, [customPlot](){
for (int i=0; i < customPlot->graphCount(); ++i) {
QCPGraph *graph = customPlot->graph(i);
QCPPlottableLegendItem *item = customPlot->legend->itemWithPlottable(graph);
if (item->selected() && !graph->selected())
graph->setSelection(QCPDataSelection(graph->data()->dataRange())); // 當(dāng)圖例項(xiàng)被選擇時(shí),選擇圖表全部的數(shù)據(jù)
else if (graph->selected())
item->setSelected(true);
}
});
}