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,6 +50,11 @@ property string yUnits: "" property string xUnits: "" + property real xDuration: 0 + property real xDivisions: 0 + property real xDivisionWidth: 0 + property real xTicksAt: Graph.dontCare + //internal property real plotWidth: width - xPadding @@ -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.5*xPadding) && + (xTickPos > 0.5*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,20 @@ 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) {