#include "customchartview.h"
#include
CustomChartView::CustomChartView(QGraphicsView *parent) :
QChartView(parent)
{
m_categories << "Jan" << "Feb" << "Mar" << "Apr";
initAxisX(m_categories);
initAxisY();
initChart();
initBar();
setBar(m_categories);
}
CustomChartView::~CustomChartView()
{
delete m_chart;
m_chart = NULL;
}
void CustomChartView::initAxisX(QStringList categories)
{
m_categories = categories;
m_axisX = new QBarCategoryAxis(this);
m_axisX->setLinePenColor(Qt::black);
m_axisX->setLabelsFont(QFont("consoles", 18));
m_axisX->setGridLineVisible(false);
m_axisX->append(m_categories);
}
void CustomChartView::initAxisY()
{
m_axisY = new QValueAxis(this);
m_axisY->setRange(0, 10);
m_axisY->setLinePenColor(Qt::black);
m_axisY->setLabelsFont(QFont("consoles", 18));
}
void CustomChartView::initChart()
{
m_chart = new QChart();
m_chart->legend()->setVisible(false);
m_chart->setAnimationOptions(QChart::SeriesAnimations);
m_chart->setAxisX(m_axisX);
setChart(m_chart);
}
void CustomChartView::initBar()
{
for(int i = 0; i < 6; i++)
{
m_barSeries[i] = new QBarSeries(this);
m_barSeries[i]->setLabelsVisible(true);
m_barSeries[i]->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);
m_barset[i] = new QBarSet("", m_barSeries[i]);
m_barset[i]->setLabelFont(QFont("consoles", 16));
*m_barset[i] << 0;
m_barSeries[i]->append(m_barset[i]);
if(0 == i)
{
m_barset[i]->setBrush(Qt::darkRed);
m_barset[i]->setLabelColor(Qt::darkRed);
QObject::connect(m_barset[0], &QBarSet::hovered, [=](bool b, int i){
if(b)
m_barset[0]->setBrush(Qt::red);
else
m_barset[0]->setBrush(Qt::darkRed);
});
}
else if(1 == i)
{
m_barset[i]->setBrush(Qt::darkGreen);
m_barset[i]->setLabelColor(Qt::darkGreen);
QObject::connect(m_barset[1], &QBarSet::hovered, [=](bool b, int i){
if(b)
m_barset[1]->setBrush(Qt::green);
else
m_barset[1]->setBrush(Qt::darkGreen);
});
}
else if(2 == i)
{
m_barset[i]->setBrush(Qt::darkBlue);
m_barset[i]->setLabelColor(Qt::darkBlue);
QObject::connect(m_barset[2], &QBarSet::hovered, [=](bool b, int i){
if(b)
m_barset[2]->setBrush(Qt::blue);
else
m_barset[2]->setBrush(Qt::darkBlue);
});
}
else if(3 == i)
{
m_barset[i]->setBrush(Qt::darkCyan);
m_barset[i]->setLabelColor(Qt::darkCyan);
QObject::connect(m_barset[3], &QBarSet::hovered, [=](bool b, int i){
if(b)
m_barset[3]->setBrush(Qt::cyan);
else
m_barset[3]->setBrush(Qt::darkCyan);
});
}
else if(4 == i)
{
m_barset[i]->setBrush(Qt::darkMagenta);
m_barset[i]->setLabelColor(Qt::darkMagenta);
QObject::connect(m_barset[i], &QBarSet::hovered, [=](bool b, int i){
if(b)
m_barset[4]->setBrush(Qt::magenta);
else
m_barset[4]->setBrush(Qt::darkMagenta);
});
}
else if(5 == i)
{
m_barset[i]->setBrush(Qt::darkGray);
m_barset[i]->setLabelColor(Qt::darkGray);
QObject::connect(m_barset[5], &QBarSet::hovered, [=](bool b, int i){
if(b)
m_barset[5]->setBrush(Qt::gray);
else
m_barset[5]->setBrush(Qt::darkGray);
});
}
}
}
void CustomChartView::setBar(QStringList categories)
{
m_categories = categories;
m_axisX->clear();
m_axisX->append(m_categories);
m_axisY->setRange(0, 10);
for(int i = 0; i < 6; i++)
{
if(m_barSeries[i] != NULL)
m_chart->removeSeries(m_barSeries[i]);
}
for(int i = 0; i < categories.size(); i++)
{
m_barset[i]->setLabel(categories.at(i));
m_barset[i]->replace(0, 0);
m_barSeries[i]->append(m_barset[i]);
m_chart->addSeries(m_barSeries[i]);
m_chart->setAxisY(m_axisY, m_barSeries[i]);
}
}
void CustomChartView::changeLabelHint(QString label)
{
int max = 0;
for(int i = 0; i < m_categories.size(); i++)
{
if(label == m_barset[i]->label())
{
m_barset[i]->append(m_barset[i]->at(0)+1);
m_barset[i]->remove(0);
max = m_barset[i]->at(0);
break;
}
}
for(int i = 0; i < m_categories.size(); i++)
{
if(m_barset[i]->at(0) > max)
max = m_barset[i]->at(0);
}
if(max + 2 >= m_axisY->max())
m_axisY->setRange(0, max+2);
}