Chart
A native chart surface for line, area, bar and pie series.
#include <QApplication>
#include <QFile>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QStandardItemModel>
#include <QUrl>
#include <QVBoxLayout>
#include <array>
#ifdef SHADCN_GALLERY_MEDIA
#include <shadcn/media.hpp>
#endif
#include <shadcn/shadcn.hpp>
QWidget* example(QWidget* parent = nullptr) {
using namespace shadcn;
auto* canvas = new QWidget(parent);
canvas->setAutoFillBackground(true);
auto* outer = new QHBoxLayout(canvas);
outer->setContentsMargins(32, 32, 32, 32);
auto* host = new QWidget(canvas);
auto* layout = new QVBoxLayout(host);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(16);
outer->addStretch();
outer->addWidget(host, 0, Qt::AlignCenter);
outer->addStretch();
auto* chart = new Chart(host);
chart->setFixedSize(480, 240);
chart->setLabels({"Jan", "Feb", "Mar", "Apr", "May", "Jun"});
(void)chart->setSeries({{"Desktop", {186, 305, 237, 273, 209, 214}, Role::Chart1},
{"Mobile", {80, 200, 120, 190, 130, 140}, Role::Chart3}});
layout->addWidget(chart);
return canvas;
}
Installation
- Source
- Installed package
add_subdirectory(shadcn-cpp)
target_link_libraries(app PRIVATE shadcn::widgets)
find_package(shadcn CONFIG REQUIRED)
target_link_libraries(app PRIVATE shadcn::widgets)
Usage
#include <shadcn/data.hpp>
shadcn::Chart chart(parent);
chart.setChartType(shadcn::ChartType::Line);
chart.setLabels({"Mon", "Tue", "Wed"});
chart.setSeries({shadcn::ChartSeries{
"Visitors", {12, 18, 15}, shadcn::Role::Chart1}});
API
setSeries returns std::expected<void, ValueError>. Labels and legend visibility are controlled separately.
Pointer movement over the plot area shows a themed tooltip with the label and every series value at that point. The tooltip stays inside the chart, follows the pointer and hides when the pointer leaves the plot area or the series change.
Variations
Choose line, area, bar or pie with setChartType. Add named series when labels share one domain.
Keyboard and accessibility
Qt supplies native focus, keyboard interaction and accessible object roles. Give the component a visible label or an accessible name when its text alone does not explain the action.
Ownership
The parent widget owns the component. Child widgets created through a component layout follow Qt parent ownership. References returned by helper methods are borrowed.