Skip to content

Commit

Permalink
Hide data points that are null (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
almossawi committed Jul 7, 2015
1 parent 36824e5 commit 2f78e4d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
35 changes: 23 additions & 12 deletions dist/metricsgraphics.js
Expand Up @@ -2445,17 +2445,24 @@ MG.button_layout = function(target) {
.y(args.scalefns.yf)
.interpolate(args.interpolate)
.tension(args.interpolate_tension);


//if missing_is_zero is not set, then hide data points that fall in missing
//data ranges or that have been explicitly identified as missing in the
//data source
if(!args.missing_is_zero) {
//a line is defined if the _missing attrib is not set to true
//and the y-accessor is not null
line = line.defined(function(d) {
return (d['_missing'] == undefined || d['_missing'] != true);
return (d['_missing'] == undefined || d['_missing'] != true)
&& d[args.y_accessor] != null;
})
}

//for animating line on first load
var flat_line = d3.svg.line()
.defined(function(d) {
return (d['_missing'] == undefined || d['_missing'] != true);
return (d['_missing'] == undefined || d['_missing'] != true)
&& d[args.y_accessor] != null;
})
.x(args.scalefns.xf)
.y(function() { return args.scales.Y(data_median); })
Expand Down Expand Up @@ -2955,20 +2962,20 @@ MG.button_layout = function(target) {
.style('opacity', 1);
}
});
} else if (args.missing_is_hidden
&& d['_missing']) {
} else if ((args.missing_is_hidden && d['_missing'])
|| d[args.y_accessor] == null
) {
//disable rollovers for hidden parts of the line
//recall that hidden parts are missing data ranges and possibly also
//data points that have been explicitly identified as missing
return;
} else {

//show circle on mouse-overed rect
if (d[args.x_accessor] >= args.processed.min_x &&
d[args.x_accessor] <= args.processed.max_x &&
d[args.y_accessor] >= args.processed.min_y &&
d[args.y_accessor] <= args.processed.max_y
){
) {
svg.selectAll('circle.mg-line-rollover-circle.mg-area' + d.line_id + '-color')
.attr('class', "")
.attr('class', 'mg-area' + d.line_id + '-color')
Expand Down Expand Up @@ -4539,7 +4546,7 @@ function process_line(args) {
var o = {};
d.setHours(0, 0, 0, 0);

//add the first date item (judge me not, world)
//add the first date item
//we'll be starting from the day after our first date
if (Date.parse(d) === Date.parse(new Date(start_date))) {
processed_data.push(MG.clone(args.data[i][0]));
Expand All @@ -4554,24 +4561,27 @@ function process_line(args) {
return false;
}
});

//if we don't have this date in our data object, add it and set it to zero
if (!existing_o) {
o[args.x_accessor] = new Date(d);
o[args.y_accessor] = 0;
o['_missing'] = true; //we want to distinguish between zero-value and missing observations
processed_data.push(o);
}
//if the data point has, say, a 'missing' attribute set, just set its
//y-value to 0 and identify it internally as missing
else if (existing_o[args.missing_is_hidden_accessor]) {
//if the data point has, say, a 'missing' attribute set or if its
//y-value is null identify it internally as missing
else if (existing_o[args.missing_is_hidden_accessor]
|| existing_o[args.y_accessor] == null
) {
existing_o['_missing'] = true;
processed_data.push(existing_o);
}
//otherwise, use the existing object for that date
else {
processed_data.push(existing_o);
}
}
}
}
else {
for (var j = 0; j < args.data[i].length; j += 1) {
Expand All @@ -4580,6 +4590,7 @@ function process_line(args) {
processed_data.push(o);
}
}

//update our date object
args.data[i] = processed_data;
}
Expand Down
4 changes: 2 additions & 2 deletions dist/metricsgraphics.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/charts/data.htm
Expand Up @@ -158,7 +158,7 @@
data = MG.convert.date(data, 'date');
MG.data_graphic({
title: "Broken Lines",
description: "Setting <i>missing_is_hidden</i> to true will hide missing ranges rather than considering them to be zeros or interpolating between the two points on either side.",
description: "Setting <i>missing_is_hidden</i> to true will hide missing ranges rather than considering them to be zeros or interpolating between the two points on either side. Furthermore, you can hide individual data points on a particular attribute by setting <i>missing_is_hidden_accessor</i>. Data points whose y-accessor values are null are also hidden.",
data: data,
missing_is_hidden: true,
width: 600,
Expand Down
2 changes: 1 addition & 1 deletion examples/data/missing-is-hidden.json
Expand Up @@ -22,7 +22,7 @@
},
{
"date": "2014-02-14",
"value": 120
"value": null
},
{
"date": "2014-02-15",
Expand Down
16 changes: 10 additions & 6 deletions src/js/charts/line.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/js/misc/process.js
Expand Up @@ -121,7 +121,7 @@ function process_line(args) {
var o = {};
d.setHours(0, 0, 0, 0);

//add the first date item (judge me not, world)
//add the first date item
//we'll be starting from the day after our first date
if (Date.parse(d) === Date.parse(new Date(start_date))) {
processed_data.push(MG.clone(args.data[i][0]));
Expand All @@ -136,24 +136,27 @@ function process_line(args) {
return false;
}
});

//if we don't have this date in our data object, add it and set it to zero
if (!existing_o) {
o[args.x_accessor] = new Date(d);
o[args.y_accessor] = 0;
o['_missing'] = true; //we want to distinguish between zero-value and missing observations
processed_data.push(o);
}
//if the data point has, say, a 'missing' attribute set, just set its
//y-value to 0 and identify it internally as missing
else if (existing_o[args.missing_is_hidden_accessor]) {
//if the data point has, say, a 'missing' attribute set or if its
//y-value is null identify it internally as missing
else if (existing_o[args.missing_is_hidden_accessor]
|| existing_o[args.y_accessor] == null
) {
existing_o['_missing'] = true;
processed_data.push(existing_o);
}
//otherwise, use the existing object for that date
else {
processed_data.push(existing_o);
}
}
}
}
else {
for (var j = 0; j < args.data[i].length; j += 1) {
Expand All @@ -162,6 +165,7 @@ function process_line(args) {
processed_data.push(o);
}
}

//update our date object
args.data[i] = processed_data;
}
Expand Down

0 comments on commit 2f78e4d

Please sign in to comment.