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

Added events to smoothie charts #116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion smoothie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export declare class TimeSeries {
* @param sumRepeatedTimeStampValues if <code>timestamp</code> has an exact match in the series, this flag controls
* whether it is replaced, or the values summed (defaults to false.)
*/
append(timestamp: number, value: number, sumRepeatedTimeStampValues?: boolean): void;
append(timestamp: number, value: number | string, sumRepeatedTimeStampValues?: boolean): void;

dropOldData(oldestValidTime: number, maxDataSetLength: number): void;
}
Expand Down
88 changes: 50 additions & 38 deletions smoothie.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
*/
TimeSeries.prototype.append = function(timestamp, value, sumRepeatedTimeStampValues) {
// Reject NaN
if (isNaN(timestamp) || isNaN(value)){
if (isNaN(timestamp) || (typeof value !== 'string' && isNaN(value))){
return
}
// Rewind until we hit an older timestamp
Expand Down Expand Up @@ -558,7 +558,7 @@

SmoothieChart.prototype.updateTooltip = function () {
if(!this.options.tooltip){
return;
return;
}
var el = this.getTooltipEl();

Expand Down Expand Up @@ -605,7 +605,7 @@
this.mousePageX = evt.pageX;
this.mousePageY = evt.pageY;
if(!this.options.tooltip){
return;
return;
}
var el = this.getTooltipEl();
el.style.top = Math.round(this.mousePageY) + 'px';
Expand Down Expand Up @@ -933,41 +933,54 @@

if (i === 0) {
firstX = x;
firstY = y;
context.moveTo(x, y);
if (typeof dataSet[i][1] === 'string' ) {
context.moveTo(x, 0);
context.lineTo(x, dimensions.height);
context.fillStyle = seriesOptions.strokeStyle;
context.fillText(dataSet[i][1], x + 5, 10);
} else {
context.moveTo(x, y);
}
Comment on lines +936 to +943
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than do this type check on every point on every series, it'd be clearer and faster to create a new series type for these events.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the function be exactly the same ? I just think it doubles the code unnecessarily

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final outcome would be the same, but it's a lot more work to test the type on every single point on all series. Smoothie is light and fast. Currently this cost would be paid by everyone, regardless of whether they use these events. Using a new SmoothieEventSeries type avoids that cost.

} else {
switch (chartOptions.interpolation) {
case "linear":
case "line": {
context.lineTo(x,y);
break;
}
case "bezier":
default: {
// Great explanation of Bezier curves: http://en.wikipedia.org/wiki/Bezier_curve#Quadratic_curves
//
// Assuming A was the last point in the line plotted and B is the new point,
// we draw a curve with control points P and Q as below.
//
// A---P
// |
// |
// |
// Q---B
//
// Importantly, A and P are at the same y coordinate, as are B and Q. This is
// so adjacent curves appear to flow as one.
//
context.bezierCurveTo( // startPoint (A) is implicit from last iteration of loop
Math.round((lastX + x) / 2), lastY, // controlPoint1 (P)
Math.round((lastX + x)) / 2, y, // controlPoint2 (Q)
x, y); // endPoint (B)
break;
}
case "step": {
context.lineTo(x,lastY);
context.lineTo(x,y);
break;
if (typeof dataSet[i][1] === 'string') {
context.moveTo(x, 0);
context.lineTo(x, dimensions.height);
context.fillStyle = seriesOptions.strokeStyle;
context.fillText(dataSet[i][1], x + 5, 10);
} else {
switch (chartOptions.interpolation) {
case "linear":
case "line": {
context.lineTo(x,y);
break;
}
case "bezier":
default: {
// Great explanation of Bezier curves: http://en.wikipedia.org/wiki/Bezier_curve#Quadratic_curves
//
// Assuming A was the last point in the line plotted and B is the new point,
// we draw a curve with control points P and Q as below.
//
// A---P
// |
// |
// |
// Q---B
//
// Importantly, A and P are at the same y coordinate, as are B and Q. This is
// so adjacent curves appear to flow as one.
//
context.bezierCurveTo( // startPoint (A) is implicit from last iteration of loop
Math.round((lastX + x) / 2), lastY, // controlPoint1 (P)
Math.round((lastX + x)) / 2, y, // controlPoint2 (Q)
x, y); // endPoint (B)
break;
}
case "step": {
context.lineTo(x,lastY);
context.lineTo(x,y);
break;
}
}
}
}
Expand Down Expand Up @@ -1107,4 +1120,3 @@
exports.SmoothieChart = SmoothieChart;

})(typeof exports === 'undefined' ? this : exports);