LineChart.qml 4.14 KB
Newer Older
Marco Martin's avatar
Marco Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 *   Copyright 2019 Marco Martin <mart@kde.org>
 *   Copyright 2019 David Edmundson <davidedmundson@kde.org>
 *   Copyright 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import QtQuick 2.9
import QtQuick.Layouts 1.1

import org.kde.kirigami 2.8 as Kirigami

import org.kde.ksysguard.sensors 1.0 as Sensors
import org.kde.ksysguard.faces 1.0 as Faces
import org.kde.quickcharts 1.0 as Charts

Charts.LineChart {
    id: chart
    
    //property var sensors: root.controller.highPrioritySensorIds

    readonly property alias sensorsModel: sensorsModel
37
    property int maximumHistory: root.controller.faceConfiguration.rangeToX - root.controller.faceConfiguration.rangeFromX
Marco Martin's avatar
Marco Martin committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    direction: Charts.XYChart.ZeroAtEnd

    fillOpacity: root.controller.faceConfiguration.lineChartFillOpacity / 100
    stacked: root.controller.faceConfiguration.lineChartStacked
    smooth: root.controller.faceConfiguration.lineChartSmooth

    //TODO: Have a central heading here too?
    //TODO: Have a plasmoid config value for line thickness?

    xRange {
        from: root.controller.faceConfiguration.rangeFromX
        to: root.controller.faceConfiguration.rangeToX
        automatic: root.controller.faceConfiguration.rangeAutoX
    }
    yRange {
54 55 56 57 58
        readonly property bool stackedAuto: root.controller.faceConfiguration.rangeAutoY && root.controller.faceConfiguration.lineChartStacked
        from: stackedAuto ? Math.min(sensorsModel.minimum, 0) :  root.controller.faceConfiguration.rangeFromY
        to: stackedAuto ? sensorsModel.stackedMaximum :  root.controller.faceConfiguration.rangeToY
        automatic: (root.controller.faceConfiguration.rangeAutoY && !root.controller.faceConfiguration.lineChartStacked)
            || stackedAuto && yRange.from == yRange.to
Marco Martin's avatar
Marco Martin committed
59 60 61 62 63
    }

    Sensors.SensorDataModel {
        id: sensorsModel
        sensors: root.controller.highPrioritySensorIds
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        property double stackedMaximum: yRange.stackedAuto ? calcStackedMaximum() : 0

        function calcStackedMaximum() {
            let max = 0
            for (let i = 0; i < sensorsModel.sensors.length; ++i) {
                max += sensorsModel.data(sensorsModel.index(0, i), Sensors.SensorDataModel.Maximum)
            }
            return max
        }
    }

    Connections {
        target: sensorsModel
        enabled: yRange.stackedAuto
        function onColumnsInserted() {
            sensorsModel.stackedMaximum = sensorsModel.calcStackedMaximum()
        }
        function onColumnsRemoved() {
            sensorsModel.stackedMaximum = sensorsModel.calcStackedMaximum()
        }
        function onSensorMetaDataChanged() {
            sensorsModel.stackedMaximum = sensorsModel.calcStackedMaximum()
        }
Marco Martin's avatar
Marco Martin committed
87 88 89 90 91 92 93 94 95
    }

    Instantiator {
        model: sensorsModel.sensors
        delegate: Charts.ModelHistorySource {
            model: sensorsModel
            column: index
            row: 0
            roleName: "Value"
96
            maximumHistory: chart.maximumHistory
97
            interval: 2000
Marco Martin's avatar
Marco Martin committed
98 99 100 101 102 103 104 105 106 107
        }
        onObjectAdded: {
            chart.insertValueSource(index, object)
        }
        onObjectRemoved: {
            chart.removeValueSource(object)
        }
    }

    colorSource: root.colorSource
Marco Martin's avatar
Marco Martin committed
108
    nameSource: Charts.ModelSource {
109 110 111 112
        roleName: "Name";
        model: sensorsModel
        indexColumns: true
    }
Marco Martin's avatar
Marco Martin committed
113
    shortNameSource: Charts.ModelSource {
114
        roleName: "ShortName";
Marco Martin's avatar
Marco Martin committed
115 116 117 118 119
        model: sensorsModel
        indexColumns: true
    }
}