Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
if (helpers.isDatasetVisible(dataset) && dataset.bar) {
++barCount;
}
}, this);
});
return barCount;
},

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controller.bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
helpers.each(this.getDataset().metaData, function(point, index) {
point.transition(easingDecimal);
point.draw();
}, this);
});

},

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
var easingDecimal = ease || 1;
helpers.each(this.getDataset().metaData, function(arc, index) {
arc.transition(easingDecimal).draw();
}, this);
});
},

setHoverStyle: function(arc) {
Expand Down
96 changes: 46 additions & 50 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
this.getDataset().metaData.splice(index, 0, point);

// Make sure bezier control points are updated
if (this.chart.options.showLines)
if (this.chart.options.showLines && this.chart.options.elements.line.tension !== 0)
this.updateBezierControlPoints();
},

Expand All @@ -82,30 +82,28 @@

// Update Line
if (this.chart.options.showLines) {
helpers.extend(line, {
// Utility
_scale: yScale,
_datasetIndex: this.index,
// Data
_children: points,
// Model
_model: {
// Appearance
tension: line.custom && line.custom.tension ? line.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension),
backgroundColor: line.custom && line.custom.backgroundColor ? line.custom.backgroundColor : (this.getDataset().backgroundColor || this.chart.options.elements.line.backgroundColor),
borderWidth: line.custom && line.custom.borderWidth ? line.custom.borderWidth : (this.getDataset().borderWidth || this.chart.options.elements.line.borderWidth),
borderColor: line.custom && line.custom.borderColor ? line.custom.borderColor : (this.getDataset().borderColor || this.chart.options.elements.line.borderColor),
borderCapStyle: line.custom && line.custom.borderCapStyle ? line.custom.borderCapStyle : (this.getDataset().borderCapStyle || this.chart.options.elements.line.borderCapStyle),
borderDash: line.custom && line.custom.borderDash ? line.custom.borderDash : (this.getDataset().borderDash || this.chart.options.elements.line.borderDash),
borderDashOffset: line.custom && line.custom.borderDashOffset ? line.custom.borderDashOffset : (this.getDataset().borderDashOffset || this.chart.options.elements.line.borderDashOffset),
borderJoinStyle: line.custom && line.custom.borderJoinStyle ? line.custom.borderJoinStyle : (this.getDataset().borderJoinStyle || this.chart.options.elements.line.borderJoinStyle),
fill: line.custom && line.custom.fill ? line.custom.fill : (this.getDataset().fill !== undefined ? this.getDataset().fill : this.chart.options.elements.line.fill),
// Scale
scaleTop: yScale.top,
scaleBottom: yScale.bottom,
scaleZero: scaleBase,
},
});
// Utility
line._scale = yScale;
line._datasetIndex = this.index;
// Data
line._children = points;
// Model
line._model = {
// Appearance
tension: line.custom && line.custom.tension ? line.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension),
backgroundColor: line.custom && line.custom.backgroundColor ? line.custom.backgroundColor : (this.getDataset().backgroundColor || this.chart.options.elements.line.backgroundColor),
borderWidth: line.custom && line.custom.borderWidth ? line.custom.borderWidth : (this.getDataset().borderWidth || this.chart.options.elements.line.borderWidth),
borderColor: line.custom && line.custom.borderColor ? line.custom.borderColor : (this.getDataset().borderColor || this.chart.options.elements.line.borderColor),
borderCapStyle: line.custom && line.custom.borderCapStyle ? line.custom.borderCapStyle : (this.getDataset().borderCapStyle || this.chart.options.elements.line.borderCapStyle),
borderDash: line.custom && line.custom.borderDash ? line.custom.borderDash : (this.getDataset().borderDash || this.chart.options.elements.line.borderDash),
borderDashOffset: line.custom && line.custom.borderDashOffset ? line.custom.borderDashOffset : (this.getDataset().borderDashOffset || this.chart.options.elements.line.borderDashOffset),
borderJoinStyle: line.custom && line.custom.borderJoinStyle ? line.custom.borderJoinStyle : (this.getDataset().borderJoinStyle || this.chart.options.elements.line.borderJoinStyle),
fill: line.custom && line.custom.fill ? line.custom.fill : (this.getDataset().fill !== undefined ? this.getDataset().fill : this.chart.options.elements.line.fill),
// Scale
scaleTop: yScale.top,
scaleBottom: yScale.bottom,
scaleZero: scaleBase
};
line.pivot();
}

Expand All @@ -114,7 +112,7 @@
this.updateElement(point, index, reset);
}, this);

