#include "bargraph.h"
#include "mychartview.h"
bargraph::bargraph(QWidget *parent) :
QWidget(parent),
m_axisYmin(0),
m_axisYmax(30),
m_XtickCount(10),
m_axisXmin(0),
m_axisXmax(30),
m_YtickCount(10),
m_chart(nullptr),
m_chartView(nullptr),
m_axisX(nullptr),
m_axisY(nullptr),
m_Xname("X轴"),
m_Yname("Y轴")
{
init();
}
void bargraph::setData(QMap<QString, QPointF> data)
{
QMapIterator<QString, QPointF> i(data);
while (i.hasNext()) {
i.next();
bool move = m_chartView->Moveorzoom();
if(i.value().x() > m_axisX->max())
{
qreal leng = i.value().x() - m_axisX->max();
if(!move)
{
m_axisX->setMax(m_axisX->max() + leng);
m_axisX->setMin(m_axisX->min() +leng);
}
else
{
m_chartView->setAxisXmax(m_axisX->max() + leng);
m_chartView->setAxisXmin(m_axisX->min() +leng);
}
}
if(i.value().y() > m_axisYmax)
{
if(!move)
{
m_axisY->setMax(i.value().y());
}
else {
m_chartView->setAxisYmax(i.value().y());
}
m_axisYmax = i.value().y();
}
if(i.value().y() < m_axisYmin)
{
if(!move)
{
m_axisY->setMin(i.value().y());
}
else
{
m_chartView->setAxisYmin(i.value().y());
}
m_axisYmin = i.value().y();
}
if(!m_mapLineSeries.contains(i.key()))
{
QLineSeries *lineSeries = new QLineSeries();
lineSeries->setPointsVisible(false);
lineSeries->setName(i.key());
lineSeries->append(i.value());
m_chart->addSeries(lineSeries);
lineSeries->attachAxis(m_axisX);
lineSeries->attachAxis(m_axisY);
m_mapLineSeries.insert(i.key(),lineSeries);
}
else
{
m_mapLineSeries.value(i.key())->append(i.value());
}
}
}
void bargraph::setData(QMap<QString, QList<QPointF>> data)
{
deleteLineSeries();
QMapIterator<QString, QList<QPointF>> i(data);
while (i.hasNext()) {
i.next();
QLineSeries *lineSeries = new QLineSeries();
lineSeries->setPointsVisible(false);
lineSeries->setName(i.key());
lineSeries->append(i.value());
m_chart->addSeries(lineSeries);
lineSeries->attachAxis(m_axisX);
lineSeries->attachAxis(m_axisY);
m_mapLineSeries.insert(i.key(),lineSeries);
}
}
void bargraph::init()
{
m_axisX = new QValueAxis();
m_axisX->setTitleText(m_Xname);
m_axisX->setMin(m_axisYmin);
m_axisX->setMax(m_axisYmax);
m_axisX->setTickCount(m_XtickCount);
m_axisY = new QValueAxis();
m_axisY->setTitleText(m_Yname);
m_axisY->setMin(m_axisXmin);
m_axisY->setMax(m_axisXmax);
m_axisYmin = m_axisY->min();
m_axisYmax = m_axisY->max();
m_axisY->setTickCount(m_YtickCount);
m_chart = new QChart();
m_chart->setAnimationOptions(QChart::SeriesAnimations);
m_chart->addAxis(m_axisY,Qt::AlignLeft);
m_chart->addAxis(m_axisX,Qt::AlignBottom);
m_chartView = new MyChartView(m_chart);
m_chartView->setRenderHint(QPainter::Antialiasing);
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setContentsMargins(0,0,0,0);
gridLayout->addWidget(m_chartView);
this->setLayout(gridLayout);
this->setContentsMargins(0,0,0,0);
}
QString bargraph::Xname() const
{
return m_Xname;
}
void bargraph::setXname(const QString &newXname)
{
if (m_Xname == newXname)
return;
m_Xname = newXname;
m_axisX->setTitleText(m_Xname);
emit XnameChanged();
}
QString bargraph::Yname() const
{
return m_Yname;
}
void bargraph::setYname(const QString &newYname)
{
if (m_Yname == newYname)
return;
m_Yname = newYname;
m_axisY->setTitleText(m_Yname);
emit YnameChanged();
}
qreal bargraph::XtickCount() const
{
return m_XtickCount;
}
void bargraph::setXtickCount(qreal newXtickCount)
{
if (qFuzzyCompare(m_XtickCount, newXtickCount))
return;
m_XtickCount = newXtickCount;
m_axisX->setTickCount(m_XtickCount);
emit XtickCountChanged();
}
qreal bargraph::YtickCount() const
{
return m_YtickCount;
}
void bargraph::setYtickCount(qreal newYtickCount)
{
if (qFuzzyCompare(m_YtickCount, newYtickCount))
return;
m_YtickCount = newYtickCount;
m_axisY->setTickCount(m_YtickCount);
emit YtickCountChanged();
}
QValueAxis *bargraph::axisY() const
{
return m_axisY;
}
QValueAxis *bargraph::axisX() const
{
return m_axisX;
}
QChart *bargraph::chart() const
{
return m_chart;
}
qreal bargraph::axisXmax() const
{
return m_axisXmax;
}
void bargraph::setAxisXmax(qreal newAxisXmax)
{
if (qFuzzyCompare(m_axisXmax, newAxisXmax))
return;
m_axisXmax = newAxisXmax;
m_axisX->setMax(m_axisXmax);
emit axisXmaxChanged();
}
qreal bargraph::axisXmin() const
{
return m_axisXmin;
}
void bargraph::setAxisXmin(qreal newAxisXmin)
{
if (qFuzzyCompare(m_axisXmin, newAxisXmin))
return;
m_axisXmin = newAxisXmin;
m_axisX->setMin(m_axisXmin);
emit axisXminChanged();
}
qreal bargraph::axisYmax() const
{
return m_axisYmax;
}
void bargraph::setAxisYmax(qreal newAxisYmax)
{
if (qFuzzyCompare(m_axisYmax, newAxisYmax))
return;
m_axisYmax = newAxisYmax;
m_axisY->setMax(m_axisYmax);
emit axisYmaxChanged();
}
qreal bargraph::axisYmin() const
{
return m_axisYmin;
}
void bargraph::setAxisYmin(qreal newAxisYmin)
{
if (qFuzzyCompare(m_axisYmin, newAxisYmin))
return;
m_axisYmin = newAxisYmin;
m_axisY->setMin(m_axisYmin);
emit axisYminChanged();
}
void bargraph::deleteLineSeries()
{
QMapIterator<QString, QLineSeries *> i(m_mapLineSeries);
while (i.hasNext()) {
i.next();
m_chart->removeSeries(i.value());
delete i.value();
}
m_mapLineSeries.clear();
}
void bargraph::deleteLineSeries(QString seriesName)
{
QLineSeries *tmp = m_mapLineSeries.value(seriesName,nullptr);
if(!tmp)
{
m_chart->removeSeries(tmp);
delete tmp;
m_mapLineSeries.remove(seriesName);
}
}
void bargraph::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::LeftButton)
{
qDebug()<<event->pos()- QPoint(110,0);
m_chart->mapToValue(event->pos() - QPoint(110,0));
}
}