#if 0

// code from 2022.03.22

#include "visualizationchart.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <vector>
#include <iomanip>
#include <cmath>

// https://russianblogs.com/article/5807234373/

double calculateDistance(std::pair<int, int> &x, std::pair<int, int> &y)
{
    return std::sqrt(std::pow(x.first - y.first, 2) +
                std::pow(x.second - y.second, 2));
}

double calculateDistance(std::pair<double, double> &x, std::pair<double, double> &y)
{
    return std::sqrt(std::pow(x.first - y.first, 2) +
                std::pow(x.second - y.second, 2));
}

double distanceBetweenTwoPointsOnOneAxis(int x1, int x2)
{
    vector<std::pair<int, int>> vec = { {x1, 0} , {x2, 0}, };
    double dist = calculateDistance(vec[0], vec[1]);
    return dist;
}



VisualizationChart::VisualizationChart() : QChart(), mQue(1,1)
{
    initChart();
}
VisualizationChart::VisualizationChart(int w, int h, int minY, int maxY, bool keepXGrowToTheRight, bool keepYFullRange)
    : QChart(), mW(w), mH(h), mMinY(minY), mMaxY(maxY), mMakeXaxisLengtGrowToTheRightAccordingIncommingXdata(keepXGrowToTheRight), mMakeYaxisLengtAutoResizeAccordingIncommingYdata(!keepYFullRange), mQue(1,1)
{
    initChart();
}

