Merge branch 'master' into file-size-reduction

This commit is contained in:
Evert Timberg 2016-05-08 08:11:16 -04:00
commit b35b246414
15 changed files with 17634 additions and 16723 deletions

View File

@ -16,7 +16,7 @@ To install via npm / bower:
npm install chart.js --save
bower install Chart.js --save
```
CDN: https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js
CDN: https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.1/Chart.min.js
## Documentation

View File

@ -1,6 +1,6 @@
{
"name": "Chart.js",
"version": "2.1.0",
"version": "2.1.1",
"description": "Simple HTML5 Charts using the canvas element",
"homepage": "https://github.com/chartjs/Chart.js",
"author": "nnnick",

17890
dist/Chart.bundle.js vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

16277
dist/Chart.js vendored

File diff suppressed because it is too large Load Diff

14
dist/Chart.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -299,3 +299,15 @@ The radial linear scale extends the core scale class with the following tick tem
},
}
```
### Update Default Scale config
The default configuration for a scale can be easily changed using the scale service. Pass in a partial configuration that will be merged with the current scale default configuration.
For example, to set the minimum value of 0 for all linear scales, you would do the following. Any linear scales created after this time would now have a minimum of 0.
```
Chart.scaleService.updateScaleDefaults('linear', {
ticks: {
min: 0
}
})
```

View File

