Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LineDash and 0-width lines #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 50 additions & 6 deletions smoothie.js
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -671,26 +686,50 @@
var timeSeries = this.seriesSet[d].timeSeries,
dataSet = timeSeries.data,
seriesOptions = this.seriesSet[d].options;

// Delete old data that's moved off the left of the chart.
timeSeries.dropOldData(oldestValidTime, chartOptions.maxDataSetLength);

// Set style for this dataSet.
context.lineWidth = seriesOptions.lineWidth;
context.strokeStyle = seriesOptions.strokeStyle;
if (typeof(seriesOptions.lineDash) !== "undefined") {
context.setLineDash(seriesOptions.lineDash);
} else {
context.setLineDash([]);
}
// Draw the line...
context.beginPath();
// Retain lastX, lastY for calculating the control points of bezier curves.
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);
Expand Down Expand Up @@ -723,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);
}
}
}

Expand All @@ -739,7 +783,7 @@
context.fill();
}

if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none') {
if (seriesOptions.strokeStyle && seriesOptions.strokeStyle !== 'none' && (seriesOptions.lineWidth !== 0)) {
context.stroke();
}
context.closePath();
Expand Down