if (this.chart.options.showLines)
if (this.chart.options.showLines && this.chart.options.elements.line.tension !== 0)
this.updateBezierControlPoints();
},

Expand Down Expand Up @@ -174,28 +172,26 @@
scaleBase = yScale.getPixelForValue(0);
}

helpers.extend(point, {
// Utility
_chart: this.chart.chart,
_xScale: xScale,
_yScale: yScale,
_datasetIndex: this.index,
_index: index,

// Desired view properties
_model: {
x: xScale.getPixelForValue(this.getDataset().data[index], index, this.index, this.chart.isCombo),
y: reset ? scaleBase : this.calculatePointY(this.getDataset().data[index], index, this.index, this.chart.isCombo),
// Appearance
tension: point.custom && point.custom.tension ? point.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension),
radius: point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().radius, index, this.chart.options.elements.point.radius),
backgroundColor: this.getPointBackgroundColor(point, index),
borderColor: this.getPointBorderColor(point, index),
borderWidth: this.getPointBorderWidth(point, index),
// Tooltip
hitRadius: point.custom && point.custom.hitRadius ? point.custom.hitRadius : helpers.getValueAtIndexOrDefault(this.getDataset().hitRadius, index, this.chart.options.elements.point.hitRadius),
},
});
// Utility
point._chart = this.chart.chart;
point._xScale = xScale;
point._yScale = yScale;
point._datasetIndex = this.index;
point._index = index;

// Desired view properties
point._model = {
x: xScale.getPixelForValue(this.getDataset().data[index], index, this.index, this.chart.isCombo),
y: reset ? scaleBase : this.calculatePointY(this.getDataset().data[index], index, this.index, this.chart.isCombo),
// Appearance
tension: point.custom && point.custom.tension ? point.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension),
radius: point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().radius, index, this.chart.options.elements.point.radius),
backgroundColor: this.getPointBackgroundColor(point, index),
borderColor: this.getPointBorderColor(point, index),
borderWidth: this.getPointBorderWidth(point, index),
// Tooltip
hitRadius: point.custom && point.custom.hitRadius ? point.custom.hitRadius : helpers.getValueAtIndexOrDefault(this.getDataset().hitRadius, index, this.chart.options.elements.point.hitRadius)
};

point._model.skip = point.custom && point.custom.skip ? point.custom.skip : (isNaN(point._model.x) || isNaN(point._model.y));
},
Expand Down Expand Up @@ -259,9 +255,9 @@
var easingDecimal = ease || 1;

// Transition Point Locations
helpers.each(this.getDataset().metaData, function(point, index) {
helpers.each(this.getDataset().metaData, function(point) {
point.transition(easingDecimal);
}, this);
});

// Transition and Draw the line
if (this.chart.options.showLines)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
var easingDecimal = ease || 1;
helpers.each(this.getDataset().metaData, function(arc, index) {
arc.transition(easingDecimal).draw();
}, this);
});
},

setHoverStyle: function(arc) {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/controller.radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
// Transition Point Locations
helpers.each(this.getDataset().metaData, function(point, index) {
point.transition(easingDecimal);
}, this);
});

// Transition and Draw the line
this.getDataset().metaDataset.transition(easingDecimal).draw();
Expand Down
18 changes: 9 additions & 9 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@
if (this.options.scales.xAxes && this.options.scales.xAxes.length) {
helpers.each(this.options.scales.xAxes, function(xAxisOptions, index) {
xAxisOptions.id = xAxisOptions.id || (defaultXAxisID + index);
}, this);
});
}

if (this.options.scales.yAxes && this.options.scales.yAxes.length) {
// Build the y axes
helpers.each(this.options.scales.yAxes, function(yAxisOptions, index) {
yAxisOptions.id = yAxisOptions.id || (defaultYAxisID + index);
}, this);
});
}
}
},
Expand Down Expand Up @@ -239,7 +239,7 @@
resetElements: function resetElements() {
helpers.each(this.data.datasets, function(dataset, datasetIndex) {
dataset.controller.reset();
}, this);
});
},

update: function update(animationDuration, lazy) {
Expand All @@ -254,12 +254,12 @@
// Make sure all dataset controllers have correct meta data counts
helpers.each(this.data.datasets, function(dataset, datasetIndex) {
dataset.controller.buildOrUpdateElements();
}, this);
});

// This will loop through any data and do the appropriate element update for the type
helpers.each(this.data.datasets, function(dataset, datasetIndex) {
dataset.controller.update();
}, this);
});
this.render(animationDuration, lazy);
},