@ -2,7 +2,7 @@
"name": "chart.js",
"homepage": "http://www.chartjs.org",
"description": "Simple HTML5 charts using the canvas element.",
"version": "2.1.0",
"version": "2.1.1",
"license": "MIT",
"main": "src/chart.js",
"repository": {

View File

@ -203,6 +203,7 @@ module.exports = function(Chart) {
backgroundColor: this.getPointBackgroundColor(point, index),
borderColor: this.getPointBorderColor(point, index),
borderWidth: this.getPointBorderWidth(point, index),
tension: meta.dataset._model ? meta.dataset._model.tension : 0,
// Tooltip
hitRadius: point.custom && point.custom.hitRadius ? point.custom.hitRadius : helpers.getValueAtIndexOrDefault(this.getDataset().pointHitRadius, index, this.chart.options.elements.point.hitRadius)
};

View File

@ -252,47 +252,56 @@ module.exports = function(Chart) {
helpers.callCallback(this.options.beforeFit, [this]);
},
fit: function() {
this.minSize = {
// Reset
var minSize = this.minSize = {
width: 0,
height: 0
};
var tickFontSize = helpers.getValueOrDefault(this.options.ticks.fontSize, Chart.defaults.global.defaultFontSize);
var tickFontStyle = helpers.getValueOrDefault(this.options.ticks.fontStyle, Chart.defaults.global.defaultFontStyle);
var tickFontFamily = helpers.getValueOrDefault(this.options.ticks.fontFamily, Chart.defaults.global.defaultFontFamily);
var opts = this.options;
var tickOpts = opts.ticks;
var scaleLabelOpts = opts.scaleLabel;
var globalOpts = Chart.defaults.global;
var display = opts.display;
var isHorizontal = this.isHorizontal();
var tickFontSize = helpers.getValueOrDefault(tickOpts.fontSize, globalOpts.defaultFontSize);
var tickFontStyle = helpers.getValueOrDefault(tickOpts.fontStyle, globalOpts.defaultFontStyle);
var tickFontFamily = helpers.getValueOrDefault(tickOpts.fontFamily, globalOpts.defaultFontFamily);
var tickLabelFont = helpers.fontString(tickFontSize, tickFontStyle, tickFontFamily);
var scaleLabelFontSize = helpers.getValueOrDefault(this.options.scaleLabel.fontSize, Chart.defaults.global.defaultFontSize);
var scaleLabelFontStyle = helpers.getValueOrDefault(this.options.scaleLabel.fontStyle, Chart.defaults.global.defaultFontStyle);
var scaleLabelFontFamily = helpers.getValueOrDefault(this.options.scaleLabel.fontFamily, Chart.defaults.global.defaultFontFamily);
var scaleLabelFontSize = helpers.getValueOrDefault(scaleLabelOpts.fontSize, globalOpts.defaultFontSize);
var scaleLabelFontStyle = helpers.getValueOrDefault(scaleLabelOpts.fontStyle, globalOpts.defaultFontStyle);
var scaleLabelFontFamily = helpers.getValueOrDefault(scaleLabelOpts.fontFamily, globalOpts.defaultFontFamily);
var scaleLabelFont = helpers.fontString(scaleLabelFontSize, scaleLabelFontStyle, scaleLabelFontFamily);
var tickMarkLength = opts.gridLines.tickMarkLength;
// Width
if (this.isHorizontal()) {
if (isHorizontal) {
// subtract the margins to line up with the chartArea if we are a full width scale
this.minSize.width = this.isFullWidth() ? this.maxWidth - this.margins.left - this.margins.right : this.maxWidth;
minSize.width = this.isFullWidth() ? this.maxWidth - this.margins.left - this.margins.right : this.maxWidth;
} else {
this.minSize.width = this.options.gridLines.tickMarkLength;
minSize.width = display ? tickMarkLength : 0;
}
// height
if (this.isHorizontal()) {
this.minSize.height = this.options.gridLines.tickMarkLength;
if (isHorizontal) {
minSize.height = display ? tickMarkLength : 0;
} else {
this.minSize.height = this.maxHeight; // fill all the height
minSize.height = this.maxHeight; // fill all the height
}
// Are we showing a title for the scale?
if (this.options.scaleLabel.display) {
if (this.isHorizontal()) {
this.minSize.height += (scaleLabelFontSize * 1.5);
if (scaleLabelOpts.display && display) {
if (isHorizontal) {
minSize.height += (scaleLabelFontSize * 1.5);
} else {
this.minSize.width += (scaleLabelFontSize * 1.5);
minSize.width += (scaleLabelFontSize * 1.5);
}
}
if (this.options.ticks.display && this.options.display) {
if (tickOpts.display && display) {
// Don't bother fitting the ticks if we are not showing them
if (!this.longestTextCache) {
this.longestTextCache = {};
@ -300,14 +309,14 @@ module.exports = function(Chart) {
var largestTextWidth = helpers.longestText(this.ctx, tickLabelFont, this.ticks, this.longestTextCache);
if (this.isHorizontal()) {
if (isHorizontal) {
// A horizontal axis is more constrained by the height.
this.longestLabelWidth = largestTextWidth;
// TODO - improve this calculation
var labelHeight = (Math.sin(helpers.toRadians(this.labelRotation)) * this.longestLabelWidth) + 1.5 * tickFontSize;
this.minSize.height = Math.min(this.maxHeight, this.minSize.height + labelHeight);
minSize.height = Math.min(this.maxHeight, minSize.height + labelHeight);
this.ctx.font = tickLabelFont;
var firstLabelWidth = this.ctx.measureText(this.ticks[0]).width;
@ -321,19 +330,23 @@ module.exports = function(Chart) {
this.paddingRight = this.labelRotation !== 0 ? (sinRotation * (tickFontSize / 2)) + 3 : lastLabelWidth / 2 + 3; // when rotated
} else {
// A vertical axis is more constrained by the width. Labels are the dominant factor here, so get that length first
var maxLabelWidth = this.maxWidth - this.minSize.width;
var maxLabelWidth = this.maxWidth - minSize.width;
// Account for padding
if (!this.options.ticks.mirror) {
var mirror = tickOpts.mirror;
if (!mirror) {
largestTextWidth += this.options.ticks.padding;
} else {
// If mirrored text is on the inside so don't expand
largestTextWidth = 0;
}
if (largestTextWidth < maxLabelWidth) {
// We don't need all the room
this.minSize.width += largestTextWidth;
minSize.width += largestTextWidth;
} else {
// Expand to max size
this.minSize.width = this.maxWidth;
minSize.width = this.maxWidth;
}
this.paddingTop = tickFontSize / 2;
@ -348,8 +361,8 @@ module.exports = function(Chart) {
this.paddingBottom = Math.max(this.paddingBottom - this.margins.bottom, 0);
}
this.width = this.minSize.width;
this.height = this.minSize.height;
this.width = minSize.width;
this.height = minSize.height;
},
afterFit: function() {

View File

@ -24,6 +24,12 @@ module.exports = function(Chart) {
// Return the scale defaults merged with the global settings so that we always use the latest ones
return this.defaults.hasOwnProperty(type) ? helpers.scaleMerge(Chart.defaults.scale, this.defaults[type]) : {};
},
updateScaleDefaults: function(type, additions) {
var defaults = this.defaults;
if (defaults.hasOwnProperty(type)) {
defaults[type] = helpers.extend(defaults[type], additions);
}
},
addScalesToLayout: function(chartInstance) {
// Adds each scale to the chart.boxes array to be sized accordingly
helpers.each(chartInstance.scales, function(scale) {

View File

@ -147,8 +147,11 @@ module.exports = function(Chart) {
}
if (this.min === this.max) {
this.min--;
this.max++;
if (!this.options.ticks.beginAtZero) {
this.min--;
}
}
},
buildTicks: function() {

View File

@ -307,6 +307,7 @@ module.exports = function(Chart) {
}
},
convertTicksToLabels: function() {
this.tickMoments = this.ticks;
this.ticks = this.ticks.map(this.tickFormatFunction, this);
},
getPixelForValue: function(value, index, datasetIndex, includeOffset) {
@ -332,6 +333,9 @@ module.exports = function(Chart) {
}
}
},
getPixelForTick: function(index, includeOffset) {
return this.getPixelForValue(this.tickMoments[index], null, null, includeOffset);
},
getValueForPixel: function(pixel) {
var innerDimension = this.isHorizontal() ? this.width - (this.paddingLeft + this.paddingRight) : this.height - (this.paddingTop + this.paddingBottom);
var offset = (pixel - (this.isHorizontal() ? this.left + this.paddingLeft : this.top + this.paddingTop)) / innerDimension;

View File

@ -0,0 +1,29 @@
// Tests of the scale service
describe('Test the scale service', function() {
it('should update scale defaults', function() {
var defaults = {
testProp: true
};
var type = 'my_test_type';
var Constructor = function() {
this.initialized = true;
};
Chart.scaleService.registerScaleType(type, Constructor, defaults);
// Should equal defaults but not be an identical object
expect(Chart.scaleService.getScaleDefaults(type)).toEqual(jasmine.objectContaining({
testProp: true
}));
Chart.scaleService.updateScaleDefaults(type, {
testProp: 'red',
newProp: 42
});
expect(Chart.scaleService.getScaleDefaults(type)).toEqual(jasmine.objectContaining({
testProp: 'red',
newProp: 42
}));
});
});

View File

@ -409,6 +409,31 @@ describe('Linear Scale', function() {
expect(chartInstance.scales.yScale0.max).toBe(1);
});
it('Should ensure that the scale has a max and min that are not equal when beginAtZero is set', function() {
chartInstance = window.acquireChart({
type: 'bar',
data: {
datasets: [],
labels: ['a', 'b', 'c', 'd', 'e', 'f']
},
options: {
scales: {
yAxes: [{
id: 'yScale0',
type: 'linear',
ticks: {
beginAtZero: true
}
}]
}
}
});
expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
expect(chartInstance.scales.yScale0.min).toBe(0);
expect(chartInstance.scales.yScale0.max).toBe(1);
});
it('Should use the suggestedMin and suggestedMax options', function() {
chartInstance = window.acquireChart({
type: 'bar',