Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/Sparkline.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick

Item {
    id: root

    required property Theme theme
    property var values: []
    property var valuesDown: []
    property color barColor: theme ? theme.ui.accent : "white"
    property color barColorDown: theme ? theme.info : "#3ec5e3"
    property real scaleMax: 0
    property bool split: false

    readonly property int shown: Math.max(1, Math.floor(width / 2))
    readonly property var slice: tail(values)
    readonly property var sliceDown: tail(valuesDown)
    readonly property real maxV: {
        if (root.scaleMax > 0)
            return root.scaleMax;

        let m = 0;
        for (let i = 0; i < root.slice.length; i++)
            m = Math.max(m, Number(root.slice[i]) || 0);

        if (root.split) {
            for (let j = 0; j < root.sliceDown.length; j++)
                m = Math.max(m, Number(root.sliceDown[j]) || 0);
        }
        return m;
    }
    readonly property real budget: root.split ? (root.height - 1) / 2 : root.height

    function tail(v) {
        const a = v || [];
        return a.length > root.shown ? a.slice(a.length - root.shown) : a;
    }

    Row {
        anchors.right: parent.right
        anchors.bottom: root.split ? parent.verticalCenter : parent.bottom
        anchors.bottomMargin: root.split ? 1 : 0
        height: root.budget
        spacing: 0

        Repeater {
            model: root.slice

            delegate: Item {
                id: upBar

                required property var modelData

                width: 2
                height: root.budget

                Rectangle {
                    anchors.bottom: parent.bottom
                    width: parent.width
                    radius: 1
                    height: Math.max(root.split ? 0 : 1, root.budget * (root.maxV > 0 ? Math.min(1, (Number(upBar.modelData) || 0) / root.maxV) : 0))
                    color: root.barColor
                }
            }
        }
    }

    Row {
        visible: root.split
        anchors.right: parent.right
        anchors.top: parent.verticalCenter
        anchors.topMargin: 1
        height: root.budget
        spacing: 0

        Repeater {
            model: root.split ? root.sliceDown : []

            delegate: Item {
                id: downBar

                required property var modelData

                width: 2
                height: root.budget

                Rectangle {
                    anchors.top: parent.top
                    width: parent.width
                    radius: 1
                    height: Math.max(0, root.budget * (root.maxV > 0 ? Math.min(1, (Number(downBar.modelData) || 0) / root.maxV) : 0))
                    color: root.barColorDown
                }
            }
        }
    }
}