2

QChart 包含一些系列。

放大图表时是否可以检索图表上当前可见的点列表?

表示获取实际显示点的点列表/向量。


从 Kuba 回答中消化和简化:

QList<QVector<QPointF>> XXX::getDisplayedPoints(QChart *chart)
{
   QList<QVector<QPointF>> result;

   foreach (QAbstractSeries * series, chart->series())
   {
       QVector<QPointF> vector;

       auto inScene = chart->plotArea();
       auto inChart = chart->mapFromScene(inScene);
       auto inChartRect = inChart.boundingRect();
       auto inItem1 = chart->mapToValue(inChartRect.topLeft(), series);
       auto inItem2 = chart->mapToValue(inChartRect.bottomRight(), series);
       QRectF rect = QRectF(inItem1, inItem2).normalized();

       const QVector<QPointF> points = static_cast<QLineSeries*>(series)->pointsVector();
       std::copy_if(points.begin(), points.end(), std::back_inserter(vector),
                    [rect](QPointF const &p) { return rect.contains(p); });

       result.append(vector);
   }

   return result;
}

可以从信号调用

connect(static_cast<QValueAxis *>(m_chart->axisX()), &QValueAxis::rangeChanged, this, &XXX::on_zoomUpdated);
4

1 回答 1

2

您需要将图表对角的坐标映射到plotArea您显示的系列的世界坐标系,然后迭代系列以提取适合该矩形的点。

// https://github.com/KubaO/stackoverflown/tree/master/questionschart-visible-points-52777058
#include <QtCharts>
#include <algorithm>
#include <cmath>

auto seriesRect(QChart *chart, QAbstractSeries *series = nullptr) {
   auto inScene = chart->plotArea();
   auto inChart = chart->mapFromScene(inScene);
   auto inChartRect = inChart.boundingRect();
   auto inItem1 = chart->mapToValue(inChartRect.topLeft(), series);
   auto inItem2 = chart->mapToValue(inChartRect.bottomRight(), series);
   return QRectF(inItem1, inItem2).normalized();
}

auto pointsInRect(QXYSeries *series, const QRectF &rect) {
   QVector<QPointF> result;
   auto const points = series->pointsVector();
   std::copy_if(points.begin(), points.end(), std::back_inserter(result),
                [rect](auto &p) { return rect.contains(p); });
   return result;
}

示例的其余部分:

示例截图

auto data() {
   QVector<QPointF> result;
   for (auto x = 0.; x < 10.; x += 0.1) result.append({x, exp(x)});
   return result;
}

int main(int argc, char *argv[]) {
   QApplication a(argc, argv);
   QWidget ui;
   QVBoxLayout layout(&ui);
   QChartView view1, view2;
   QLabel status;
   layout.addWidget(&view1);
   layout.addWidget(&view2);
   layout.addWidget(&status);
   layout.setMargin(4);

   QLineSeries series;
   series.replace(data());
   auto *chart = view1.chart();
   chart->addSeries(&series);
   view1.setRubberBand(QChartView::RectangleRubberBand);

   QLineSeries subSeries;
   subSeries.setPointsVisible(true);
   auto *subChart = view2.chart();
   subChart->addSeries(&subSeries);

   for (auto *chart : {view1.chart(), view2.chart()}) {
      chart->legend()->hide();
      chart->createDefaultAxes();
      chart->layout()->setContentsMargins(0, 0, 0, 0);
   }

   auto update = [&] {
      auto rect = seriesRect(chart, &series);
      auto const points = pointsInRect(&series, rect);

      status.setText(QStringLiteral("Visible Range: (%1,%2)-(%3,%4)")
                         .arg(rect.left())
                         .arg(rect.top())
                         .arg(rect.right())
                         .arg(rect.bottom()));

      subSeries.replace(points);
      subChart->axisX(&subSeries)->setRange(rect.left(), rect.right());
      subChart->axisY(&subSeries)->setRange(rect.top(), rect.bottom());
   };
   QObject::connect(chart, &QChart::plotAreaChanged, update);
   ui.setMinimumSize(400, 400);
   ui.show();
   return a.exec();
}

对于映射本身,请参阅此答案

于 2018-10-12T13:17:24.337 回答