In this part of my Qt Video Tutorial I’ll cover how to make numerous charts. We’ll make two types of Bar Charts along with Pie and Line. I’ll also cover numerous attributes associated with Qt charts.
All of the code follows the video below. I did my best to structure it as a transcript as well. Feel free to use it as you may.
If you like tutorials like this, consider donating $1, or simply turn off Ad Blockers. Either helps me continue to make free tutorials for all.
Code from the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
#include "mainwindow.h" // Manages the applications main settings like // widget initialization #include <QApplication> // The main window to which you add toolbars, // menubars, widgets and status bar #include <QtWidgets/QMainWindow> // Widget used to display charts #include <QtCharts/QChartView> // Used to draw bars representing data provided // grouped into categories #include <QtCharts/QBarSeries> // Represents 1 set of bars in a bar chart #include <QtCharts/QBarSet> // Displays the color used to represent each // QBarSet #include <QtCharts/QLegend> // Adds categories to the charts axes #include <QtCharts/QBarCategoryAxis> // Used to create stacked bar charts #include <QtCharts/QHorizontalStackedBarSeries> // Used to create a line chart #include <QtCharts/QLineSeries> // Used to change names on axis #include <QtCharts/QCategoryAxis> // Used to make Pie Charts #include <QtCharts/QPieSeries> #include <QtCharts/QPieSlice> // Define the scope for your variables and functions QT_CHARTS_USE_NAMESPACE int main(int argc, char *argv[]) { QApplication a(argc, argv); /* // Assign names to the set of bars used QBarSet *set0 = new QBarSet("Altuve"); QBarSet *set1 = new QBarSet("Martinez"); QBarSet *set2 = new QBarSet("Segura"); QBarSet *set3 = new QBarSet("Simmons"); QBarSet *set4 = new QBarSet("Trout"); // Assign values for each bar *set0 << 283 << 341 << 313 << 338 << 346 << 335; *set1 << 250 << 315 << 282 << 307 << 303 << 330; *set2 << 294 << 246 << 257 << 319 << 300 << 325; *set3 << 248 << 244 << 265 << 281 << 278 << 313; *set4 << 323 << 287 << 299 << 315 << 306 << 313; // Add all sets of data to the chart as a whole // 1. Bar Chart QBarSeries *series = new QBarSeries(); // 2. Stacked bar chart // QHorizontalStackedBarSeries *series = new QHorizontalStackedBarSeries(); series->append(set0); series->append(set1); series->append(set2); series->append(set3); series->append(set4); // Used to define the bar chart to display, title // legend, QChart *chart = new QChart(); // Add the chart chart->addSeries(series); // Set title chart->setTitle("Batting Avg by Year"); // Define starting animation // NoAnimation, GridAxisAnimations, SeriesAnimations chart->setAnimationOptions(QChart::AllAnimations); // Holds the category titles QStringList categories; categories << "2013" << "2014" << "2015" << "2016" << "2017" << "2018"; // Adds categories to the axes QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); // 1. Bar chart chart->setAxisX(axis, series); // 2. Stacked Bar chart // chart->setAxisY(axis, series); // Define where the legend is displayed chart->legend()->setVisible(true); chart->legend()->setAlignment(Qt::AlignBottom); // Used to display the chart QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); // Used to change the palette QPalette pal = qApp->palette(); // Change the color around the chart widget and text pal.setColor(QPalette::Window, QRgb(0xffffff)); pal.setColor(QPalette::WindowText, QRgb(0x404044)); // Apply palette changes to the application qApp->setPalette(pal); */ // 3. Line chart example // Other options here https://doc.qt.io/qt-5.11/qtcharts-customchart-example.html /* QLineSeries *series = new QLineSeries(); series->append(0, 16); series->append(1, 25); series->append(2, 24); series->append(3, 19); series->append(4, 33); series->append(5, 25); series->append(6, 34); // Create chart, add data, hide legend, and add axis QChart *chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); chart->createDefaultAxes(); // Customize the title font QFont font; font.setPixelSize(18); chart->setTitleFont(font); chart->setTitleBrush(QBrush(Qt::black)); chart->setTitle("Barry Bonds HRs as Pirate"); // Change the line color and weight QPen pen(QRgb(0x000000)); pen.setWidth(5); series->setPen(pen); chart->setAnimationOptions(QChart::AllAnimations); // Change the x axis categories QCategoryAxis *axisX = new QCategoryAxis(); axisX->append("1986",0); axisX->append("1987",1); axisX->append("1988",2); axisX->append("1989",3); axisX->append("1990",4); axisX->append("1991",5); axisX->append("1992",6); chart->setAxisX(axisX, series); // Used to display the chart QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); */ // 4. Pie Chart Example // Define slices and percentage of whole they take up QPieSeries *series = new QPieSeries(); series->append("Vegetables",.40); series->append("Beans",.20); series->append("Fruits",.15); series->append("Seeds/Nuts",.10); series->append("Whole Grains",.15); // Add label to 1st slice QPieSlice *slice0 = series->slices().at(0); slice0->setLabelVisible(); // Add label, explode and define brush for 2nd slice QPieSlice *slice1 = series->slices().at(1); slice1->setExploded(); slice1->setLabelVisible(); slice1->setPen(QPen(Qt::darkGreen, 2)); slice1->setBrush(Qt::green); // Add labels to rest of slices QPieSlice *slice2 = series->slices().at(2); slice2->setLabelVisible(); QPieSlice *slice3 = series->slices().at(3); slice3->setLabelVisible(); QPieSlice *slice4 = series->slices().at(4); slice4->setLabelVisible(); // Create the chart widget QChart *chart = new QChart(); // Add data to chart with title and hide legend chart->addSeries(series); chart->setTitle("What Derek Ate this Week"); chart->legend()->hide(); // Used to display the chart QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); // Create the main app window QMainWindow window; // Set the main window widget window.setCentralWidget(chartView); window.resize(420, 300); window.show(); return a.exec(); } |
Leave a Reply