|
|
#include "mainwindow.h"
|
|
|
#include <qchartview.h>
|
|
|
#include <qpieseries.h>
|
|
|
#include <qpieslice.h>
|
|
|
#include <qlineseries.h>
|
|
|
#include <qscatterseries.h>
|
|
|
#include <qchartaxis.h>
|
|
|
#include <QDebug>
|
|
|
|
|
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
|
: QMainWindow(parent)
|
|
|
{
|
|
|
// Here's the set of company's colors used throughout the example
|
|
|
m_companyColor1 = "#b90020";
|
|
|
m_companyColor2 = "#6d0013";
|
|
|
m_companyColor3 = "#d5d5f5";
|
|
|
m_companyColor4 = "#fcfcfc";
|
|
|
|
|
|
resize(400, 300);
|
|
|
setWindowFlags(Qt::FramelessWindowHint);
|
|
|
|
|
|
// Create chart view
|
|
|
m_chartView = new QChartView(this);
|
|
|
setCentralWidget(m_chartView);
|
|
|
m_chartView->setChartTitle("Custom colors example");
|
|
|
m_chartView->setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
// Create line series
|
|
|
m_line = new QLineSeries();
|
|
|
m_line->add(0.0, 1.1);
|
|
|
m_line->add(1.0, 2.3);
|
|
|
m_line->add(2.0, 2.1);
|
|
|
m_line->add(3.0, 3.3);
|
|
|
m_chartView->addSeries(m_line);
|
|
|
|
|
|
// Create scatter series with the same data
|
|
|
m_scatter = new QScatterSeries();
|
|
|
m_scatter->add(m_line->data());
|
|
|
m_chartView->addSeries(m_scatter);
|
|
|
|
|
|
// Create pie series with different data
|
|
|
m_pie = new QPieSeries();
|
|
|
m_pie->add(1.1, "1");
|
|
|
m_pie->add(2.1, "2");
|
|
|
m_pie->add(3.0, "3");
|
|
|
m_pie->setPositionFactors(0.7, 0.7);
|
|
|
m_pie->setSizeFactor(0.5);
|
|
|
m_chartView->addSeries(m_pie);
|
|
|
|
|
|
connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize()));
|
|
|
m_timer.setInterval(1500);
|
|
|
m_timer.setSingleShot(false);
|
|
|
m_timer.start();
|
|
|
}
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
void MainWindow::customize()
|
|
|
{
|
|
|
// Customize chart background
|
|
|
// Use a gradient from color 3 to color 4 for chart background
|
|
|
QLinearGradient chartGradient(0, 0, 0, 300);
|
|
|
chartGradient.setColorAt(0.0, m_companyColor3);
|
|
|
chartGradient.setColorAt(0.5, m_companyColor4);
|
|
|
chartGradient.setColorAt(1.0, m_companyColor3);
|
|
|
m_chartView->setChartBackgroundBrush(chartGradient);
|
|
|
m_chartView->setBackgroundBrush(m_companyColor4);
|
|
|
m_chartView->setChartTitleBrush(m_companyColor1);
|
|
|
|
|
|
// Customize chart axis
|
|
|
QPen color1Pen(m_companyColor1, 4.0);
|
|
|
m_chartView->axisX()->setAxisPen(color1Pen);
|
|
|
m_chartView->axisY()->setAxisPen(color1Pen);
|
|
|
|
|
|
// Customize series
|
|
|
m_line->setPen(color1Pen);
|
|
|
m_scatter->setPen(color1Pen);
|
|
|
m_scatter->setBrush(m_companyColor3);
|
|
|
for (int i(0); i < m_pie->slices().count(); i++) {
|
|
|
Qt::BrushStyle style = static_cast<Qt::BrushStyle>(i + 1);
|
|
|
m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style));
|
|
|
m_pie->slices().at(i)->setSlicePen(color1Pen);
|
|
|
}
|
|
|
|
|
|
|
|
|
// Calculate new colors to be used on the next update for the series
|
|
|
m_companyColor1.setRed((m_companyColor1.red() + 25) % 255);
|
|
|
m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255);
|
|
|
m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255);
|
|
|
m_companyColor2.setRed((m_companyColor2.red() + 25) % 255);
|
|
|
m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255);
|
|
|
m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255);
|
|
|
}
|
|
|
|