Expand Down Expand Up @@ -310,7 +310,7 @@
if (helpers.isDatasetVisible(dataset)) {
dataset.controller.draw(ease);
}
}, this);
});

// Finally draw the tooltip
this.tooltip.transition(easingDecimal).draw();
Expand All @@ -330,9 +330,9 @@
elementsArray.push(element);
return elementsArray;
}
}, this);
});
}
}, this);
});

return elementsArray;
},
Expand Down Expand Up @@ -361,7 +361,7 @@
if(helpers.isDatasetVisible(dataset)){
elementsArray.push(dataset.metaData[found._index]);
}
}, this);
});

return elementsArray;
},
Expand Down
19 changes: 12 additions & 7 deletions src/core/core.element.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
if (!this._view) {
this._view = helpers.clone(this._model);
}

// No animation -> No Transition
if (ease === 1) {
this._view = this._model;
this._start = null;
return this;
}

if (!this._start) {
this.pivot();
}
Expand All @@ -37,16 +45,16 @@
}

// Init if doesn't exist
else if (!this._view[key]) {
if (typeof value === 'number' && isNaN(this._view[key]) === false) {
else if (!this._view.hasOwnProperty(key)) {
if (typeof value === 'number' && !isNaN(this._view[key])) {
this._view[key] = value * ease;
} else {
this._view[key] = value || null;
this._view[key] = value;
}
}

// No unnecessary computations
else if (this._model[key] === this._view[key]) {
else if (value === this._view[key]) {
// It's the same! Woohoo!
}

Expand All @@ -70,9 +78,6 @@
}
}, this);

if (ease === 1) {
delete this._start;
}
return this;
},
tooltipPosition: function() {
Expand Down
30 changes: 27 additions & 3 deletions src/core/core.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,13 +756,37 @@
helpers.fontString = function(pixelSize, fontStyle, fontFamily) {
return fontStyle + " " + pixelSize + "px " + fontFamily;
};
helpers.longestText = function(ctx, font, arrayOfStrings) {
helpers.longestText = function(ctx, font, arrayOfStrings, cache) {
cache = cache || {};
cache.data = cache.data || {};
cache.garbageCollect = cache.garbageCollect || [];

if (cache.font !== font) {
cache.data = {};
cache.garbageCollect = [];
cache.font = font;
}

ctx.font = font;
var longest = 0;
helpers.each(arrayOfStrings, function(string) {
var textWidth = ctx.measureText(string).width;
longest = (textWidth > longest) ? textWidth : longest;
var textWidth = cache.data[string];
if (!textWidth) {
textWidth = cache.data[string] = ctx.measureText(string).width;
cache.garbageCollect.push(string);
}
if (textWidth > longest)
longest = textWidth;
});

var gcLen = cache.garbageCollect.length / 2;
if (gcLen > arrayOfStrings.length) {
for (var i = 0; i < gcLen; i++) {
var key = cache.garbageCollect.shift();
delete cache.data[key];
}
}

return longest;
};
helpers.drawRoundedRectangle = function(ctx, x, y, width, height, radius) {
Expand Down
14 changes: 11 additions & 3 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@
this.paddingRight = lastWidth / 2 + 3;
this.paddingLeft = firstWidth / 2 + 3;

var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.ticks);
if (!this.longestTextCache) {
this.longestTextCache = {};
}
var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.ticks, this.longestTextCache);
var labelWidth = originalLabelWidth;
var cosRotation;
var sinRotation;
Expand Down Expand Up @@ -289,9 +292,15 @@
var labelFont = helpers.fontString(this.options.ticks.fontSize,
this.options.ticks.fontStyle, this.options.ticks.fontFamily);

if (!this.longestTextCache) {
this.longestTextCache = {};
}

var largestTextWidth = helpers.longestText(this.ctx, labelFont, this.ticks, this.longestTextCache);

if (this.isHorizontal()) {
// A horizontal axis is more constrained by the height.
this.longestLabelWidth = helpers.longestText(this.ctx, labelFont, this.ticks);
this.longestLabelWidth = largestTextWidth;

// TODO - improve this calculation
var labelHeight = (Math.sin(helpers.toRadians(this.labelRotation)) * this.longestLabelWidth) + 1.5 * this.options.ticks.fontSize;
Expand All @@ -313,7 +322,6 @@
} 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 largestTextWidth = helpers.longestText(this.ctx, labelFont, this.ticks);

// Account for padding
if (!this.options.ticks.mirror) {
Expand Down
Loading