void VisualizationChart::initChart()
{
    xi = 0;

    xmin = 0;
    xmax = 10 + xeps;
    ymin = 0;
    // ymax = 500 + yeps;
    ymax = yeps;

    axisX = new QValueAxis();
    axisY = new QValueAxis();

    axisX->setTickCount(1);
    axisX->setRange(xmin, xmax);

    axisY->setRange(mMinY - mQue.offset(), mMaxY + mQue.offset());
    //axisY->setRange(mMinY, mMaxY);
    //axisY->setRange(ymin, ymax);

    //mMaxY = 60;
    //mMinY = 0;
    mRangeY = static_cast<int>(distanceBetweenTwoPointsOnOneAxis(mMinY, mMaxY));
    mQue.setSize(mRangeY);

    // legend
    this->legend()->hide();
    this->legend()->setAlignment(Qt::AlignBottom);
    QFont font(this->legend()->font());
    font.setPixelSize(18);
    font.setBold(true);
    this->legend()->setFont(font);

    // X
    this->addAxis(axisX, Qt::AlignBottom);
    // Y
    //this->addAxis(axisY, Qt::AlignLeft);
    this->addAxis(axisY, Qt::AlignRight);


/*    // Series
    ssMax = new QSplineSeries(this);
    ssMid = new QSplineSeries(this);
    ssMin = new QSplineSeries(this);

    ssMax->setName(tr("Максимальная", "Series name"));
    ssMid->setName(tr("Средняя", "Series name"));
    ssMin->setName(tr("Низкая", "Series name"));

    ssMax->setColor(Qt::red);
    ssMid->setColor(Qt::green);
    ssMin->setColor(Qt::yellow);

    this->addSeries(ssMax);
    this->addSeries(ssMid);
    this->addSeries(ssMin);

    ssMax->attachAxis(axisX);
    ssMax->attachAxis(axisY);

    ssMid->attachAxis(axisX);
    ssMid->attachAxis(axisY);

    ssMin->attachAxis(axisX);
    ssMin->attachAxis(axisY);

    connect(ssMax, &QSplineSeries::pointAdded, this, &VisualizationChart::onPointAdded);
*/

    // Vertial line that follows maouse cursor

    QFont fontXYTitle(this->legend()->font());
    fontXYTitle.setPixelSize(18);

    m_lineVert = new QGraphicsLineItem(this);
    QPen pen1(Qt::lightGray, 1, Qt::SolidLine, Qt::FlatCap);
    m_lineVert->setPen(pen1);

    m_lineVertText = new QGraphicsTextItem(this);
    m_lineVertText->setPlainText("");
    m_lineVertText->setDefaultTextColor(Qt::black);
    m_lineVertText->setFont(fontXYTitle);



    if(mEnableOurCustomAxesOrDeafultAxesInstead)
    {
        axisX->hide();
        axisY->hide();

        // < - 981 -> //  V 441 ^  //
        // int h = this->plotArea().height();
        // int w = this->plotArea().width();
        // int w = parent()-();
        // int h = parent()->height();

        {
            // <- ^
            QGraphicsRectItem *rect = new QGraphicsRectItem(this);
            QPen               penR(Qt::magenta, 1, Qt::SolidLine, Qt::FlatCap);
            rect->setPen(penR);
            rect->setRect(0, 0, 10, 10);
        }
        {
            // <- V
            QGraphicsRectItem *rect = new QGraphicsRectItem(this);
            QPen               penR(Qt::magenta, 1, Qt::SolidLine, Qt::FlatCap);
            rect->setPen(penR);
            rect->setRect(0, 430, 10, 10);
        }
        {
            // -> ^
            QGraphicsRectItem *rect = new QGraphicsRectItem(this);
            QPen               penR(Qt::magenta, 1, Qt::SolidLine, Qt::FlatCap);
            rect->setPen(penR);
            rect->setRect(910, 0, 10, 10);
        }



        //
        m_coordX = new QGraphicsSimpleTextItem(this);
        m_coordX->setPos(this->size().width() / 2 - 50, this->size().height());
        // m_coordX->setText("X: ");
        m_coordY = new QGraphicsSimpleTextItem(this);
        m_coordY->setPos(this->size().width() / 2 + 50, this->size().height());
        // m_coordY->setText("Y: ");

        mChartYTitle = new QGraphicsTextItem(this);
        mChartYTitle->setPlainText("Температура, °C");
        mChartYTitle->setFont(fontXYTitle);
        mChartYTitle->setDefaultTextColor(Qt::lightGray);
        mChartYTitle->setX(50); // size().width()
        mChartYTitle->setY(30); // size().height()

        mChartXTitle = new QGraphicsTextItem(this);
        mChartXTitle->setPlainText("Время, с");
        mChartXTitle->setFont(mChartYTitle->font());
        mChartXTitle->setDefaultTextColor(Qt::lightGray);
        mChartXTitle->setX(880 - 50); // size().width()
        mChartXTitle->setY(390);      // size().height()


        // m_lineHoriz1->setLine(m_coordX->pos().x(), m_coordX->pos().y() + yStep, size().width(), m_coordX->pos().y() +
        // yStep);
        /*
        int xStart    = 45;
        int yTop      = 60;
        int xEnd      = xStart + 880;
        int yBottom   = 460;
        int yLen      = yBottom - yTop;
        int xLen      = xEnd - xStart;
        int yPartsLen = yLen / 6;
        int xPartsLen = xLen / 6;
        int iPart = 0;
        */
        int hundred100= mH+10;
        double onepercent = (double)hundred100 / 100.0;

        //mRangeY; //6;

        int step = 0;
        int a = mRangeY;
        if((a>0)&&(a<=10)) {
        step = 1;
        }
        else if((a>10)&&(a<=50)) {
        step = 5;
        }
        else if((a>50)&&(a<=100)) {
        step = 10;
        }
        else if((a>100)&&(a<=250)) {
        step = 25;
        }
        else if((a>250)&&(a<=500)) {
        step = 50;
        }
        else if((a>500)&&(a<=1000)) {
        step = 100;
        }
        else if((a>1000)&&(a<=5000)) {
        step = 500;
        }
        else if((a>5000)&&(10000<=a)) {
        step = 1000;
        }

        int horizLines = mRangeY / step;
        int yStep     = step; //mRangeY / horizLines ; //10;
        int xStart    = 45;
        int yTop      = hundred100 * 0.130434; //60;
        int xEnd      = xStart + 880;
        int yBottom   = hundred100 * 1.0; //460;
        int yLen      = yBottom - yTop;
        int xLen      = xEnd - xStart;
        int yPartsLen = yLen / horizLines;
        //int yPartsLen = yLen / ;
        int xPartsLen = xLen / horizLines;
        int iPart     = 0;
        int xhTextEndOffset = hundred100 * 0.04347826; //20;
        int yhTextTopOffset = hundred100 * 0.0652173913; //30;


        // Dot Y lines with numbers on right side

        QPen penDots(Qt::lightGray, 1, Qt::DotLine, Qt::FlatCap);
        QPen penLast(Qt::lightGray, 1, Qt::SolidLine, Qt::FlatCap);

        mlinesHoriz.clear();
        for(int i = 0, ilen = horizLines; i<ilen; i++) {
            mlinesHoriz.push_back(new QGraphicsLineItem(this));
        }
        /*
        m_lineHoriz1 = new QGraphicsLineItem(this);
        m_lineHoriz2 = new QGraphicsLineItem(this);
        m_lineHoriz3 = new QGraphicsLineItem(this);
        m_lineHoriz4 = new QGraphicsLineItem(this);
        m_lineHoriz5 = new QGraphicsLineItem(this);
        m_lineHorizX = new QGraphicsLineItem(this);
        */
        for(int i = 0, ilen = horizLines; i<ilen; i++) {
            mlinesHoriz[i]->setPen(penDots); //->setFont(fontXYTitle);
            if(i == (ilen - 1))
                mlinesHoriz[i]->setPen(penLast); //->setFont(fontXYTitle);
        }
        /*
        m_lineHoriz1->setPen(penDots);
        m_lineHoriz2->setPen(penDots);
        m_lineHoriz3->setPen(penDots);
        m_lineHoriz4->setPen(penDots);
        m_lineHoriz5->setPen(penDots);
        m_lineHorizX->setPen(penLast);
        */
        for(int iPart = 0, ilen = horizLines; iPart<ilen; iPart++) {
            mlinesHoriz[iPart]->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen)); //mlinesHoriz.push_back(new QGraphicsTextItem(this));
        }
        /*
        iPart=0; m_lineHoriz1->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen));
        ++iPart; m_lineHoriz2->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen));
        ++iPart; m_lineHoriz3->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen));
        ++iPart; m_lineHoriz4->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen));
        ++iPart; m_lineHoriz5->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen));
        ++iPart; m_lineHorizX->setLine(xStart, yTop + (iPart * yPartsLen), xEnd, yTop + (iPart * yPartsLen));
        */

        mTextsHoriz.clear();
        // Text near Lines
        for(int i = 0, ilen = horizLines; i<ilen; i++) {
            mTextsHoriz.push_back(new QGraphicsTextItem(this));
        }
        /*
        mTextHoriz1 = new QGraphicsTextItem(this);
        mTextHoriz2 = new QGraphicsTextItem(this);
        mTextHoriz3 = new QGraphicsTextItem(this);
        mTextHoriz4 = new QGraphicsTextItem(this);
        mTextHoriz5 = new QGraphicsTextItem(this);
        */
        for(int i = 0, number = mMaxY, ilen = mTextsHoriz.size(); i<ilen; i++) {
            if(i==0)
                mTextsHoriz[i]->setPlainText("");
            else
            {
                number -= yStep;
                mTextsHoriz[i]->setPlainText(QString::number(number));
            }
        }
        /*
        mTextHoriz1->setPlainText("");
        mTextHoriz2->setPlainText("40");
        mTextHoriz3->setPlainText("30");
        mTextHoriz4->setPlainText("20");
        mTextHoriz5->setPlainText("10");
        */
        for(int i = 0, ilen = mTextsHoriz.size(); i<ilen; i++) {
                mTextsHoriz[i]->setDefaultTextColor(Qt::lightGray);
        }
        /*
        mTextHoriz1->setDefaultTextColor(Qt::lightGray);
        mTextHoriz2->setDefaultTextColor(Qt::lightGray);
        mTextHoriz3->setDefaultTextColor(Qt::lightGray);
        mTextHoriz4->setDefaultTextColor(Qt::lightGray);
        mTextHoriz5->setDefaultTextColor(Qt::lightGray);
        */
        for(int i = 0, ilen = mTextsHoriz.size(); i<ilen; i++) {
                mTextsHoriz[i]->setFont(fontXYTitle);
        }
        /*
        mTextHoriz1->setFont(fontXYTitle);
        mTextHoriz2->setFont(fontXYTitle);
        mTextHoriz3->setFont(fontXYTitle);
        mTextHoriz4->setFont(fontXYTitle);
        mTextHoriz5->setFont(fontXYTitle);
        */
        for(int iPart = 0, ilen = mTextsHoriz.size(); iPart<ilen; iPart++) {
                //mTextsHoriz[i]->setFont(fontXYTitle);
                mTextsHoriz[iPart]->setPos(xEnd - xhTextEndOffset, yTop + (iPart * yPartsLen) - yhTextTopOffset);
        }
        /*
        iPart = 0;  mTextHoriz1->setPos(xEnd - 20, yTop + (iPart * yPartsLen) - 30);
        ++iPart;    mTextHoriz2->setPos(xEnd - 20, yTop + (iPart * yPartsLen) - 30);
        ++iPart;    mTextHoriz3->setPos(xEnd - 20, yTop + (iPart * yPartsLen) - 30);
        ++iPart;    mTextHoriz4->setPos(xEnd - 20, yTop + (iPart * yPartsLen) - 30);
        ++iPart;    mTextHoriz5->setPos(xEnd - 20, yTop + (iPart * yPartsLen) - 30);
        */

        // Time textes on X Axis

        QPen penSolid(Qt::lightGray, 1, Qt::SolidLine, Qt::FlatCap);
        m_lineVert1 = new QGraphicsLineItem(this);
        m_lineVert2 = new QGraphicsLineItem(this);
        m_lineVert3 = new QGraphicsLineItem(this);
        m_lineVert4 = new QGraphicsLineItem(this);
        m_lineVert5 = new QGraphicsLineItem(this);
        m_lineVert6 = new QGraphicsLineItem(this);

        m_lineVert1->setPen(penSolid);
        m_lineVert2->setPen(penSolid);
        m_lineVert3->setPen(penSolid);
        m_lineVert4->setPen(penSolid);
        m_lineVert5->setPen(penSolid);
        m_lineVert6->setPen(penSolid);

        iPart = 0; m_lineVert1->setLine(xStart + (iPart * xPartsLen), yBottom - yPartsLen - 4, xStart + (iPart * xPartsLen), yBottom - yPartsLen + 30);
        ++iPart;   m_lineVert2->setLine(xStart + (iPart * xPartsLen), yBottom - yPartsLen - 4, xStart + (iPart * xPartsLen), yBottom - yPartsLen + 30);
        ++iPart;   m_lineVert3->setLine(xStart + (iPart * xPartsLen), yBottom - yPartsLen - 4, xStart + (iPart * xPartsLen), yBottom - yPartsLen + 30);
        ++iPart;   m_lineVert4->setLine(xStart + (iPart * xPartsLen), yBottom - yPartsLen - 4, xStart + (iPart * xPartsLen), yBottom - yPartsLen + 30);
        ++iPart;   m_lineVert5->setLine(xStart + (iPart * xPartsLen), yBottom - yPartsLen - 4, xStart + (iPart * xPartsLen), yBottom - yPartsLen + 30);
        ++iPart;   m_lineVert6->setLine(xStart + (iPart * xPartsLen), yBottom - yPartsLen - 4, xStart + (iPart * xPartsLen), yBottom - yPartsLen + 30);

        // Time textes on X Axis

        //
        //
        //
        //         mTextVert1  Vert2  Vert3  ... VertN

        mTextVert1 = new QGraphicsTextItem(this);
        mTextVert2 = new QGraphicsTextItem(this);
        mTextVert3 = new QGraphicsTextItem(this);
        mTextVert4 = new QGraphicsTextItem(this);
        mTextVert5 = new QGraphicsTextItem(this);
        mTextVert6 = new QGraphicsTextItem(this);

        mTextVert1->setPlainText("0");
        mTextVert2->setPlainText("30");
        mTextVert3->setPlainText("60");
        mTextVert4->setPlainText("90");
        mTextVert5->setPlainText("120");
        mTextVert6->setPlainText("150");

        mTextVert1->setDefaultTextColor(Qt::lightGray);
        mTextVert2->setDefaultTextColor(Qt::lightGray);
        mTextVert3->setDefaultTextColor(Qt::lightGray);
        mTextVert4->setDefaultTextColor(Qt::lightGray);
        mTextVert5->setDefaultTextColor(Qt::lightGray);
        mTextVert6->setDefaultTextColor(Qt::lightGray);

        mTextVert1->setFont(fontXYTitle);
        mTextVert2->setFont(fontXYTitle);
        mTextVert3->setFont(fontXYTitle);
        mTextVert4->setFont(fontXYTitle);
        mTextVert5->setFont(fontXYTitle);
        mTextVert6->setFont(fontXYTitle);

        iPart = 0;
        mTextVert1->setPos(xStart + (iPart * xPartsLen) + 5, yBottom - yPartsLen);
        ++iPart;
        mTextVert2->setPos(xStart + (iPart * xPartsLen) + 5, yBottom - yPartsLen);
        ++iPart;
        mTextVert3->setPos(xStart + (iPart * xPartsLen) + 5, yBottom - yPartsLen);
        ++iPart;
        mTextVert4->setPos(xStart + (iPart * xPartsLen) + 5, yBottom - yPartsLen);
        ++iPart;
        mTextVert5->setPos(xStart + (iPart * xPartsLen) + 5, yBottom - yPartsLen);
        ++iPart;
        mTextVert6->setPos(xStart + (iPart * xPartsLen) + 5, yBottom - yPartsLen);

    } // mEnableDeafultAxesOrOurCustomAxesInstead

    //
    // THIS IS IMPORTANT !!!
    //
    setAcceptHoverEvents(true);
}

void VisualizationChart::addSeriesWithNumber(int number, QString name, QColor color)
{
    bool found = (mSeriesMap.count(number) >= 1);
    if(found)
        return;
/*
    QSplineSeries* newSer = new QSplineSeries(this);
    newSer->setName(name);
    newSer->setColor(color);
    newSer->attachAxis(axisX);
    newSer->attachAxis(axisY);

    //connect(newSer, &QSplineSeries::pointAdded, this, &VisualizationChart::onPointAdded);

    mSeriesMap.insert(number, newSer);
*/


        //connect(newSer, &QSplineSeries::pointAdded, this, &VisualizationChart::onPointAdded);

        mSeriesMap.insert(number, new QLineSeries(this));
        mSeriesMap[number]->setName(name);
        mSeriesMap[number]->setColor(color);

        // !! IMPORTANAT !!! CALL THIS FIRST
        this->addSeries(mSeriesMap[number]);

        // !! IMPORTANAT !!! CALL THIS SECOND
        mSeriesMap[number]->attachAxis(axisX);
        mSeriesMap[number]->attachAxis(axisY);

        /*// Series
        ssMax = new QSplineSeries(this);
        ssMax->setName(tr("Максимальная"));
        ssMax->setColor(Qt::red);
        this->addSeries(ssMax);
        ssMax->attachAxis(axisX);
        ssMax->attachAxis(axisY);
        */
}



void VisualizationChart::setTitleForX(QString s)
{
    mChartXTitle->setPlainText(s);
}

void VisualizationChart::setTitleForY(QString s)
{
    mChartYTitle->setPlainText(s);
}


QLineSeries* VisualizationChart::getSplineSeries(int num)
{
    bool found = (this->mSeriesMap.count(num) >= 1);
    return (found ? this->mSeriesMap[num] : nullptr);
}
QColor VisualizationChart::getSplineSeriesColor(int num)
{
    bool found = (this->mSeriesMap.count(num) >= 1);
    return (found ? this->mSeriesMap[num]->color() : QColor(0,0,0));
}

/*
QSplineSeries &VisualizationChart::getSplineSeries()
{
    return *(this->ssMax);
}

QColor VisualizationChart::getFirstSeriesColor()
{
    return ssMax->color();
}
QColor VisualizationChart::getSecondSeriesColor()
{
    return ssMid->color();
}
QColor VisualizationChart::getThirdSeriesColor()
{
    return ssMin->color();
}
*/

void VisualizationChart::append(const QPointF point)
{
    //auto mm = std::minmax_element(ssMax->points().begin(), ssMax->points().end());
    //qDebug() << *mm.first << *mm.second;
    mQue.add(point.y());
    //mQue.addOffset();


    //*ssMax << QPointF(point.x(), point.y() - 0);
    //*ssMid << QPointF(point.x(),point.y() - 5);
    //*ssMin << QPointF(point.x(),point.y() - 10);
}

void VisualizationChart::appendPointToSeries(int serNum, const QPointF point)
{
    //auto mm = std::minmax_element(ssMax->points().begin(), ssMax->points().end());
    //qDebug() << *mm.first << *mm.second;

    mQue.add(point.y());

    *mSeriesMap[serNum] << QPointF(point.x(), point.y());

    refreshSeries(serNum);

    //if(mSeriesMap[serNum] == mSeriesMap.first())
    //    *ssMax << QPointF(point.x(), point.y() - 0);
    //*ssMid << QPointF(point.x(),point.y() - 5);
    //*ssMin << QPointF(point.x(),point.y() - 10);
}

void VisualizationChart::append(const qreal x, const qreal y)
{
    append(QPointF(x, y));
}
void VisualizationChart::append(const qreal y)
{
    append(QPointF(++xi, y));
}

void VisualizationChart::appendNextX()
{
    (++xi);
}
void VisualizationChart::appendYToSeries(int serNum, const qreal y)
{
    appendPointToSeries(serNum, QPointF(xi, y));
}

VisualizationChart &VisualizationChart::operator+=(const QPointF point)
{
    append(point);
    return *this;
}
VisualizationChart &VisualizationChart::operator+=(const qreal y)
{
    append(y);
    return *this;
}

VisualizationChart &VisualizationChart::operator<<(const QPointF point)
{
    append(point);
    return *this;
}
VisualizationChart &VisualizationChart::operator<<(const qreal y)
{
    append(y);
    return *this;
}


