Skip to content

Commit

Permalink
Cleanup (w3c#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
almossawi committed Jul 28, 2016
1 parent 5d040bb commit da4d9ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/js/charts/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
.numericalRange('bottom');

var baselines = (args.baselines || []).map(function(d) {
return d[args.y_accessor] });
return d[args.y_accessor]
});

new MG.scale_factory(args)
.namespace('y')
.zeroBottom(true)
.inflateDomain(true)
.numericalDomainFromData(baselines)
.numericalRange('left');

x_axis(args);
y_axis(args);

Expand Down
23 changes: 13 additions & 10 deletions src/js/common/x_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function mg_add_processed_object(args) {
}
}

// seems to be deprecated, only used by bar and histogram
// ought to be deprecated, only used by bar and histogram
function x_axis(args) {
'use strict';

Expand Down Expand Up @@ -308,24 +308,27 @@ function mg_default_xax_format(args) {
if (args.xax_format) {
return args.xax_format;
}

var data = args.processed.original_data || args.data;
var flattened = mg_flatten_array(data)[0];
var test_point_x = flattened[args.processed.original_x_accessor || args.x_accessor];
var test_point_y = flattened[args.processed.original_y_accessor || args.y_accessor];
var test_point_x = flattened[args.processed.original_x_accessor || args.x_accessor] || flattened;

return function(d) {
mg_process_time_format(args);

if (test_point_x instanceof Date) {
return args.processed.main_x_time_format(new Date(d));
} else if (test_point_y instanceof Number) {
if (d < 1000) {
var pf = d3.format(',.0f');
return args.xax_units + pf(d);
} else if (typeof test_point_x === 'number') {
var pf;
if (d < 1.0 && d > 0) {
// don't scale tiny values
pf = d3.format(',.' + args.decimals + 'f');
} else if (d < 1000) {
pf = d3.format(',.0f');
} else {
var pf = d3.format(',.0s');
return args.xax_units + pf(d);
pf = d3.format(',.0s');
}
return args.xax_units + pf(d);
} else {
return args.xax_units + d;
}
Expand Down Expand Up @@ -355,7 +358,7 @@ function mg_add_x_axis_rim(args, g) {
})
.attr('x2', function() {
if (args.xax_count === 0 || (args.axes_not_compact && args.chart_type !== 'bar')) {
return mg_get_plot_right(args);
return mg_get_right(args);
} else {
return args.scales.X(args.scales.X.ticks(args.xax_count)[last_i]).toFixed(2);
}
Expand Down
28 changes: 19 additions & 9 deletions src/js/common/y_axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function rimPlacement (args, axisArgs) {
} else if (tick_length) {
coordinates.y1 = scale(ticks[0]).toFixed(2);
coordinates.y2 = scale(ticks[tick_length - 1]).toFixed(2);
}
}
//else {
// coordinates.y1 = 0;
// coordinates.y2 = 0;
Expand All @@ -139,12 +139,14 @@ function labelPlacement (args, axisArgs) {
coordinates.dy = '.35em';
coordinates.textAnchor = 'end';
coordinates.text = function (d) {
return mg_compute_yax_format(args)(d); };
return mg_compute_yax_format(args)(d);
};
}
if (position === 'right') {
coordinates.x = mg_get_right(args) + tickLength * 3 / 2;
coordinates.y = function (d) {
return scale(d).toFixed(2); };
return scale(d).toFixed(2);
};
coordinates.dx = 3;
coordinates.dy = '.35em';
coordinates.textAnchor = 'start';
Expand All @@ -153,13 +155,15 @@ function labelPlacement (args, axisArgs) {
}
if (position === 'top') {
coordinates.x = function (d) {
return scale(d).toFixed(2); };
return scale(d).toFixed(2);
};
coordinates.y = (mg_get_top(args) - tickLength * 7 / 3).toFixed(2);
coordinates.dx = 0;
coordinates.dy = '0em';
coordinates.textAnchor = 'middle';
coordinates.text = function (d) {
return mg_default_xax_format(args)(d); };
return mg_default_xax_format(args)(d);
};
}
if (position === 'bottom') {
coordinates.x = function (d) {
Expand All @@ -169,7 +173,8 @@ function labelPlacement (args, axisArgs) {
coordinates.dy = '.50em';
coordinates.textAnchor = 'middle';
coordinates.text = function (d) {
return mg_default_xax_format(args)(d); };
return mg_default_xax_format(args)(d);
};
}

return coordinates;
Expand Down Expand Up @@ -796,10 +801,15 @@ function mg_compute_yax_format (args) {
}

yax_format = function (f) {
if (f < 1000) {
var pf = d3.format(',.0f');
var pf;

if (f < 1.0 && f > 0) {
// don't scale tiny values
pf = d3.format(',.' + args.decimals + 'f');
} else if (f < 1000) {
pf = d3.format(',.0f');
} else {
var pf = d3.format(',.0s');
pf = d3.format(',.0s');
}

// are we adding units after the value or before?
Expand Down

0 comments on commit da4d9ba

Please sign in to comment.