Skip to content

Commit

Permalink
Merge pull request speced#509 from pcl/master
Browse files Browse the repository at this point in the history
Fix for speced#505 (histograms and dates)
  • Loading branch information
hamilton committed Oct 13, 2015
2 parents 4fb6a1e + a9008be commit 959b519
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
44 changes: 34 additions & 10 deletions dist/metricsgraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,10 @@ function x_axis(args) {
var min_x;
var max_x;

args.processed = {};
if (!args.processed) {
args.processed = {};
}

var all_data = [];
for (var i = 0; i < args.data.length; i++) {
for (var j = 0; j < args.data[i].length; j++) {
Expand Down Expand Up @@ -1528,7 +1531,9 @@ function mg_default_bar_xax_format(args) {

function mg_get_time_frame(diff){
// diff should be (max_x - min_x) / 1000, in other words, the difference in seconds.
if (diff < 60) {
if (diff < 10) {
time_frame = 'millis'
} else if (diff < 60) {
time_frame = 'seconds';
} else if (diff / (60 * 60) <= 24) {
time_frame = 'less-than-a-day';
Expand All @@ -1541,7 +1546,9 @@ function mg_get_time_frame(diff){
}

function mg_get_time_format(utc, diff){
if (diff < 60) {
if (diff < 10) {
main_time_format = MG.time_format(utc, '%M:%S.%L');
} else if (diff < 60) {
main_time_format = MG.time_format(utc, '%M:%S');
} else if (diff / (60 * 60) <= 24) {
main_time_format = MG.time_format(utc, '%H:%M');
Expand All @@ -1557,7 +1564,8 @@ function mg_default_xax_format(args) {
if (args.xax_format) {
return args.xax_format;
}
var test_point = mg_flatten_array(args.data)[0][args.x_accessor]
var data = args.processed.original_data || args.data;
var test_point = mg_flatten_array(data)[0][args.processed.original_x_accessor || args.x_accessor]

return function(d) {
var diff;
Expand Down Expand Up @@ -1674,6 +1682,7 @@ function mg_add_x_tick_labels(g, args) {
var time_frame = args.processed.x_time_frame;

switch(time_frame) {
case 'millis':
case 'seconds':
secondary_function = d3.time.days;
yformat = MG.time_format(args.utc_time, '%I %p');
Expand Down Expand Up @@ -1836,6 +1845,11 @@ function init(args) {

args = arguments[0];
if (!args) { args = {}; }

if (!args.processed) {
args.processed = {};
}

args = merge_with_defaults(args, defaults);
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.');
Expand All @@ -1850,13 +1864,13 @@ function init(args) {

//do we have a time_series?

function is_time_series(args) {
function is_time_series() {
var flat_data = [];
var first_elem = mg_flatten_array(args.data)[0];
return first_elem[args.x_accessor] instanceof Date;
var first_elem = mg_flatten_array(args.processed.original_data || args.data)[0];
return first_elem[args.processed.original_x_accessor || args.x_accessor] instanceof Date;
}

args.time_series = is_time_series(args);
args.time_series = is_time_series();

var svg_width = args.width;
var svg_height = args.height;
Expand Down Expand Up @@ -3010,6 +3024,9 @@ MG.button_layout = function(target) {
var svg = mg_get_svg_child_of(args.target);
var fmt;
switch(args.processed.x_time_frame) {
case 'millis':
fmt = MG.time_format(args.utc_time, '%b %e, %Y %H:%M:%S.%L');
break;
case 'seconds':
fmt = MG.time_format(args.utc_time, '%b %e, %Y %H:%M:%S');
break;
Expand Down Expand Up @@ -3409,7 +3426,6 @@ MG.button_layout = function(target) {

this.rolloverOn = function(args) {
var svg = mg_get_svg_child_of(args.target);
var x_formatter = MG.time_format(args.utc_time, '%Y-%m-%d');

return function(d, i) {
svg.selectAll('text')
Expand All @@ -3418,7 +3434,7 @@ MG.button_layout = function(target) {
})
.attr('opacity', 0.3);

var fmt = MG.time_format(args.utc_time, '%b %e, %Y');
var fmt = args.processed.xax_format || MG.time_format(args.utc_time, '%b %e, %Y');
var num = format_rollover_number(args);

svg.selectAll('.mg-bar rect')
Expand Down Expand Up @@ -4761,6 +4777,14 @@ function process_histogram(args) {
}
}
}

// capture the original data and accessors before replacing args.data
if (!args.processed) {
args.processed = {};
}
args.processed.original_data = args.data;
args.processed.original_x_accessor = args.x_accessor;
args.processed.original_y_accessor = args.y_accessor;

args.data = [args.processed_data];
args.x_accessor = args.processed_x_accessor;
Expand Down
6 changes: 3 additions & 3 deletions dist/metricsgraphics.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/js/charts/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@

this.rolloverOn = function(args) {
var svg = mg_get_svg_child_of(args.target);
var x_formatter = MG.time_format(args.utc_time, '%Y-%m-%d');

return function(d, i) {
svg.selectAll('text')
Expand All @@ -139,7 +138,7 @@
})
.attr('opacity', 0.3);

var fmt = MG.time_format(args.utc_time, '%b %e, %Y');
var fmt = args.processed.xax_format || MG.time_format(args.utc_time, '%b %e, %Y');
var num = format_rollover_number(args);

svg.selectAll('.mg-bar rect')
Expand Down
3 changes: 3 additions & 0 deletions src/js/charts/line.js

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

13 changes: 9 additions & 4 deletions src/js/common/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ function init(args) {

args = arguments[0];
if (!args) { args = {}; }

if (!args.processed) {
args.processed = {};
}

args = merge_with_defaults(args, defaults);
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.');
Expand All @@ -25,13 +30,13 @@ function init(args) {

//do we have a time_series?

function is_time_series(args) {
function is_time_series() {
var flat_data = [];
var first_elem = mg_flatten_array(args.data)[0];
return first_elem[args.x_accessor] instanceof Date;
var first_elem = mg_flatten_array(args.processed.original_data || args.data)[0];
return first_elem[args.processed.original_x_accessor || args.x_accessor] instanceof Date;
}

args.time_series = is_time_series(args);
args.time_series = is_time_series();

var svg_width = args.width;
var svg_height = args.height;
Expand Down
17 changes: 13 additions & 4 deletions src/js/common/x_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ function x_axis(args) {
var min_x;
var max_x;

args.processed = {};
if (!args.processed) {
args.processed = {};
}

var all_data = [];
for (var i = 0; i < args.data.length; i++) {
for (var j = 0; j < args.data[i].length; j++) {
Expand Down Expand Up @@ -295,7 +298,9 @@ function mg_default_bar_xax_format(args) {

function mg_get_time_frame(diff){
// diff should be (max_x - min_x) / 1000, in other words, the difference in seconds.
if (diff < 60) {
if (diff < 10) {
time_frame = 'millis'
} else if (diff < 60) {
time_frame = 'seconds';
} else if (diff / (60 * 60) <= 24) {
time_frame = 'less-than-a-day';
Expand All @@ -308,7 +313,9 @@ function mg_get_time_frame(diff){
}

function mg_get_time_format(utc, diff){
if (diff < 60) {
if (diff < 10) {
main_time_format = MG.time_format(utc, '%M:%S.%L');
} else if (diff < 60) {
main_time_format = MG.time_format(utc, '%M:%S');
} else if (diff / (60 * 60) <= 24) {
main_time_format = MG.time_format(utc, '%H:%M');
Expand All @@ -324,7 +331,8 @@ function mg_default_xax_format(args) {
if (args.xax_format) {
return args.xax_format;
}
var test_point = mg_flatten_array(args.data)[0][args.x_accessor]
var data = args.processed.original_data || args.data;
var test_point = mg_flatten_array(data)[0][args.processed.original_x_accessor || args.x_accessor]

return function(d) {
var diff;
Expand Down Expand Up @@ -441,6 +449,7 @@ function mg_add_x_tick_labels(g, args) {
var time_frame = args.processed.x_time_frame;

switch(time_frame) {
case 'millis':
case 'seconds':
secondary_function = d3.time.days;
yformat = MG.time_format(args.utc_time, '%I %p');
Expand Down
8 changes: 8 additions & 0 deletions src/js/misc/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ function process_histogram(args) {
}
}
}

// capture the original data and accessors before replacing args.data
if (!args.processed) {
args.processed = {};
}
args.processed.original_data = args.data;
args.processed.original_x_accessor = args.x_accessor;
args.processed.original_y_accessor = args.y_accessor;

args.data = [args.processed_data];
args.x_accessor = args.processed_x_accessor;
Expand Down

0 comments on commit 959b519

Please sign in to comment.