// PRO-File
QT += core gui \
widgets \
charts
TEMPLATE = app
SOURCES += main.cpp\
// cpp-file
#include <QApplication>
#include <QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include <QtCharts/QDateTimeAxis>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QAreaSeries>
#include <QtWidgets/QGraphicsView>
#include <QtCharts/QChartGlobal>
#include <QTime>
#include <QDebug>
#include <QHBoxLayout>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
// obtain time first
// create Chart
QChart *chart = new QChart();
chart->legend()->hide();
chart->setMinimumSize(640, 480);
chart->createDefaultAxes();
// costumize chart a little
QDateTimeAxis *dateTimeAxis = new QDateTimeAxis;
dateTimeAxis->setTickCount(7);
dateTimeAxis->setFormat("hh:mm");
dateTimeAxis->setTitleText("Time");
dateTimeAxis->setMin(currentTime.addSecs(-3600));
dateTimeAxis->setMax(currentTime.addSecs( 3600));
// config axes
categories << "Paris" << "London" << "Berlin" << "Madrid";
QBarCategoryAxis *categoryAxis = new QBarCategoryAxis();
categoryAxis->append(categories);
chart->addAxis(dateTimeAxis,Qt::AlignBottom);
chart->addAxis(categoryAxis, Qt::AlignLeft);
chart->axisY()->setRange(0, categories.length());
// use QChartView
QChartView *myChartView = new QChartView(chart);
// promote to QMainWindow
layout->addWidget(myChartView);
window->setLayout(layout);
mainwindow->setCentralWidget(window);
///////////////////
// add some life
QLineSeries *lowerSeries = new QLineSeries();
QLineSeries *upperSeries = new QLineSeries();
QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
area
->setBrush
(QColor(100,
100,
100,
100));
chart->addSeries(area);
area->attachAxis(dateTimeAxis);
area->attachAxis(chart->axisY());
QPointF p2
= QPointF(currentTime.
addSecs(2400).
toMSecsSinceEpoch(),
1);
QPointF p4
= QPointF(currentTime.
addSecs(2400).
toMSecsSinceEpoch(),
2);
lowerSeries->append(p1);
lowerSeries->append(p2);
upperSeries->append(p3);
upperSeries->append(p4);
// now the problem:
// first try
position = chart->mapToPosition(p1, lowerSeries);
x = x.setNum(position.x());
y = y.setNum(position.y());
tag1->setText(x + " " + y);
tag1->setPos(position);
// approach randomly
x = x.setNum(position.x());
y = y.setNum(position.y());
tag2->setText(x + " " + y);
tag2->setPos(position);
mainwindow->show();
return a.exec();
}
// PRO-File
QT += core gui \
widgets \
charts
TEMPLATE = app
SOURCES += main.cpp\
// cpp-file
#include <QApplication>
#include <QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include <QtCharts/QDateTimeAxis>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QAreaSeries>
#include <QtWidgets/QGraphicsView>
#include <QtCharts/QChartGlobal>
#include <QTime>
#include <QDebug>
#include <QHBoxLayout>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// obtain time first
QDateTime currentTime = QDateTime::currentDateTime();
// create Chart
QChart *chart = new QChart();
chart->legend()->hide();
chart->setMinimumSize(640, 480);
chart->createDefaultAxes();
// costumize chart a little
QDateTimeAxis *dateTimeAxis = new QDateTimeAxis;
dateTimeAxis->setTickCount(7);
dateTimeAxis->setFormat("hh:mm");
dateTimeAxis->setTitleText("Time");
dateTimeAxis->setMin(currentTime.addSecs(-3600));
dateTimeAxis->setMax(currentTime.addSecs( 3600));
// config axes
QStringList categories;
categories << "Paris" << "London" << "Berlin" << "Madrid";
QBarCategoryAxis *categoryAxis = new QBarCategoryAxis();
categoryAxis->append(categories);
chart->addAxis(dateTimeAxis,Qt::AlignBottom);
chart->addAxis(categoryAxis, Qt::AlignLeft);
chart->axisY()->setRange(0, categories.length());
// use QChartView
QChartView *myChartView = new QChartView(chart);
// promote to QMainWindow
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(myChartView);
QWidget *window = new QWidget;
window->setLayout(layout);
QMainWindow *mainwindow = new QMainWindow();
mainwindow->setCentralWidget(window);
///////////////////
// add some life
QLineSeries *lowerSeries = new QLineSeries();
QLineSeries *upperSeries = new QLineSeries();
QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
area->setBrush(QColor(100,100,100,100));
chart->addSeries(area);
area->attachAxis(dateTimeAxis);
area->attachAxis(chart->axisY());
QPointF p1 = QPointF(currentTime.toMSecsSinceEpoch(), 1);
QPointF p3 = QPointF(currentTime.toMSecsSinceEpoch(), 2);
QPointF p2 = QPointF(currentTime.addSecs(2400).toMSecsSinceEpoch(), 1);
QPointF p4 = QPointF(currentTime.addSecs(2400).toMSecsSinceEpoch(), 2);
lowerSeries->append(p1);
lowerSeries->append(p2);
upperSeries->append(p3);
upperSeries->append(p4);
// now the problem:
QGraphicsSimpleTextItem *tag1 = new QGraphicsSimpleTextItem(chart);
QGraphicsSimpleTextItem *tag2 = new QGraphicsSimpleTextItem(chart);
QString x, y;
QPointF position;
// first try
position = chart->mapToPosition(p1, lowerSeries);
x = x.setNum(position.x());
y = y.setNum(position.y());
tag1->setText(x + " " + y);
tag1->setPos(position);
// approach randomly
position = QPointF(300,200);
x = x.setNum(position.x());
y = y.setNum(position.y());
tag2->setText(x + " " + y);
tag2->setPos(position);
mainwindow->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks