#ifndef DPBARSCHART_H
#define DPBARSCHART_H

/*******************************************************************************
** DataPanel-BarsChart
** author dsc
**
** 柱状图展示面板，用来展示统计数据(如各类报警、事件的数量)
** 采用QCustomPlot实现
**
** CustomBars
** 自定义Bars，继承自QCPBars，可以展示实时数据等信息
**
******************************************************************************/

#include "dpBaseChart.h"

class CustomBars : public QCPBars
{
public:
    explicit CustomBars(QCPAxis* keyAxis, QCPAxis* valueAxis);

    Qt::Alignment textAlignment() { return m_textAlignment; }
    double spacing() { return m_spacing; }
    QFont font() { return m_font; }

    void setTextVisible(bool);
    void setTextAlignment(Qt::Alignment);
    void setSpacing(double);
    void setFont(QFont);

protected:
    virtual void draw(QCPPainter* painter) override;

private:
    bool m_textVisible;
    Qt::Alignment m_textAlignment; //文字对齐方式
    double m_spacing; //文字与柱状图之间的间距，单位为像素
    QFont m_font; //文字的字体
};

class dpBarsChart : public dpBaseChart
{
    Q_OBJECT

public:
    dpBarsChart(QWidget *parent = nullptr);
    ~dpBarsChart();

    void setTimeRange(TimeUnit) override;
    void setDateTime(const QDateTime&) override;
    void viewHistoricalData(const QDateTime&) override;

    void synchronizeConfigData(const configurationResults&) override;

public slots:
    void onSignal_dataUpdated(const QString& dataID, const QVariant& data, const QDateTime& timestamp);

private:
    struct Bars
    {
        QColor color;
        QString name;
        QString dataID;
        RealTimeDataType dataType;
        QVector<double> keys;
        QPointer<CustomBars> qBars;

        Bars()
        {
            name = "";
            dataID = "";
        }
    };

    void initQCP();

    QCustomPlot* m_pCustomPlot;
    ChartStyle   m_chartStyle;
    //QCPBarsGroup* m_pBarsGroup;
    QHash<QString, Bars> m_bars;
    QHash<RealTimeDataType, QCPBarsGroup*> m_barsGroups;
    bool m_updateData;
    QStringList m_requestDataList;
};

#endif