void VisualizationChart::refreshSeries(int serNum)
{
    auto series = mSeriesMap[serNum];
    auto points = series->points();

    int x = 0, y = 0;
    x = (points.last().x());
    y = (points.last().y());

    // Delete the first items if the number of points has reached a threshold
    while (series->points().length() > 0 && series->points().length() >= xlimit)
        series->remove(0);

    if (points.size() == 0)
        return;

    xmin = xmax = x;
    ymax        = y;

    // Update the chart area
    for (int i = 0; i < series->count(); i++) {
        const QPointF &point = series->at(i);

        if (point.x() > xmax)
            xmax = (point.x());

        if (point.x() < xmin)
            xmin = (point.x());

        if (point.y() > ymax)
            ymax = (point.y());
    }

    ymin = 0;
    //ymax = ymax + yeps;

    // Update the axis
    if(mMakeXaxisLengtGrowToTheRightAccordingIncommingXdata)
    {
        axisX->setRange(0, xmax);
    }
    else
    {
        axisX->setRange(xmin, xmax);
    }

    if(mMakeYaxisLengtAutoResizeAccordingIncommingYdata)
    {
        int min = mQue.min() - mQue.offset();
        int max = mQue.max() + mQue.offset();

        axisY->setRange(min, max);
    }
    else
    {
        // keep Y axis static from the start
        int min = mMinY - mQue.offset();
        int max = mMaxY + mQue.offset();

        axisY->setRange(min , max);
    }
}

