问题:根据K值生成对应K个不同的QScatterSeries,每个QSCatterSeries的点的颜色都不同。
for (i = 0; i < k; i++) {
QScatterSeries* series = new QScatterSeries;
// 使用动态生成的颜色
QColor color = QColor::fromHsvF(i / static_cast<double>(k), 1.0, 1.0);
series->setBrush(color);
addSeries(series);
connect(series, &QScatterSeries::hovered, this, &UCMdsQChart::tooltip);
}
之前K>7的时候最多就5个颜色限制,因为没有固定设置,导致颜色随机,也就会有这种相同情况,所以需要特别限制指定颜色,颜色设置的逻辑有很多,建议动态设置。
// Iterate through points and assign them to respective series based on model_labels
for (i = 0; i < points.size(); i++) {
QList<QAbstractSeries*> seriesList = series();
for (j = 0; j < seriesList.size(); j++) {
auto scater_series = qobject_cast<QScatterSeries*>(seriesList.at(j));
if (scater_series && model_labels[i] == j) {
scater_series->append(points[i]);
break;
}
}
}
设置完颜色后就可以加点了,加的逻辑是根据label而定。