diff --git a/Modules/energy/package/contents/ui/Graph.qml b/Modules/energy/package/contents/ui/Graph.qml --- a/Modules/energy/package/contents/ui/Graph.qml +++ b/Modules/energy/package/contents/ui/Graph.qml @@ -37,7 +37,7 @@ antialiasing: true property int xPadding: 45 - property int yPadding: 10 + property int yPadding: 40 property var data //expect an array of QPointF @@ -50,19 +50,24 @@ property string yUnits: "" property string xUnits: "" + property real xDuration: 0 + property real xDivisions: 0 + property real xDivisionWidth: 0 + property real xTicksAt: xTicksAtDontCare + //internal property real plotWidth: width - xPadding - property real plotHeight: height - yPadding *2 + property real plotHeight: height - yPadding * 2 onDataChanged: { canvas.requestPaint(); } //take a QPointF function scalePoint(plot) { - var scaledX = (plot.x - xMin) * plotWidth / (xMax-xMin); - var scaledY = (plot.y - yMin) * plotHeight / (yMax-yMin); + var scaledX = (plot.x - xMin) * plotWidth / (xMax - xMin); + var scaledY = (plot.y - yMin) * plotHeight / (yMax - yMin); return Qt.point(xPadding + scaledX, height - yPadding - scaledY); @@ -118,7 +123,7 @@ //draw an outline c.strokeStyle = 'rgba(0,0,0,0.02)'; c.lineWidth = 1; - c.rect(xPadding-1, yPadding-1, plotWidth+2, plotHeight+2); + c.rect(xPadding - 1, yPadding - 1, plotWidth + 2, plotHeight + 2); // Draw the Y value texts c.fillStyle = palette.text; @@ -134,5 +139,76 @@ c.lineTo(canvas.width, y) c.stroke() } + + // Draw the X value texts + c.textAlign = "center" + c.lineWidth = 1 + c.strokeStyle = 'rgba(0, 0, 0, 0.02)' + + var xDivisions = xDuration / xDivisionWidth * 1000 + var xTickPadding = (canvas.width - xPadding) / xDivisions + var xTickPos + var xTickDate + var xTickTime + + var currentUnixTime = Date.now() + var currentDateTime = new Date() + var lastDate = "" + + var hours = currentDateTime.getHours() + var minutes = currentDateTime.getMinutes() + var seconds = currentDateTime.getSeconds() + + var diff = 0 + + if (xTicksAt == xTicksAtTwelveOClock) { + diff = ((hours - 12) * 60 * 60 + minutes * 60 + seconds) * 1000 // ms + } + + if (xTicksAt == xTicksAtFullHour) { + diff = (minutes * 60 + seconds) * 1000 // ms + } + + if (xTicksAt == xTicksAtHalfHour) { + diff = ((minutes - 30) * 60 + seconds) * 1000 // ms + } + + var xCanvasWidth = (canvas.width - xPadding) + var xTickOffset = plotWidth * (diff / 1000) / xDuration + + for (var i = xDivisions; i >= 0; i--) { + xTickPos = i*xTickPadding + xPadding - xTickOffset + + if ((xTickPos < canvas.width - 0.25 * xPadding) && + (xTickPos > xPadding)) + { + currentDateTime = new Date(currentUnixTime - (xDivisions - i) * xDivisionWidth - diff) + xTickDate = currentDateTime.toLocaleDateString(Qt.locale(), Locale.ShortFormat) + xTickTime = currentDateTime.toLocaleTimeString(Qt.locale(), Locale.ShortFormat) + + if (( (i + 1) % 2 == 0) + || (xDivisions < 10)) + { + // Display the time + c.fillText(xTickTime, xTickPos, canvas.height - yPadding / 2) + + // If the date has changed, display it + if (xTickDate != lastDate) { + c.fillText(xTickDate, xTickPos, canvas.height - yPadding / 4) + } + + // Tick markers + c.moveTo(xTickPos, canvas.height - yPadding) + c.lineTo(xTickPos, canvas.height - (yPadding * 4) / 5) + c.stroke() + } + // Vertical grid line + c.moveTo(xTickPos, yPadding) + c.lineTo(xTickPos, canvas.height - yPadding) + c.stroke() + lastDate = xTickDate + } + } + } } diff --git a/Modules/energy/package/contents/ui/main.qml b/Modules/energy/package/contents/ui/main.qml --- a/Modules/energy/package/contents/ui/main.qml +++ b/Modules/energy/package/contents/ui/main.qml @@ -249,9 +249,22 @@ data: history.points + readonly property var xDivisionWidths: [1000 * 60 * 10, 1000 * 60 * 30, 1000 * 60 * 60, + 1000 * 60 * 60, 1000 * 60 * 60 * 2, 1000 * 60 * 60 * 24] + readonly property real xTicksAtDontCare: 0 + readonly property real xTicksAtTwelveOClock: 1 + readonly property real xTicksAtFullHour: 2 + readonly property real xTicksAtHalfHour: 3 + readonly property var xTicksAtList: [xTicksAtDontCare, xTicksAtHalfHour, xTicksAtHalfHour, + xTicksAtHalfHour, xTicksAtFullHour, xTicksAtTwelveOClock] + + xDivisionWidth: xDivisionWidths[timespanCombo.currentIndex] + xTicksAt: xTicksAtList[timespanCombo.currentIndex] + xMin: history.firstDataPointTime xMax: history.lastDataPointTime - + xDuration: history.duration + yUnits: root.historyType == HistoryModel.RateType ? i18nc("Shorthand for Watts","W") : i18nc("literal percent sign","%") yMax: { if (root.historyType == HistoryModel.RateType) {