I have created a QPolarChart and I want to hide the radial tick labels but leave the tick circles. I simply want to get rid of the text which shows "0.0", "20.0" and so on. I tried to change the label format but this did not work.
Here is a minimal example of what I tried:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QPolarChart>
#include <QtCharts/QValueAxis>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPolarChart *chart = new QPolarChart();
QValueAxis *angularAxis = new QValueAxis();
angularAxis->setTickCount(13);
angularAxis->setLabelFormat("%d");
angularAxis->setRange(0, 361);
chart->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);
QValueAxis *radialAxis = new QValueAxis();
radialAxis->setTickCount(10);
radialAxis->setLabelFormat(""); // <-- what do I have to add here?
radialAxis->setRange(0, 90);
chart->addAxis(radialAxis, QPolarChart::PolarOrientationRadial);
chart->legend()->setVisible(false);
QLineSeries *series = new QLineSeries();
*series << QPointF(0, 0) << QPointF(90, 22.5) << QPointF(180, 45) << QPointF(270, 67.5) << QPointF(360, 90);
chart->addSeries(series);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chart->legend()->hide();
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(400, 400);
window.show();
return a.exec();
}
This is how the result looks like.
I want to get rid of the radial axis labels ("0.0", "10.0" ... "90.0")
The trick is to set as labelFormat to " " or an invalid format like "#":
radialAxis->setLabelFormat(" ");
# or radialAxis->setLabelFormat("#");
radialAxis->setLabelsVisible(false);
Related
here I get a chart result in window. I get this source code in Qt official page. And i get some number result in this program and how I get the result in a widget
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineSeries *series0 = new QLineSeries();
QLineSeries *series1 = new QLineSeries();
*series0 << QPointF(2.3,5)<<QPointF(-11,4)<<QPointF(-8,3.2)<<QPointF(-9,7.2);
QAreaSeries *series = new QAreaSeries(series0, series1);
series->setName("Area Enclosed");
QPen pen(0x059605);
pen.setWidth(2);
series->setPen(pen);
QLinearGradient gradient(QPointF(0, 10), QPointF(0, 10));
gradient.setColorAt(0.0, 0x26f626);
gradient.setColorAt(1.0, 0x26f626);
gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
series->setBrush(gradient);
QChart *chart = new QChart();
chart->addSeries(series);
chart->setTitle("hello world");
chart->createDefaultAxes();
chart->axes(Qt::Horizontal).first()->setRange(-15, 15);
chart->axes(Qt::Vertical).first()->setRange(-15, 15);
chart->resize(400,400);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(600, 600);
window.show();
return a.exec();
}
how i get a result in the widget view
I work on Windows 10, ide - Qt Creator, compiler - Visual Studio 2017.
I am testing "LineChart Example"
https://doc.qt.io/qt-5/qtcharts-linechart-example.html#
I change data in this one. I can see a good result.
If uncomment line
//series->setUseOpenGL(true); //don'n work!
a chart is not drawn.
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const int count = 1495;
const double _xmin(17249.999500);
const double _xmax(17250.000500);
const double _ymin(4.0);
const double _ymax(8.0);
double _xstep((_xmax-_xmin)/count);
double _ystep((_ymax-_ymin)/count);
QValueAxis* _axisX = new QValueAxis;
QValueAxis* _axisY = new QValueAxis;
_axisX->setRange(_xmin, _xmax);
_axisY->setRange(_ymin, _ymax);
QLineSeries *series = new QLineSeries();
for(int i=0; i<count; i++) {
series->append(_xmin + _xstep*i, _ymin + _ystep*i);
}
//series->setUseOpenGL(true); //don'n work!
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
chart->setAxisX(_axisX, series);
chart->setAxisY(_axisY, series);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(600, 400);
window.show();
return a.exec();
}
I did some new experiments with data. In case series->setUseOpenGL(true);
If I set
const double _xmin(17249.999500); //stairs
const double _xmax(17250.015500); //stairs
I can see the stairs instead of straight!
I got an error when using QValueAxis in QtCharts:
ASSERT: "width > 0.0" in file painting\qrasterizer.cpp, line 761
This happens when the QBarSet values are all 0.
I have this example with the minimal necessary code below:
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QChart *chart = new QChart;
QBarSeries *series = new QBarSeries(chart);
QBarSet *set = new QBarSet("BarSet", series);
series->append(set);
QValueAxis *valueAxisX = new QValueAxis(chart);
QChartView *chartView = new QChartView(chart);
for(int i = 0; i < 24; ++i) {
set->append(0); //error
//if I set like set->append(1) or anything just to make sure the values are not all 0, there will be no error.
}
chart->addSeries(series);
chart->setAxisX(valueAxisX, series);
QMainWindow window;
window.setCentralWidget(chartView);
window.resize(420, 300);
window.show();
return a.exec();
}
My program will initiate the QBarSet dynamically from a QMap. Like this:
for(auto it = map.cbegin(); it != map.cend(); ++it) {
set->append(it.value());
}
And the QMap is initiated to something like this when the program starts:
QMap(("First", 0)("Second", 0)...)
Sometimes the QMap will not add values due to the use of the program, then all the QBarSet values will be initiated to 0. Then the program will crash because of that error.
So how can I avoid this error when the QBarSet values are all initiated to 0?
As the error speaks:
ASSERT: "width > 0.0"
You need a value greater than zero.
Also QBarSet::append,
void QBarSet::append(const qreal value)
expects a qreal or double value.
Perhaps you want to set it to 0.1 initially:
set->append(0.1);
Is there a way to assign a color for each label in a QCategoryAxis?
I know I can have a legend, but I prefer setting the colors on the axis to match the colors of the lines I have. I want to change the color of the markers (text of the categories) themselves, not the ticks. Notice that I want to set a different color for each axis label.
Tried using axisY.setLabelsBrush(QBrush(Qt::red));
But this sets the same color for all the labels.
Using Qt 5.10
the labels of QCategoryAxis are QGraphicsTextItem so they support HTML, so you could pass the color through that method:
#include <QApplication>
#include <QMainWindow>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineSeries *series = new QLineSeries();
*series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26);
QChart *chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
QCategoryAxis *axisX = new QCategoryAxis();
QCategoryAxis *axisY = new QCategoryAxis();
// Customize axis label font
QFont labelsFont;
labelsFont.setPixelSize(12);
axisX->setLabelsFont(labelsFont);
axisY->setLabelsFont(labelsFont);
// Customize axis colors
QPen axisPen(QRgb(0xd18952));
axisPen.setWidth(2);
axisX->setLinePen(axisPen);
axisY->setLinePen(axisPen);
axisX->append("<span style=\"color: #339966;\">low</span>", 10);
axisX->append("<span style=\"color: #330066;\">optimal</span>", 20);
axisX->append("<span style=\"color: #55ff66;\">high</span>", 30);
axisX->setRange(0, 30);
axisY->append("<font color=\"red\">slow</font>", 10);
axisY->append("<font color=\"green\">med</font>", 20);
axisY->append("<span style=\"color: #ffff00;\">fast</span>", 30);
axisY->setRange(0, 30);
chart->setAxisX(axisX, series);
chart->setAxisY(axisY, series);
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 generate a pdf report containing text and a QChart. I currently use the QTextDocument and add the chart into it as an image, then I use the QPdfWriter to export to pdf.
#include <QtGui>
#include <QtCore>
#include <QApplication>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
using namespace QtCharts;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a chart
QLineSeries *series = new QLineSeries();
series->append(0,0);
series->append(1,1);
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axisX()->setTitleText(QString("x [m]"));
chart->axisY()->setTitleText(QString("y [m]"));
chart->setTitle("Simple chart example");
chart->resize(500,500);
// Paint the chart into an image
QImage img(500,500,QImage::Format_RGB32);
QPainter painter(&img);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->show();
chartView->render(&painter);
// Set up pdf writer
QString filename = "myfile.pdf";
QPdfWriter writer(filename);
writer.setPageSize(QPagedPaintDevice::A4);
// Create the document
QTextDocument doc;
QTextCursor cursor(&doc);
cursor.insertText(QString("some text\n"));
cursor.insertImage(img);
// Print the document
doc.print(&writer);
return a.exec();
}
Although it works, I would like to avoid converting the chart into an image because it messes up the image quality. Is there a way to do it?
Here is a workaround, if the image quality is really what matters : you can use QPainter directly on the TextDocument. It requires more work and adjustement, but the quality is not lost.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a chart
QLineSeries *series = new QLineSeries();
series->append(0,0);
series->append(1,1);
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axisX()->setTitleText(QString("x [m]"));
chart->axisY()->setTitleText(QString("y [m]"));
chart->setTitle("Simple chart example");
chart->resize(500,500);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::TextAntialiasing);
chartView->show();
// Set up pdf writer
QString filename = "myfile.pdf";
QPdfWriter writer(filename);
writer.setPageSize(QPagedPaintDevice::A4);
// Create the document
QTextDocument doc;
QPainter painter(&writer);
QFont font = painter.font();
font.setPixelSize(200);
painter.setFont(font);
painter.drawText(QPoint(700,100),"Some text");
chartView->render(&painter);
doc.drawContents(&painter);
return a.exec();
}