/* * Copyright (C) 2023 Arseniy Movshev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, 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 General Public License * along with this program. If not, see . */ import QtQuick 2.15 import org.asteroid.controls 1.0 Item { id: root property real startValue: 0 property real endValue: 0 property real scaleFactor: 1 // to avoid float issues, set the range using ints and then apply this scale factor to the output property int minLabels: 3 property int maxLabels: 8 property real valueDivisionsInterval: 0 property real startValueDivision: 0 property real valuesDelta property bool labelK: true onStartValueChanged: update() onEndValueChanged: update() onMinLabelsChanged: update() onMaxLabelsChanged: update() implicitWidth: childrenRect.width function update() { // this tries to guess how to generate nice looking labels valuesDelta = endValue - startValue var powTen = Math.pow(10,Math.trunc(Math.log10(valuesDelta))) var interval = powTen var numLabel = Math.floor(valuesDelta/interval) if (numLabel > maxLabels) { interval = powTen*2 numLabel = valuesDelta/interval if (numLabel > maxLabels) { interval = powTen*5 numLabel = valuesDelta/interval if (numLabel > maxLabels) { interval = powTen*10 numLabel = valuesDelta/interval } } } if (numLabel < minLabels) { interval = powTen/2 numLabel = valuesDelta/interval if (numLabel < minLabels) { interval = powTen/5 numLabel = valuesDelta/interval if (numLabel < minLabels) { interval = powTen/10 numLabel = valuesDelta/interval } } } valueDivisionsInterval = interval labelsRepeater.model = Math.round(numLabel) + 1 startValueDivision = interval*Math.trunc(startValue/interval) } Repeater { id: labelsRepeater delegate: Label { anchors.right: parent.right property real value: root.startValueDivision + root.valueDivisionsInterval*index text: (value*root.scaleFactor > 1000 & root.labelK) ? value*root.scaleFactor/1000 + "k" : value*root.scaleFactor //maybe this spaghetti could be better written but it's only run once on item load font.pixelSize: Dims.w(5) y: parent.height - (parent.height)*((value-root.startValue)/root.valuesDelta) - height/2 verticalAlignment: Text.AlignVCenter } } }