void VisualizationChart::onPointAdded(int index)
{
    /*
    auto series = ssMax;
    auto points = ssMax->points();

    int x = 0, y = 0;
    x = (ssMax->points()[index].x());
    y = (ssMax->points()[index].y());

    // Delete the first items if the number of points has reached a threshold
    while (ssMax->points().length() > 0 && ssMax->points().length() >= xlimit)
        series->remove(0);

    if (points.size() == 0)
        return;

    xmin = xmax = x;
    ymax        = y;

    // Update the chart area
    for (int i = 0; i < series->count(); i++) {
        const QPointF &point = series->at(i);

        if (point.x() > xmax)
            xmax = (point.x());

        if (point.x() < xmin)
            xmin = (point.x());

        if (point.y() > ymax)
            ymax = (point.y());
    }

    xmin = xmin;
    xmax = xmax;
    ymin = 0;
    //    ymax = ymax + yeps;
    ymax = ymax;

    // Update the axis
    axisX->setRange(xmin, xmax);

    //axisY->setRange(ymin, ymax);
    //axisY->setRange(mQue.min() - mQue.offset(), mQue.max() + mQue.offset());

    if(mMakeYaxisLengtAutoResizeAccordingIncommingYdata)
    {
        axisY->setRange(mQue.min() - mQue.offset(), mQue.max() + mQue.offset());
    }
    else
    {
        // keep Y axis static from the start
        axisY->setRange(mMinY - mQue.offset(), mMaxY + mQue.offset());
    }
    //*/
/*
    int a = 0;

    if(mMakeYaxisLengtAutoResizeAccordingIncommingYdata)
    {
        axisY->setRange(mQue.min() - mQue.offset(), mQue.max() + mQue.offset());
    }
    else
    {
        // keep Y axis static from the start
        axisY->setRange(mMinY - mQue.offset(), mMaxY + mQue.offset());
    }
*/
}

void VisualizationChart::showCoordinatesNearCursor(QGraphicsSceneHoverEvent *event)
{
    int x = event->scenePos().x();
    int y = event->scenePos().y();
    m_lineVertText->setPlainText(QString("%1 %2").arg(x).arg(y));
    m_lineVertText->setPos(x, y);
}

void VisualizationChart::showUpdatedCoordinatesNearCursor(QGraphicsSceneHoverEvent *event)
{
    static int chartTopY    = 39;
    static int chartLeftX   = 39;
    static int chartBottomY = 385;
    static int chartRightX  = 910;
    static int chartFullX = chartRightX - chartLeftX;

    int x = (event->scenePos().x() - chartLeftX);
    if(x<0 || x>chartRightX) x = 0;
    int y = (event->scenePos().y() - chartTopY);
    if(y<0 || x>chartBottomY) y = 0;

    //this->axisX->;
    int min = axisX->min();
    int max = axisX->max();
    int width = max - min;

    int secs = xi;
    int onePart = chartFullX / secs;
    int halfPart = onePart / 2;
    int val = 0;
    for(int i = 0, ilen = secs, left=0, right=0; i<ilen; ++i)
    {
        left = i * onePart;
        right = (i+1) * onePart;
        if(x>=left && x<=right)
        {
            if(x>=left && x<=left+halfPart) {
                val = i+1;
                break;
            }
            if(x>=right-halfPart && x<=right) {
                val = i+2;
                break;
            }
        }
    }

    //m_lineVertText->setPlainText(QString("%1 %2 - %3 %4 ").arg(min).arg(max).arg(x).arg(y));
    //m_lineVertText->setPlainText(QString("%1 %2 ").arg(x).arg(y));
    m_lineVertText->setPlainText(QString("%1 %2 %3 ").arg(val).arg(x).arg(y));
    m_lineVertText->setPos(x, y);
}

