Skip to content

Commit

Permalink
Fix #289 - error when missing_is_zero is set and data length is 1
Browse files Browse the repository at this point in the history
  • Loading branch information
almossawi committed Jan 20, 2015
1 parent 3d7b749 commit 35f6ecf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules
other/divider.psd

other/htaccess.txt

.DS_Store
21 changes: 15 additions & 6 deletions dist/metricsgraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,12 @@
if (!args) { args = {}; }
args = merge_with_defaults(args, defaults);

//this is how we're dealing with passing in a single array of data,
if (d3.select(args.target).empty()) {
console.warn('The specified target element "' + args.target + '" could not be found in the page. The chart will not be rendered.');
return;
}

//this is how we're dealing with passing in a single array of data,
//but with the intention of using multiple values for multilines, etc.

//do we have a time_series?
Expand Down Expand Up @@ -1854,16 +1859,16 @@
chart_title(args);

//draw axes
args.use_small_class = args.height - args.top - args.bottom - args.buffer
<= args.small_height_threshold && args.width - args.left-args.right - args.buffer * 2
args.use_small_class = args.height - args.top - args.bottom - args.buffer
<= args.small_height_threshold && args.width - args.left-args.right - args.buffer * 2
<= args.small_width_threshold || args.small_text;

//if we're updating an existing chart and we have fewer lines than
//before, remove the outdated lines, e.g. if we had 3 lines, and we're calling
//data_graphic() on the same target with 2 lines, remove the 3rd line
if(args.data.length < $(args.target).find('svg .mg-main-line').length) {
//now, the thing is we can't just remove, say, line3 if we have a custom
//line-color map, instead, see which are the lines to be removed, and delete those
//line-color map, instead, see which are the lines to be removed, and delete those
if(args.custom_line_color_map.length > 0) {
var array_full_series = function(len) {
var arr = new Array(len);
Expand All @@ -1873,7 +1878,7 @@

//get an array of lines ids to remove
var lines_to_remove = arrDiff(
array_full_series(args.max_data_size),
array_full_series(args.max_data_size),
args.custom_line_color_map);

for(var i=0; i<lines_to_remove.length; i++) {
Expand Down Expand Up @@ -4116,7 +4121,11 @@
: false;

//are we replacing missing y values with zeros?
if (args.missing_is_zero && args.chart_type === 'line' && is_time_series) {
if (args.missing_is_zero
&& args.chart_type === 'line'
&& is_time_series
&& args.data.length > 1
) {
for (var i = 0;i < args.data.length; i++) {
var first = args.data[i][0];
var last = args.data[i][args.data[i].length-1];
Expand Down
6 changes: 3 additions & 3 deletions dist/metricsgraphics.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/js/misc/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ function process_line(args) {
: false;

//are we replacing missing y values with zeros?
if (args.missing_is_zero && args.chart_type === 'line' && is_time_series) {
if (args.missing_is_zero
&& args.chart_type === 'line'
&& is_time_series
&& args.data.length > 1
) {
for (var i = 0;i < args.data.length; i++) {
var first = args.data[i][0];
var last = args.data[i][args.data[i].length-1];
Expand Down
14 changes: 14 additions & 0 deletions tests/misc/process_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module('process');

test('args.missing_is_zero doesn\'t throw a "args.data[0][0] is undefined" error', function() {
var data = [{"date": new Date('2014-02-02'), "value": 6}];
var params = {
data: data,
target: "#qunit-fixture",
missing_is_zero: true
};

MG.data_graphic(params);

equal(params.data.length, 1, 'args.data is defined');
});

0 comments on commit 35f6ecf

Please sign in to comment.