Skip to content
Merged
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
5 changes: 3 additions & 2 deletions src/components/Chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,11 @@
ctx.fillStyle = color;
ctx.beginPath();
for (let i = 0; i < data.length; i++) {
const y = value2y(dataset.getPointValue(i));
if (isNaN(y)) {
const ptVal = dataset.getPointValue(i);
if (isNaN(ptVal) || ptVal == null) {
continue;
Comment on lines -722 to 724
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks like it was intended to prevent plotting null values in the data, but there were two major problems with the logic:

First, JavaScript's isNaN() doesnt quite determine if the object in question is a number or not; it determines if it is (or if it would be coerced to) the numeric value of NaN (a reserved word in the language). You might think that null is not a number, but when used in a numeric expression, null is "coerced" to the numeric value of zero. (Similarly, the string "1" is coerced to numeric 1 and string "0" to numeric 0. The string "xyz" would be coerced to NaN because it cannot be interpreted as a numeric value. For strange historical reasons and by convention, the empty string "" also becomes 0.) Thus, isNaN() is not sufficient to test for null, and that must be done explicitly.

isNaN(null)  == false;
isNaN(0)     == false;
isNaN("1")   == false;
isNaN("")    == false;
isNaN(NaN)   == true;
isNaN("xyz") == true;

Second, since null is coerced to 0 when used in a numeric expression, we must test a value for equality to null before it is run through such an expression... And it is transformed by arithmetic operations when passed to value2y(), so testing for null afterward is futile!

let a = null, b = null;
a += 0; // identity operation?
b *= 1; // identity operation?
console.log(a, b); // => "null null"

}
const y = value2y(ptVal);
date1 = date2;
date2 = data[i].getDate().getIndex();
if (date2 - date1 !== gap) {
Expand Down
2 changes: 1 addition & 1 deletion src/data/DataSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class DataSet {

getPointValue(index: number): number {
const temp = this.data[index].getValue();
if (Number.isNaN(temp)) {
if (Number.isNaN(temp) || temp == null) {
return temp;
}
return this.verticalOffset + temp * this.scale;
Expand Down