Reputation: 10191
I'm trying to adjust the LineChart example from the Qt Charts library. Here's the code:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineSeries *series = new QLineSeries();
series->append(0, 6);
series->append(2, 4);
series->append(3, 8);
series->append(7, 4);
series->append(10, 5);
*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 300);
window.show();
return a.exec();
}
I want to change the chart in a way that I can draw a point at certain x,y pairs in a different color. Now I tried to change the color of the chart line and it works, but it's not the chart line I want to change but the color of the point at x,y. Thus I guess I'll have to add points to the chart first, but apparently the API gives me only the append()
method not something like drawPoint()
. What do I have to do to get this functionality? Is it available already and I'm just not seeing it?
Upvotes: 2
Views: 6019
Reputation: 41
If all you want to do is add points onto the graph at specific locations, then QScatterSeries would be the best way to do so. As far as I know, if you want each point to be a separate color, they must be contained in a separate series, as the whole series will share the same properties.
QScatterSeries* redSeries = new QScatterSeries;
redSeries->append(0, 6);
redSeries->append(2, 4);
redSeries->setColor(Qt::red);
QScatterSeries* blueSeries = new QScatterSeries;
blueSeries->append(3, 8);
blueSeries->append(7, 4);
blueSeries->append(10, 5);
blueSeries->setColor(Qt::blue);
...
chart->addSeries(redSeries);
chart->addSeries(blueSeries);
Upvotes: 1