2015-05-17 01:40:52 +02:00
|
|
|
(function() {
|
2015-06-13 16:15:21 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var root = this,
|
|
|
|
Chart = root.Chart,
|
|
|
|
helpers = Chart.helpers;
|
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
Chart.defaults.scale = {
|
|
|
|
display: true,
|
|
|
|
|
|
|
|
// grid line settings
|
|
|
|
gridLines: {
|
2015-11-25 01:18:15 +01:00
|
|
|
display: true,
|
2015-09-23 03:31:26 +02:00
|
|
|
color: "rgba(0, 0, 0, 0.1)",
|
|
|
|
lineWidth: 1,
|
|
|
|
drawOnChartArea: true,
|
|
|
|
drawTicks: true,
|
|
|
|
zeroLineWidth: 1,
|
|
|
|
zeroLineColor: "rgba(0,0,0,0.25)",
|
|
|
|
offsetGridLines: false,
|
2015-06-13 16:15:21 +02:00
|
|
|
},
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-09-24 23:45:56 +02:00
|
|
|
// scale label
|
|
|
|
scaleLabel: {
|
2016-02-02 03:06:54 +01:00
|
|
|
fontColor: Chart.defaults.global.defaultFontColor,
|
|
|
|
fontFamily: Chart.defaults.global.defaultFontFamily,
|
|
|
|
fontSize: Chart.defaults.global.defaultFontSize,
|
|
|
|
fontStyle: Chart.defaults.global.defaultFontStyle,
|
2015-09-24 23:45:56 +02:00
|
|
|
|
|
|
|
// actual label
|
|
|
|
labelString: '',
|
|
|
|
|
|
|
|
// display property
|
2015-11-25 01:18:15 +01:00
|
|
|
display: false,
|
2015-09-24 23:45:56 +02:00
|
|
|
},
|
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
// label settings
|
|
|
|
ticks: {
|
2015-09-26 03:04:45 +02:00
|
|
|
beginAtZero: false,
|
2016-02-02 03:06:54 +01:00
|
|
|
fontSize: Chart.defaults.global.defaultFontSize,
|
|
|
|
fontStyle: Chart.defaults.global.defaultFontStyle,
|
|
|
|
fontColor: Chart.defaults.global.defaultFontColor,
|
|
|
|
fontFamily: Chart.defaults.global.defaultFontFamily,
|
2015-09-26 03:04:45 +02:00
|
|
|
maxRotation: 90,
|
|
|
|
mirror: false,
|
|
|
|
padding: 10,
|
|
|
|
reverse: false,
|
2015-11-25 01:18:15 +01:00
|
|
|
display: true,
|
2015-12-18 16:18:49 +01:00
|
|
|
autoSkip: true,
|
2015-12-21 23:37:09 +01:00
|
|
|
autoSkipPadding: 20,
|
2015-10-07 04:40:25 +02:00
|
|
|
callback: function(value) {
|
2015-10-19 01:34:56 +02:00
|
|
|
return '' + value;
|
2015-10-07 04:40:25 +02:00
|
|
|
},
|
2015-06-13 16:15:21 +02:00
|
|
|
},
|
2015-09-23 03:31:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Chart.Scale = Chart.Element.extend({
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
// These methods are ordered by lifecyle. Utilities then follow.
|
|
|
|
// Any function defined here is inherited by all scale types.
|
|
|
|
// Any function can be extended by the scale type
|
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
beforeUpdate: function() {
|
|
|
|
helpers.callCallback(this.options.beforeUpdate, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
update: function(maxWidth, maxHeight, margins) {
|
|
|
|
|
|
|
|
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
|
|
|
|
this.beforeUpdate();
|
|
|
|
|
|
|
|
// Absorb the master measurements
|
|
|
|
this.maxWidth = maxWidth;
|
|
|
|
this.maxHeight = maxHeight;
|
2015-10-27 09:33:19 +01:00
|
|
|
this.margins = helpers.extend({
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
bottom: 0
|
|
|
|
}, margins);
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
// Dimensions
|
|
|
|
this.beforeSetDimensions();
|
|
|
|
this.setDimensions();
|
|
|
|
this.afterSetDimensions();
|
2015-12-17 15:21:04 +01:00
|
|
|
|
|
|
|
// Data min/max
|
|
|
|
this.beforeDataLimits();
|
|
|
|
this.determineDataLimits();
|
|
|
|
this.afterDataLimits();
|
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
// Ticks
|
|
|
|
this.beforeBuildTicks();
|
|
|
|
this.buildTicks();
|
|
|
|
this.afterBuildTicks();
|
2015-09-25 04:08:25 +02:00
|
|
|
|
|
|
|
this.beforeTickToLabelConversion();
|
|
|
|
this.convertTicksToLabels();
|
|
|
|
this.afterTickToLabelConversion();
|
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
// Tick Rotation
|
|
|
|
this.beforeCalculateTickRotation();
|
|
|
|
this.calculateTickRotation();
|
|
|
|
this.afterCalculateTickRotation();
|
|
|
|
// Fit
|
|
|
|
this.beforeFit();
|
|
|
|
this.fit();
|
|
|
|
this.afterFit();
|
|
|
|
//
|
|
|
|
this.afterUpdate();
|
|
|
|
|
|
|
|
return this.minSize;
|
|
|
|
|
|
|
|
},
|
2015-12-17 15:21:04 +01:00
|
|
|
afterUpdate: function() {
|
|
|
|
helpers.callCallback(this.options.afterUpdate, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
beforeSetDimensions: function() {
|
|
|
|
helpers.callCallback(this.options.beforeSetDimensions, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
setDimensions: function() {
|
|
|
|
// Set the unconstrained dimension before label rotation
|
|
|
|
if (this.isHorizontal()) {
|
2015-10-13 17:43:00 +02:00
|
|
|
// Reset position before calculating rotation
|
2015-09-24 05:52:31 +02:00
|
|
|
this.width = this.maxWidth;
|
2015-10-13 17:43:00 +02:00
|
|
|
this.left = 0;
|
|
|
|
this.right = this.width;
|
2015-09-24 05:52:31 +02:00
|
|
|
} else {
|
|
|
|
this.height = this.maxHeight;
|
2015-10-13 17:43:00 +02:00
|
|
|
|
|
|
|
// Reset position before calculating rotation
|
|
|
|
this.top = 0;
|
2015-10-21 02:33:51 +02:00
|
|
|
this.bottom = this.height;
|
2015-09-24 05:52:31 +02:00
|
|
|
}
|
2015-09-26 03:04:45 +02:00
|
|
|
|
|
|
|
// Reset padding
|
|
|
|
this.paddingLeft = 0;
|
|
|
|
this.paddingTop = 0;
|
|
|
|
this.paddingRight = 0;
|
|
|
|
this.paddingBottom = 0;
|
2015-06-13 19:25:41 +02:00
|
|
|
},
|
2015-12-17 15:21:04 +01:00
|
|
|
afterSetDimensions: function() {
|
|
|
|
helpers.callCallback(this.options.afterSetDimensions, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
// Data limits
|
|
|
|
beforeDataLimits: function() {
|
|
|
|
helpers.callCallback(this.options.beforeDataLimits, [this]);
|
|
|
|
},
|
|
|
|
determineDataLimits: helpers.noop,
|
|
|
|
afterDataLimits: function() {
|
|
|
|
helpers.callCallback(this.options.afterDataLimits, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
//
|
|
|
|
beforeBuildTicks: function() {
|
|
|
|
helpers.callCallback(this.options.beforeBuildTicks, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
buildTicks: helpers.noop,
|
2015-12-17 15:21:04 +01:00
|
|
|
afterBuildTicks: function() {
|
|
|
|
helpers.callCallback(this.options.afterBuildTicks, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
beforeTickToLabelConversion: function() {
|
|
|
|
helpers.callCallback(this.options.beforeTickToLabelConversion, [this]);
|
|
|
|
},
|
2015-09-25 04:08:25 +02:00
|
|
|
convertTicksToLabels: function() {
|
|
|
|
// Convert ticks to strings
|
|
|
|
this.ticks = this.ticks.map(function(numericalTick, index, ticks) {
|
2015-10-07 04:40:25 +02:00
|
|
|
if (this.options.ticks.userCallback) {
|
|
|
|
return this.options.ticks.userCallback(numericalTick, index, ticks);
|
|
|
|
}
|
2015-10-24 04:00:59 +02:00
|
|
|
return this.options.ticks.callback(numericalTick, index, ticks);
|
2015-10-07 04:40:25 +02:00
|
|
|
},
|
|
|
|
this);
|
2015-09-25 04:08:25 +02:00
|
|
|
},
|
2015-12-17 15:21:04 +01:00
|
|
|
afterTickToLabelConversion: function() {
|
|
|
|
helpers.callCallback(this.options.afterTickToLabelConversion, [this]);
|
|
|
|
},
|
2015-09-25 04:08:25 +02:00
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
//
|
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
beforeCalculateTickRotation: function() {
|
|
|
|
helpers.callCallback(this.options.beforeCalculateTickRotation, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
calculateTickRotation: function() {
|
2015-09-23 03:31:26 +02:00
|
|
|
//Get the width of each grid by calculating the difference
|
|
|
|
//between x offsets between 0 and 1.
|
|
|
|
var labelFont = helpers.fontString(this.options.ticks.fontSize, this.options.ticks.fontStyle, this.options.ticks.fontFamily);
|
|
|
|
this.ctx.font = labelFont;
|
|
|
|
|
|
|
|
var firstWidth = this.ctx.measureText(this.ticks[0]).width;
|
|
|
|
var lastWidth = this.ctx.measureText(this.ticks[this.ticks.length - 1]).width;
|
|
|
|
var firstRotated;
|
|
|
|
|
|
|
|
this.labelRotation = 0;
|
2015-12-29 15:32:40 +01:00
|
|
|
this.paddingRight = 0;
|
|
|
|
this.paddingLeft = 0;
|
2015-06-13 16:15:21 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
if (this.options.display) {
|
|
|
|
if (this.isHorizontal()) {
|
|
|
|
this.paddingRight = lastWidth / 2 + 3;
|
|
|
|
this.paddingLeft = firstWidth / 2 + 3;
|
2015-06-13 16:15:21 +02:00
|
|
|
|
2016-01-15 13:05:03 +01:00
|
|
|
if (!this.longestTextCache) {
|
|
|
|
this.longestTextCache = {};
|
|
|
|
}
|
|
|
|
var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.ticks, this.longestTextCache);
|
2015-12-29 15:32:40 +01:00
|
|
|
var labelWidth = originalLabelWidth;
|
|
|
|
var cosRotation;
|
|
|
|
var sinRotation;
|
2015-06-13 16:15:21 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
// Allow 3 pixels x2 padding either side for label readability
|
|
|
|
// only the index matters for a dataset scale, but we want a consistent interface between scales
|
|
|
|
var tickWidth = this.getPixelForTick(1) - this.getPixelForTick(0) - 6;
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
//Max label rotation can be set or default to 90 - also act as a loop counter
|
|
|
|
while (labelWidth > tickWidth && this.labelRotation < this.options.ticks.maxRotation) {
|
|
|
|
cosRotation = Math.cos(helpers.toRadians(this.labelRotation));
|
|
|
|
sinRotation = Math.sin(helpers.toRadians(this.labelRotation));
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
firstRotated = cosRotation * firstWidth;
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
// We're right aligning the text now.
|
|
|
|
if (firstRotated + this.options.ticks.fontSize / 2 > this.yLabelWidth) {
|
|
|
|
this.paddingLeft = firstRotated + this.options.ticks.fontSize / 2;
|
|
|
|
}
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
this.paddingRight = this.options.ticks.fontSize / 2;
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
if (sinRotation * originalLabelWidth > this.maxHeight) {
|
|
|
|
// go back one step
|
|
|
|
this.labelRotation--;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-29 15:32:40 +01:00
|
|
|
this.labelRotation++;
|
|
|
|
labelWidth = cosRotation * originalLabelWidth;
|
2015-06-13 16:15:21 +02:00
|
|
|
}
|
2015-09-23 03:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
if (this.margins) {
|
2015-12-29 15:32:40 +01:00
|
|
|
this.paddingLeft = Math.max(this.paddingLeft - this.margins.left, 0);
|
|
|
|
this.paddingRight = Math.max(this.paddingRight - this.margins.right, 0);
|
2015-09-23 03:31:26 +02:00
|
|
|
}
|
|
|
|
},
|
2015-12-17 15:21:04 +01:00
|
|
|
afterCalculateTickRotation: function() {
|
|
|
|
helpers.callCallback(this.options.afterCalculateTickRotation, [this]);
|
|
|
|
},
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
//
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-17 15:21:04 +01:00
|
|
|
beforeFit: function() {
|
|
|
|
helpers.callCallback(this.options.beforeFit, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
fit: function() {
|
|
|
|
|
|
|
|
this.minSize = {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Width
|
|
|
|
if (this.isHorizontal()) {
|
2015-11-21 16:51:32 +01:00
|
|
|
// 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;
|
2015-09-24 07:23:20 +02:00
|
|
|
} else {
|
2015-11-25 01:18:15 +01:00
|
|
|
this.minSize.width = this.options.gridLines.display && this.options.display ? 10 : 0;
|
2015-09-24 05:52:31 +02:00
|
|
|
}
|
|
|
|
|
2015-09-24 07:23:20 +02:00
|
|
|
// height
|
|
|
|
if (this.isHorizontal()) {
|
2015-11-25 01:18:15 +01:00
|
|
|
this.minSize.height = this.options.gridLines.display && this.options.display ? 10 : 0;
|
2015-09-24 07:23:20 +02:00
|
|
|
} else {
|
|
|
|
this.minSize.height = this.maxHeight; // fill all the height
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:53:48 +02:00
|
|
|
// Are we showing a title for the scale?
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.scaleLabel.display) {
|
2015-10-07 04:40:25 +02:00
|
|
|
if (this.isHorizontal()) {
|
|
|
|
this.minSize.height += (this.options.scaleLabel.fontSize * 1.5);
|
|
|
|
} else {
|
|
|
|
this.minSize.width += (this.options.scaleLabel.fontSize * 1.5);
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 00:53:48 +02:00
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.ticks.display && this.options.display) {
|
2015-09-24 07:23:20 +02:00
|
|
|
// Don't bother fitting the ticks if we are not showing them
|
|
|
|
var labelFont = helpers.fontString(this.options.ticks.fontSize,
|
|
|
|
this.options.ticks.fontStyle, this.options.ticks.fontFamily);
|
|
|
|
|
2016-01-15 13:05:03 +01:00
|
|
|
if (!this.longestTextCache) {
|
|
|
|
this.longestTextCache = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var largestTextWidth = helpers.longestText(this.ctx, labelFont, this.ticks, this.longestTextCache);
|
|
|
|
|
2015-09-24 07:23:20 +02:00
|
|
|
if (this.isHorizontal()) {
|
|
|
|
// A horizontal axis is more constrained by the height.
|
2016-01-15 13:05:03 +01:00
|
|
|
this.longestLabelWidth = largestTextWidth;
|
2015-11-13 19:24:22 +01:00
|
|
|
|
|
|
|
// TODO - improve this calculation
|
2015-12-22 15:13:02 +01:00
|
|
|
var labelHeight = (Math.sin(helpers.toRadians(this.labelRotation)) * this.longestLabelWidth) + 1.5 * this.options.ticks.fontSize;
|
2015-09-25 00:53:48 +02:00
|
|
|
|
2015-09-24 07:23:20 +02:00
|
|
|
this.minSize.height = Math.min(this.maxHeight, this.minSize.height + labelHeight);
|
|
|
|
|
|
|
|
labelFont = helpers.fontString(this.options.ticks.fontSize, this.options.ticks.fontStyle, this.options.ticks.fontFamily);
|
|
|
|
this.ctx.font = labelFont;
|
|
|
|
|
|
|
|
var firstLabelWidth = this.ctx.measureText(this.ticks[0]).width;
|
|
|
|
var lastLabelWidth = this.ctx.measureText(this.ticks[this.ticks.length - 1]).width;
|
|
|
|
|
2015-09-28 03:34:16 +02:00
|
|
|
// Ensure that our ticks are always inside the canvas. When rotated, ticks are right aligned which means that the right padding is dominated
|
|
|
|
// by the font height
|
2015-09-26 03:04:45 +02:00
|
|
|
var cosRotation = Math.cos(helpers.toRadians(this.labelRotation));
|
2015-09-28 03:34:16 +02:00
|
|
|
var sinRotation = Math.sin(helpers.toRadians(this.labelRotation));
|
|
|
|
this.paddingLeft = this.labelRotation !== 0 ? (cosRotation * firstLabelWidth) + 3 : firstLabelWidth / 2 + 3; // add 3 px to move away from canvas edges
|
|
|
|
this.paddingRight = this.labelRotation !== 0 ? (sinRotation * (this.options.ticks.fontSize / 2)) + 3 : lastLabelWidth / 2 + 3; // when rotated
|
2015-09-24 07:23:20 +02:00
|
|
|
} 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;
|
|
|
|
|
2015-11-13 19:24:22 +01:00
|
|
|
// Account for padding
|
|
|
|
if (!this.options.ticks.mirror) {
|
|
|
|
largestTextWidth += this.options.ticks.padding;
|
|
|
|
}
|
|
|
|
|
2015-09-24 07:23:20 +02:00
|
|
|
if (largestTextWidth < maxLabelWidth) {
|
|
|
|
// We don't need all the room
|
|
|
|
this.minSize.width += largestTextWidth;
|
|
|
|
} else {
|
|
|
|
// Expand to max size
|
|
|
|
this.minSize.width = this.maxWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.paddingTop = this.options.ticks.fontSize / 2;
|
|
|
|
this.paddingBottom = this.options.ticks.fontSize / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.margins) {
|
2015-12-29 15:32:40 +01:00
|
|
|
this.paddingLeft = Math.max(this.paddingLeft - this.margins.left, 0);
|
|
|
|
this.paddingTop = Math.max(this.paddingTop - this.margins.top, 0);
|
|
|
|
this.paddingRight = Math.max(this.paddingRight - this.margins.right, 0);
|
|
|
|
this.paddingBottom = Math.max(this.paddingBottom - this.margins.bottom, 0);
|
2015-09-23 03:31:26 +02:00
|
|
|
}
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
this.width = this.minSize.width;
|
|
|
|
this.height = this.minSize.height;
|
2015-09-24 07:23:20 +02:00
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
},
|
2015-12-17 15:21:04 +01:00
|
|
|
afterFit: function() {
|
|
|
|
helpers.callCallback(this.options.afterFit, [this]);
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
// Shared Methods
|
|
|
|
isHorizontal: function() {
|
2015-12-09 02:22:02 +01:00
|
|
|
return this.options.position === "top" || this.options.position === "bottom";
|
2015-09-23 03:31:26 +02:00
|
|
|
},
|
2015-11-21 16:51:32 +01:00
|
|
|
isFullWidth: function() {
|
|
|
|
return (this.options.fullWidth);
|
|
|
|
},
|
2015-10-24 00:36:59 +02:00
|
|
|
|
|
|
|
// Get the correct value. NaN bad inputs, If the value type is object get the x or y based on whether we are horizontal or not
|
2015-10-24 09:22:12 +02:00
|
|
|
getRightValue: function getRightValue(rawValue) {
|
|
|
|
// Null and undefined values first
|
|
|
|
if (rawValue === null || typeof(rawValue) === 'undefined') {
|
2015-10-24 00:36:59 +02:00
|
|
|
return NaN;
|
|
|
|
}
|
2015-10-24 09:22:12 +02:00
|
|
|
// isNaN(object) returns true, so make sure NaN is checking for a number
|
|
|
|
if (typeof(rawValue) === 'number' && isNaN(rawValue)) {
|
|
|
|
return NaN;
|
|
|
|
}
|
|
|
|
// If it is in fact an object, dive in one more level
|
|
|
|
if (typeof(rawValue) === "object") {
|
2015-12-05 22:45:31 +01:00
|
|
|
if (rawValue instanceof Date) {
|
|
|
|
return rawValue;
|
|
|
|
} else {
|
|
|
|
return getRightValue(this.isHorizontal() ? rawValue.x : rawValue.y);
|
|
|
|
}
|
2015-10-24 09:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value is good, return it
|
|
|
|
return rawValue;
|
2015-10-19 00:00:46 +02:00
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
2015-10-18 22:13:57 +02:00
|
|
|
// Used to get the value to display in the tooltip for the data at the given index
|
|
|
|
// function getLabelForIndex(index, datasetIndex)
|
|
|
|
getLabelForIndex: helpers.noop,
|
2015-10-07 04:40:25 +02:00
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
// Used to get data value locations. Value can either be an index or a numerical value
|
|
|
|
getPixelForValue: helpers.noop,
|
|
|
|
|
|
|
|
// Used for tick location, should
|
|
|
|
getPixelForTick: function(index, includeOffset) {
|
2015-09-23 03:31:26 +02:00
|
|
|
if (this.isHorizontal()) {
|
|
|
|
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
|
|
|
|
var tickWidth = innerWidth / Math.max((this.ticks.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
|
|
|
|
var pixel = (tickWidth * index) + this.paddingLeft;
|
|
|
|
|
|
|
|
if (includeOffset) {
|
|
|
|
pixel += tickWidth / 2;
|
|
|
|
}
|
2015-11-21 16:51:32 +01:00
|
|
|
|
|
|
|
var finalVal = this.left + Math.round(pixel);
|
|
|
|
finalVal += this.isFullWidth() ? this.margins.left : 0;
|
|
|
|
return finalVal;
|
2015-09-23 03:31:26 +02:00
|
|
|
} else {
|
2015-09-24 10:25:21 +02:00
|
|
|
var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
|
|
|
|
return this.top + (index * (innerHeight / (this.ticks.length - 1)));
|
2015-09-23 03:31:26 +02:00
|
|
|
}
|
|
|
|
},
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
// Utility for getting the pixel location of a percentage of scale
|
2015-12-09 02:22:02 +01:00
|
|
|
getPixelForDecimal: function(decimal/*, includeOffset*/) {
|
2015-09-23 03:31:26 +02:00
|
|
|
if (this.isHorizontal()) {
|
|
|
|
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
|
|
|
|
var valueOffset = (innerWidth * decimal) + this.paddingLeft;
|
|
|
|
|
2015-11-21 16:51:32 +01:00
|
|
|
var finalVal = this.left + Math.round(valueOffset);
|
|
|
|
finalVal += this.isFullWidth() ? this.margins.left : 0;
|
|
|
|
return finalVal;
|
2015-09-23 03:31:26 +02:00
|
|
|
} else {
|
2015-10-12 22:51:00 +02:00
|
|
|
return this.top + (decimal * this.height);
|
2015-09-23 03:31:26 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Actualy draw the scale on the canvas
|
|
|
|
// @param {rectangle} chartArea : the area of the chart to draw full grid lines on
|
|
|
|
draw: function(chartArea) {
|
|
|
|
if (this.options.display) {
|
|
|
|
|
|
|
|
var setContextLineSettings;
|
2015-09-26 03:04:45 +02:00
|
|
|
var isRotated = this.labelRotation !== 0;
|
2015-09-24 05:52:31 +02:00
|
|
|
var skipRatio;
|
2015-09-24 22:34:26 +02:00
|
|
|
var scaleLabelX;
|
|
|
|
var scaleLabelY;
|
2015-12-18 16:18:49 +01:00
|
|
|
var useAutoskipper = this.options.ticks.autoSkip;
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2016-01-07 18:41:42 +01:00
|
|
|
|
|
|
|
// figure out the maximum number of gridlines to show
|
|
|
|
var maxTicks;
|
|
|
|
|
|
|
|
if (this.options.ticks.maxTicksLimit) {
|
|
|
|
maxTicks = this.options.ticks.maxTicksLimit;
|
|
|
|
}
|
|
|
|
|
2015-10-17 04:00:43 +02:00
|
|
|
// Make sure we draw text in the correct color and font
|
2015-09-23 03:31:26 +02:00
|
|
|
this.ctx.fillStyle = this.options.ticks.fontColor;
|
2015-10-17 04:00:43 +02:00
|
|
|
var labelFont = helpers.fontString(this.options.ticks.fontSize, this.options.ticks.fontStyle, this.options.ticks.fontFamily);
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-22 15:13:02 +01:00
|
|
|
var cosRotation = Math.cos(helpers.toRadians(this.labelRotation));
|
|
|
|
var sinRotation = Math.sin(helpers.toRadians(this.labelRotation));
|
|
|
|
var longestRotatedLabel = this.longestLabelWidth * cosRotation;
|
|
|
|
var rotatedLabelHeight = this.options.ticks.fontSize * sinRotation;
|
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
if (this.isHorizontal()) {
|
|
|
|
setContextLineSettings = true;
|
2015-12-09 02:22:02 +01:00
|
|
|
var yTickStart = this.options.position === "bottom" ? this.top : this.bottom - 10;
|
|
|
|
var yTickEnd = this.options.position === "bottom" ? this.top + 10 : this.bottom;
|
2015-09-24 05:52:31 +02:00
|
|
|
skipRatio = false;
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-23 13:42:24 +01:00
|
|
|
if (((longestRotatedLabel / 2) + this.options.ticks.autoSkipPadding) * this.ticks.length > (this.width - (this.paddingLeft + this.paddingRight))) {
|
2015-12-22 15:13:02 +01:00
|
|
|
skipRatio = 1 + Math.floor((((longestRotatedLabel / 2) + this.options.ticks.autoSkipPadding) * this.ticks.length) / (this.width - (this.paddingLeft + this.paddingRight)));
|
2015-06-13 16:15:21 +02:00
|
|
|
}
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-12-18 16:18:49 +01:00
|
|
|
if (!useAutoskipper) {
|
|
|
|
skipRatio = false;
|
|
|
|
}
|
2016-01-07 18:41:42 +01:00
|
|
|
|
|
|
|
// if they defined a max number of ticks,
|
|
|
|
// increase skipRatio until that number is met
|
2016-01-07 21:57:53 +01:00
|
|
|
if (maxTicks && this.ticks.length > maxTicks) {
|
2016-01-07 18:41:42 +01:00
|
|
|
while (!skipRatio || this.ticks.length / (skipRatio || 1) > maxTicks) {
|
|
|
|
if (!skipRatio) {
|
|
|
|
skipRatio = 1;
|
|
|
|
}
|
|
|
|
skipRatio += 1;
|
|
|
|
}
|
|
|
|
}
|
2015-12-18 16:18:49 +01:00
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
helpers.each(this.ticks, function(label, index) {
|
|
|
|
// Blank ticks
|
2016-01-07 18:41:42 +01:00
|
|
|
var isLastTick = this.ticks.length == index + 1;
|
|
|
|
var shouldSkip = skipRatio > 1 && index % skipRatio > 0;
|
|
|
|
if (shouldSkip && !isLastTick || (label === undefined || label === null)) {
|
2015-09-23 03:31:26 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-09-24 05:52:31 +02:00
|
|
|
var xLineValue = this.getPixelForTick(index); // xvalues for grid lines
|
|
|
|
var xLabelValue = this.getPixelForTick(index, this.options.gridLines.offsetGridLines); // x values for ticks (need to consider offsetLabel option)
|
2015-09-23 03:31:26 +02:00
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.gridLines.display) {
|
2015-09-24 10:25:21 +02:00
|
|
|
if (index === (typeof this.zeroLineIndex !== 'undefined' ? this.zeroLineIndex : 0)) {
|
2015-09-23 03:31:26 +02:00
|
|
|
// Draw the first index specially
|
|
|
|
this.ctx.lineWidth = this.options.gridLines.zeroLineWidth;
|
|
|
|
this.ctx.strokeStyle = this.options.gridLines.zeroLineColor;
|
|
|
|
setContextLineSettings = true; // reset next time
|
|
|
|
} else if (setContextLineSettings) {
|
|
|
|
this.ctx.lineWidth = this.options.gridLines.lineWidth;
|
|
|
|
this.ctx.strokeStyle = this.options.gridLines.color;
|
|
|
|
setContextLineSettings = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xLineValue += helpers.aliasPixel(this.ctx.lineWidth);
|
|
|
|
|
|
|
|
// Draw the label area
|
|
|
|
this.ctx.beginPath();
|
|
|
|
|
|
|
|
if (this.options.gridLines.drawTicks) {
|
|
|
|
this.ctx.moveTo(xLineValue, yTickStart);
|
|
|
|
this.ctx.lineTo(xLineValue, yTickEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the chart area
|
|
|
|
if (this.options.gridLines.drawOnChartArea) {
|
|
|
|
this.ctx.moveTo(xLineValue, chartArea.top);
|
|
|
|
this.ctx.lineTo(xLineValue, chartArea.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to stroke in the loop because we are potentially changing line widths & colours
|
|
|
|
this.ctx.stroke();
|
|
|
|
}
|
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.ticks.display) {
|
2015-09-23 03:31:26 +02:00
|
|
|
this.ctx.save();
|
2015-09-28 03:34:16 +02:00
|
|
|
this.ctx.translate(xLabelValue, (isRotated) ? this.top + 12 : this.options.position === "top" ? this.bottom - 10 : this.top + 10);
|
2015-09-23 03:31:26 +02:00
|
|
|
this.ctx.rotate(helpers.toRadians(this.labelRotation) * -1);
|
2015-10-17 04:00:43 +02:00
|
|
|
this.ctx.font = labelFont;
|
2015-09-23 03:31:26 +02:00
|
|
|
this.ctx.textAlign = (isRotated) ? "right" : "center";
|
2015-09-28 03:34:16 +02:00
|
|
|
this.ctx.textBaseline = (isRotated) ? "middle" : this.options.position === "top" ? "bottom" : "top";
|
2015-09-23 03:31:26 +02:00
|
|
|
this.ctx.fillText(label, 0, 0);
|
|
|
|
this.ctx.restore();
|
|
|
|
}
|
|
|
|
}, this);
|
2015-09-24 22:34:26 +02:00
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.scaleLabel.display) {
|
2015-09-24 22:34:26 +02:00
|
|
|
// Draw the scale label
|
|
|
|
this.ctx.textAlign = "center";
|
|
|
|
this.ctx.textBaseline = 'middle';
|
2015-10-17 04:00:43 +02:00
|
|
|
this.ctx.fillStyle = this.options.scaleLabel.fontColor; // render in correct colour
|
2015-09-24 22:34:26 +02:00
|
|
|
this.ctx.font = helpers.fontString(this.options.scaleLabel.fontSize, this.options.scaleLabel.fontStyle, this.options.scaleLabel.fontFamily);
|
|
|
|
|
|
|
|
scaleLabelX = this.left + ((this.right - this.left) / 2); // midpoint of the width
|
2015-12-09 02:22:02 +01:00
|
|
|
scaleLabelY = this.options.position === 'bottom' ? this.bottom - (this.options.scaleLabel.fontSize / 2) : this.top + (this.options.scaleLabel.fontSize / 2);
|
2015-09-24 22:34:26 +02:00
|
|
|
|
|
|
|
this.ctx.fillText(this.options.scaleLabel.labelString, scaleLabelX, scaleLabelY);
|
2015-06-13 16:15:21 +02:00
|
|
|
}
|
2015-09-24 22:34:26 +02:00
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
} else {
|
2015-09-24 05:52:31 +02:00
|
|
|
setContextLineSettings = true;
|
2015-12-09 02:22:02 +01:00
|
|
|
var xTickStart = this.options.position === "right" ? this.left : this.right - 5;
|
|
|
|
var xTickEnd = this.options.position === "right" ? this.left + 5 : this.right;
|
2015-09-24 05:52:31 +02:00
|
|
|
|
|
|
|
helpers.each(this.ticks, function(label, index) {
|
2015-11-12 03:40:29 +01:00
|
|
|
// If the callback returned a null or undefined value, do not draw this line
|
|
|
|
if (label === undefined || label === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
var yLineValue = this.getPixelForTick(index); // xvalues for grid lines
|
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.gridLines.display) {
|
2015-09-24 10:25:21 +02:00
|
|
|
if (index === (typeof this.zeroLineIndex !== 'undefined' ? this.zeroLineIndex : 0)) {
|
2015-09-24 05:52:31 +02:00
|
|
|
// Draw the first index specially
|
|
|
|
this.ctx.lineWidth = this.options.gridLines.zeroLineWidth;
|
|
|
|
this.ctx.strokeStyle = this.options.gridLines.zeroLineColor;
|
|
|
|
setContextLineSettings = true; // reset next time
|
|
|
|
} else if (setContextLineSettings) {
|
|
|
|
this.ctx.lineWidth = this.options.gridLines.lineWidth;
|
|
|
|
this.ctx.strokeStyle = this.options.gridLines.color;
|
|
|
|
setContextLineSettings = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
yLineValue += helpers.aliasPixel(this.ctx.lineWidth);
|
|
|
|
|
|
|
|
// Draw the label area
|
|
|
|
this.ctx.beginPath();
|
|
|
|
|
|
|
|
if (this.options.gridLines.drawTicks) {
|
|
|
|
this.ctx.moveTo(xTickStart, yLineValue);
|
|
|
|
this.ctx.lineTo(xTickEnd, yLineValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the chart area
|
|
|
|
if (this.options.gridLines.drawOnChartArea) {
|
|
|
|
this.ctx.moveTo(chartArea.left, yLineValue);
|
|
|
|
this.ctx.lineTo(chartArea.right, yLineValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to stroke in the loop because we are potentially changing line widths & colours
|
|
|
|
this.ctx.stroke();
|
|
|
|
}
|
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.ticks.display) {
|
2015-09-26 03:04:45 +02:00
|
|
|
var xLabelValue;
|
|
|
|
var yLabelValue = this.getPixelForTick(index, this.options.gridLines.offsetGridLines); // x values for ticks (need to consider offsetLabel option)
|
|
|
|
|
2015-09-24 05:52:31 +02:00
|
|
|
this.ctx.save();
|
2015-09-26 03:04:45 +02:00
|
|
|
|
2015-12-09 02:22:02 +01:00
|
|
|
if (this.options.position === "left") {
|
2015-09-26 03:04:45 +02:00
|
|
|
if (this.options.ticks.mirror) {
|
|
|
|
xLabelValue = this.right + this.options.ticks.padding;
|
|
|
|
this.ctx.textAlign = "left";
|
|
|
|
} else {
|
|
|
|
xLabelValue = this.right - this.options.ticks.padding;
|
|
|
|
this.ctx.textAlign = "right";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// right side
|
|
|
|
if (this.options.ticks.mirror) {
|
|
|
|
xLabelValue = this.left - this.options.ticks.padding;
|
|
|
|
this.ctx.textAlign = "right";
|
|
|
|
} else {
|
|
|
|
xLabelValue = this.left + this.options.ticks.padding;
|
|
|
|
this.ctx.textAlign = "left";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 10:25:21 +02:00
|
|
|
this.ctx.translate(xLabelValue, yLabelValue);
|
2015-09-24 05:52:31 +02:00
|
|
|
this.ctx.rotate(helpers.toRadians(this.labelRotation) * -1);
|
2015-10-17 04:00:43 +02:00
|
|
|
this.ctx.font = labelFont;
|
2015-09-24 10:25:21 +02:00
|
|
|
this.ctx.textBaseline = "middle";
|
2015-09-24 05:52:31 +02:00
|
|
|
this.ctx.fillText(label, 0, 0);
|
|
|
|
this.ctx.restore();
|
|
|
|
}
|
|
|
|
}, this);
|
2015-09-24 22:34:26 +02:00
|
|
|
|
2015-11-25 01:18:15 +01:00
|
|
|
if (this.options.scaleLabel.display) {
|
2015-09-24 22:34:26 +02:00
|
|
|
// Draw the scale label
|
2015-12-09 02:22:02 +01:00
|
|
|
scaleLabelX = this.options.position === 'left' ? this.left + (this.options.scaleLabel.fontSize / 2) : this.right - (this.options.scaleLabel.fontSize / 2);
|
2015-09-24 22:34:26 +02:00
|
|
|
scaleLabelY = this.top + ((this.bottom - this.top) / 2);
|
2015-12-09 02:22:02 +01:00
|
|
|
var rotation = this.options.position === 'left' ? -0.5 * Math.PI : 0.5 * Math.PI;
|
2015-09-24 22:34:26 +02:00
|
|
|
|
|
|
|
this.ctx.save();
|
|
|
|
this.ctx.translate(scaleLabelX, scaleLabelY);
|
|
|
|
this.ctx.rotate(rotation);
|
|
|
|
this.ctx.textAlign = "center";
|
2015-10-17 04:00:43 +02:00
|
|
|
this.ctx.fillStyle = this.options.scaleLabel.fontColor; // render in correct colour
|
2015-09-24 22:34:26 +02:00
|
|
|
this.ctx.font = helpers.fontString(this.options.scaleLabel.fontSize, this.options.scaleLabel.fontStyle, this.options.scaleLabel.fontFamily);
|
|
|
|
this.ctx.textBaseline = 'middle';
|
|
|
|
this.ctx.fillText(this.options.scaleLabel.labelString, 0, 0);
|
|
|
|
this.ctx.restore();
|
2015-06-13 16:15:21 +02:00
|
|
|
}
|
2015-09-20 16:32:43 +02:00
|
|
|
}
|
2015-12-28 15:26:59 +01:00
|
|
|
|
|
|
|
// Draw the line at the edge of the axis
|
|
|
|
this.ctx.lineWidth = this.options.gridLines.lineWidth;
|
|
|
|
this.ctx.strokeStyle = this.options.gridLines.color;
|
|
|
|
var x1 = this.left, x2 = this.right, y1 = this.top, y2 = this.bottom;
|
|
|
|
|
|
|
|
if (this.isHorizontal()) {
|
|
|
|
y1 = y2 = this.options.position === 'top' ? this.bottom : this.top;
|
2015-12-28 15:37:31 +01:00
|
|
|
y1 += helpers.aliasPixel(this.ctx.lineWidth);
|
|
|
|
y2 += helpers.aliasPixel(this.ctx.lineWidth);
|
2015-12-28 15:26:59 +01:00
|
|
|
} else {
|
|
|
|
x1 = x2 = this.options.position === 'left' ? this.right : this.left;
|
2015-12-28 15:37:31 +01:00
|
|
|
x1 += helpers.aliasPixel(this.ctx.lineWidth);
|
|
|
|
x2 += helpers.aliasPixel(this.ctx.lineWidth);
|
2015-12-28 15:26:59 +01:00
|
|
|
}
|
|
|
|
|
2016-01-19 03:35:35 +01:00
|
|
|
this.ctx.beginPath();
|
2015-12-28 15:26:59 +01:00
|
|
|
this.ctx.moveTo(x1, y1);
|
|
|
|
this.ctx.lineTo(x2, y2);
|
|
|
|
this.ctx.stroke();
|
2015-06-13 16:15:21 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 03:31:26 +02:00
|
|
|
});
|
|
|
|
|
2015-05-27 18:28:00 +02:00
|
|
|
}).call(this);
|