Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better number -> string callback for the radial linear scale #3281

Merged
merged 6 commits into from
Sep 24, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions src/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require('./core/core.datasetController')(Chart);
require('./core/core.layoutService')(Chart);
require('./core/core.scaleService')(Chart);
require('./core/core.plugin.js')(Chart);
require('./core/core.ticks.js')(Chart);
require('./core/core.scale')(Chart);
require('./core/core.title')(Chart);
require('./core/core.legend')(Chart);
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function(Chart) {
labelOffset: 0,
// We pass through arrays to be rendered as multiline labels, we convert Others to strings here.
callback: function(value) {
return helpers.isArray(value) ? value : '' + value;
return Chart.Ticks.formatters.values(value);
Copy link
Member

Choose a reason for hiding this comment

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

I think we can save some bits and a function call: callback: Chart.Ticks.formatters.values

}
}
};
Expand Down
73 changes: 73 additions & 0 deletions src/core/core.ticks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

module.exports = function(Chart) {

var helpers = Chart.helpers;

/**
* Namespace to hold static tick generation functions
* @namespace Chart.Ticks
*/
Chart.Ticks = {
/**
* Namespace to hold formatters for different types of ticks
* @namespace Chart.Ticks.formatters
*/
formatters: {
/**
* Formatter for value labels
* @method Chart.Ticks.formatters.values
* @param value the value to display
* @return {String|Array} the label to display
*/
values: function(value) {
return helpers.isArray(value) ? value : '' + value;
},

/**
* Formatter for linear numeric ticks
* @method Chart.Ticks.formatters.linear
* @param tickValue {Number} the value to be formatted
* @param index {Number} the position of the tickValue parameter in the ticks array
* @param ticks {Array<Number>} the list of ticks being converted
* @return {String} string representation of the tickValue parameter
*/
linear: function(tickValue, index, ticks) {
// If we have lots of ticks, don't use the ones
var delta = ticks.length > 3 ? ticks[2] - ticks[1] : ticks[1] - ticks[0];

// If we have a number like 2.5 as the delta, figure out how many decimal places we need
if (Math.abs(delta) > 1) {
if (tickValue !== Math.floor(tickValue)) {
// not an integer
delta = tickValue - Math.floor(tickValue);
}
}

var logDelta = helpers.log10(Math.abs(delta));
var tickString = '';

if (tickValue !== 0) {
var numDecimal = -1 * Math.floor(logDelta);
numDecimal = Math.max(Math.min(numDecimal, 20), 0); // toFixed has a max of 20 decimal places
tickString = tickValue.toFixed(numDecimal);
} else {
tickString = '0'; // never show decimal places for 0
}

return tickString;
},

logarithmic: function(tickValue, index, ticks) {
var remain = tickValue / (Math.pow(10, Math.floor(helpers.log10(tickValue))));

if (tickValue === 0) {
return '0';
} else if (remain === 1 || remain === 2 || remain === 5 || index === 0 || index === ticks.length - 1) {
return tickValue.toExponential();
}
return '';
}
}
};
};
24 changes: 1 addition & 23 deletions src/scales/scale.linear.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,7 @@ module.exports = function(Chart) {
position: 'left',
ticks: {
callback: function(tickValue, index, ticks) {
// If we have lots of ticks, don't use the ones
var delta = ticks.length > 3 ? ticks[2] - ticks[1] : ticks[1] - ticks[0];

// If we have a number like 2.5 as the delta, figure out how many decimal places we need
if (Math.abs(delta) > 1) {
if (tickValue !== Math.floor(tickValue)) {
// not an integer
delta = tickValue - Math.floor(tickValue);
}
}

var logDelta = helpers.log10(Math.abs(delta));
var tickString = '';

if (tickValue !== 0) {
var numDecimal = -1 * Math.floor(logDelta);
numDecimal = Math.max(Math.min(numDecimal, 20), 0); // toFixed has a max of 20 decimal places
tickString = tickValue.toFixed(numDecimal);
} else {
tickString = '0'; // never show decimal places for 0
}

return tickString;
return Chart.Ticks.formatters.linear(tickValue, index, ticks);
Copy link
Member

Choose a reason for hiding this comment

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

Same here: callback: Chart.Ticks.formatters.linear

}
}
};
Expand Down
9 changes: 1 addition & 8 deletions src/scales/scale.logarithmic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ module.exports = function(Chart) {
// label settings
ticks: {
callback: function(value, index, arr) {
var remain = value / (Math.pow(10, Math.floor(helpers.log10(value))));

if (value === 0) {
return '0';
} else if (remain === 1 || remain === 2 || remain === 5 || index === 0 || index === arr.length - 1) {
return value.toExponential();
}
return '';
return Chart.Ticks.formatters.logarithmic(value, index, arr);
Copy link
Member

Choose a reason for hiding this comment

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

callback: Chart.Ticks.formatters.logarithmic

}
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ module.exports = function(Chart) {
backdropPaddingY: 2,

// Number - The backdrop padding to the side of the label in pixels
backdropPaddingX: 2
backdropPaddingX: 2,

callback: function(tickValue, index, ticks) {
return Chart.Ticks.formatters.linear(tickValue, index, ticks);
Copy link
Member

Choose a reason for hiding this comment

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

Same

}
},

pointLabels: {
Expand Down