From cff44d7dd4e26981571bdb68302932f8f5d3e26c Mon Sep 17 00:00:00 2001 From: johnvillar Date: Wed, 24 Feb 2016 11:55:55 -0430 Subject: [PATCH 1/2] Added setLineDash support to series drawing Added support for 0 line width on series (they get skipped) Signed-off-by: johnvillar --- smoothie.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/smoothie.js b/smoothie.js index 27f5151..d1972f5 100644 --- a/smoothie.js +++ b/smoothie.js @@ -671,6 +671,11 @@ var timeSeries = this.seriesSet[d].timeSeries, dataSet = timeSeries.data, seriesOptions = this.seriesSet[d].options; + + if (seriesOptions.lineWidth === 0) { + context.restore(); + continue; + } // Delete old data that's moved off the left of the chart. timeSeries.dropOldData(oldestValidTime, chartOptions.maxDataSetLength); @@ -678,6 +683,9 @@ // Set style for this dataSet. context.lineWidth = seriesOptions.lineWidth; context.strokeStyle = seriesOptions.strokeStyle; + if (typeof(seriesOptions.lineDash) !== "undefined") { + context.setLineDash(seriesOptions.lineDash); + } // Draw the line... context.beginPath(); // Retain lastX, lastY for calculating the control points of bezier curves. From ed7378562acc8ced46317d37dc03141ae1e1460c Mon Sep 17 00:00:00 2001 From: johnvillar Date: Wed, 24 Feb 2016 17:25:44 -0430 Subject: [PATCH 2/2] Fixed lineWidth incorrectly stopping series drawing Added a way to append data to a series via object instead of positional parameters Added a provision to add metadata to series points Metadata can control interpolation from point to point Added "jump" interpolation that just moves to the series' point without drawing Added a way to add text to a series point Signed-off-by: johnvillar --- smoothie.js | 56 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/smoothie.js b/smoothie.js index d1972f5..bb8c8b6 100644 --- a/smoothie.js +++ b/smoothie.js @@ -172,6 +172,21 @@ * whether it is replaced, or the values summed (defaults to false.) */ TimeSeries.prototype.append = function(timestamp, value, sumRepeatedTimeStampValues) { + var metaData + if ((typeof(value) === "undefined") && + (typeof(sumRepeatedTimeStampValues) === "undefined") && + (typeof(timestamp) === "object")) { + var ob = timestamp; + timestamp = ob.timestamp; + value = ob.value; + sumRepeatedTimeStampValues = ob.sumRepeatedTimeStampValues; + + delete ob.timestamp; + delete ob.value; + delete ob.sumRepeatedTimeStampValues; + + metaData = ob + } // Rewind until we hit an older timestamp var i = this.data.length - 1; while (i >= 0 && this.data[i][0] > timestamp) { @@ -193,10 +208,10 @@ } } else if (i < this.data.length - 1) { // Splice into the correct position to keep timestamps in order - this.data.splice(i + 1, 0, [timestamp, value]); + this.data.splice(i + 1, 0, [timestamp, value, metaData]); } else { // Add to the end of the array - this.data.push([timestamp, value]); + this.data.push([timestamp, value, metaData]); } this.maxValue = isNaN(this.maxValue) ? value : Math.max(this.maxValue, value); @@ -672,11 +687,6 @@ dataSet = timeSeries.data, seriesOptions = this.seriesSet[d].options; - if (seriesOptions.lineWidth === 0) { - context.restore(); - continue; - } - // Delete old data that's moved off the left of the chart. timeSeries.dropOldData(oldestValidTime, chartOptions.maxDataSetLength); @@ -685,6 +695,8 @@ context.strokeStyle = seriesOptions.strokeStyle; if (typeof(seriesOptions.lineDash) !== "undefined") { context.setLineDash(seriesOptions.lineDash); + } else { + context.setLineDash([]); } // Draw the line... context.beginPath(); @@ -692,13 +704,32 @@ var firstX = 0, lastX = 0, lastY = 0; for (var i = 0; i < dataSet.length && dataSet.length !== 1; i++) { var x = timeToXPixel(dataSet[i][0]), - y = valueToYPixel(dataSet[i][1]); + y = valueToYPixel(dataSet[i][1]), + metaData = dataSet[i][2]; if (i === 0) { firstX = x; context.moveTo(x, y); } else { - switch (chartOptions.interpolation) { + var interpolation; + if (typeof(metaData) !== "undefined") { + interpolation = metaData.interpolation || chartOptions.interpolation + + if (typeof(metaData.text) !== "undefined") { + context.save(); + context.font = metaData.text.font; + context.strokeStyle = metaData.text.strokeStyle; + context.fillStyle = metaData.text.fillStyle; + var tx = x + (metaData.text.dx || 0), + ty = y + (metaData.text.dy || 0); + context.fillText(metaData.text.text, x, y); + context.restore(); + } + + } else { + interpolation = chartOptions.interpolation + } + switch (interpolation) { case "linear": case "line": { context.lineTo(x,y); @@ -731,6 +762,11 @@ context.lineTo(x,y); break; } + case "jump": { + // Only to be used to create gaps in the data + // Never use as interpolation method + context.moveTo(x, y); + } } } @@ -747,7 +783,7 @@ context.fill(); } - if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none') { + if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none' && (seriesOptions.lineWidth !== 0)) { context.stroke(); } context.closePath();