void VisualizationChart::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    //axisX->
    //if(m_coordX)
    //    int xx = m_coordX->pos().x();
    //if(m_coordX)
    //    int yy = m_coordX->pos().y();


#if VERSION_1
    int x1 = event->scenePos().x();
    int y1 = 58;
    int x2 = x1;
    int y2 = size().height() - 50; // y1 + 100;
    m_lineVert->setLine(x1, y1, x2, y2);
    QChart::hoverEnterEvent(event);

    int secs = x1 - 45;

    emit sendSecs(secs);

    if (secs <= 0)
        m_lineVertText->setPlainText(QString(""));
    else if (secs < 60)
        m_lineVertText->setPlainText(QString("%1 с").arg(secs));
    else if (secs % 60 == 0)
        m_lineVertText->setPlainText(QString("%1 мин").arg(secs / 60));
    else if (secs > 60)
        m_lineVertText->setPlainText(QString("%1 мин %2 с").arg(secs / 60).arg(secs % 60));

    m_lineVertText->setPos(x1, y1 + 20);
#endif

    //showCoordinatesNearCursor(event);
    //showUpdatedCoordinatesNearCursor(event);


    int x1 = event->scenePos().x();
    int y1 = 58;
    int x2 = x1;
    int y2 = size().height() - 50; // y1 + 100;
    m_lineVert->setLine(x1, y1, x2, y2);
    QChart::hoverEnterEvent(event);

    int secs = x1 - 45;

    emit sendSecs(secs);

    static int chartTopY    = 39;
    static int chartLeftX   = 25;
    static int chartBottomY = 385;
    static int chartRightX  = 900;
    static int chartFullX = chartRightX - chartLeftX;

    int x = (event->scenePos().x() - chartLeftX);
    if(x<0 || x>chartRightX) x = 0;
    int y = (event->scenePos().y() - chartTopY);
    if(y<0 || x>chartBottomY) y = 0;

    //this->axisX->;
    int min = axisX->min();
    int max = axisX->max();
    int width = max - min;

    int ssecs = xi;
    int onePart = chartFullX / ssecs;
    int halfPart = onePart / 2;
    int val = 0;
    for(int i = 0, ilen = ssecs, left=0, right=0; i<ilen; ++i)
    {
        left = i * onePart;
        right = (i+1) * onePart;
        if(x>=left && x<=right)
        {
            if(x>=left && x<=left+halfPart) {
                val = i; //i+1;
                break;
            }
            if(x>=right-halfPart && x<=right) {
                val = i+1; //i+2;
                break;
            }
        }
    }

    //m_lineVertText->setPlainText(QString("%1 %2 - %3 %4 ").arg(min).arg(max).arg(x).arg(y));
    //m_lineVertText->setPlainText(QString("%1 %2 ").arg(x).arg(y));
    //m_lineVertText->setPlainText(QString("%1 %2 %3 ").arg(val).arg(x).arg(y));

    if (val <= 0)
        m_lineVertText->setPlainText(QString(""));
    else if (val < 60)
        m_lineVertText->setPlainText(QString("%1 с").arg(val));
    else if (val % 60 == 0)
        m_lineVertText->setPlainText(QString("%1 мин").arg(val / 60));
    else if (val > 60)
        m_lineVertText->setPlainText(QString("%1 мин %2 с").arg(val / 60).arg(val % 60));

    m_lineVertText->setPos(x1, y1 + 20);
}

void VisualizationChart::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    // ensure it is visible
    if (this->isVisible()) {
        // Update cursor position (text to be displayed)
        QString string = QString("%1, %2").arg(event->scenePos().x()).arg(event->scenePos().y());
    }
}
#endif
