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

Improved code minimization #6362

Merged
merged 1 commit into from Aug 13, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/controllers/controller.bar.js
Expand Up @@ -33,14 +33,13 @@ defaults._set('bar', {
*/
function computeMinSampleSize(scale, pixels) {
var min = scale._length;
var ticks = scale.getTicks();
var prev, curr, i, ilen;

for (i = 1, ilen = pixels.length; i < ilen; ++i) {
min = Math.min(min, Math.abs(pixels[i] - pixels[i - 1]));
}

for (i = 0, ilen = ticks.length; i < ilen; ++i) {
for (i = 0, ilen = scale.getTicks().length; i < ilen; ++i) {
curr = scale.getPixelForTick(i);
min = i > 0 ? Math.min(min, Math.abs(curr - prev)) : min;
prev = curr;
Expand Down Expand Up @@ -260,13 +259,11 @@ module.exports = DatasetController.extend({
getRuler: function() {
var me = this;
var scale = me._getIndexScale();
var stackCount = me.getStackCount();
var datasetIndex = me.index;
var pixels = [];
var i, ilen, min;

for (i = 0, ilen = me.getMeta().data.length; i < ilen; ++i) {
pixels.push(scale.getPixelForValue(null, i, datasetIndex));
pixels.push(scale.getPixelForValue(null, i, me.index));
}

min = helpers.isNullOrUndef(scale.options.barThickness)
Expand All @@ -278,7 +275,7 @@ module.exports = DatasetController.extend({
pixels: pixels,
start: scale._startPixel,
end: scale._endPixel,
stackCount: stackCount,
stackCount: me.getStackCount(),
scale: scale
};
},
Expand All @@ -290,14 +287,13 @@ module.exports = DatasetController.extend({
calculateBarValuePixels: function(datasetIndex, index) {
var me = this;
var chart = me.chart;
var meta = me.getMeta();
var scale = me._getValueScale();
var isHorizontal = scale.isHorizontal();
var datasets = chart.data.datasets;
var value = scale._parseValue(datasets[datasetIndex].data[index]);
var minBarLength = scale.options.minBarLength;
var stacked = scale.options.stacked;
var stack = meta.stack;
var stack = me.getMeta().stack;
var start = value.start === undefined ? 0 : value.max >= 0 && value.min >= 0 ? value.min : value.max;
var length = value.start === undefined ? value.end : value.max >= 0 && value.min >= 0 ? value.max - value.min : value.min - value.max;
var i, imeta, ivalue, base, head, size, stackLength;
Expand Down
17 changes: 8 additions & 9 deletions src/core/core.scale.js
Expand Up @@ -442,13 +442,13 @@ var Scale = Element.extend({
var me = this;
var options = me.options;
var tickOpts = options.ticks;
var ticks = me.getTicks();
var numTicks = me.getTicks().length;
var minRotation = tickOpts.minRotation || 0;
var maxRotation = tickOpts.maxRotation;
var labelRotation = minRotation;
var labelSizes, maxLabelWidth, maxLabelHeight, maxWidth, tickWidth, maxHeight, maxLabelDiagonal;

if (!me._isVisible() || !tickOpts.display || minRotation >= maxRotation || ticks.length <= 1 || !me.isHorizontal()) {
if (!me._isVisible() || !tickOpts.display || minRotation >= maxRotation || numTicks <= 1 || !me.isHorizontal()) {
me.labelRotation = minRotation;
return;
}
Expand All @@ -460,11 +460,11 @@ var Scale = Element.extend({
// Estimate the width of each grid based on the canvas width, the maximum
// label width and the number of tick intervals
maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth);
tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1);
tickWidth = options.offset ? me.maxWidth / numTicks : maxWidth / (numTicks - 1);

// Allow 3 pixels x2 padding either side for label readability
if (maxLabelWidth + 6 > tickWidth) {
tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1));
tickWidth = maxWidth / (numTicks - (options.offset ? 0.5 : 1));
maxHeight = me.maxHeight - getTickMarkLength(options.gridLines)
- tickOpts.padding - getScaleLabelHeight(options.scaleLabel);
maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
Expand Down Expand Up @@ -494,13 +494,12 @@ var Scale = Element.extend({
height: 0
};

var ticks = me.getTicks();
var opts = me.options;
var tickOpts = opts.ticks;
var scaleLabelOpts = opts.scaleLabel;
var gridLineOpts = opts.gridLines;
var display = me._isVisible();
var position = opts.position;
var isBottom = opts.position === 'bottom';
var isHorizontal = me.isHorizontal();

// Width
Expand Down Expand Up @@ -542,16 +541,16 @@ var Scale = Element.extend({
minSize.height = Math.min(me.maxHeight, minSize.height + labelHeight + tickPadding);

var offsetLeft = me.getPixelForTick(0) - me.left;
var offsetRight = me.right - me.getPixelForTick(ticks.length - 1);
var offsetRight = me.right - me.getPixelForTick(me.getTicks().length - 1);
var paddingLeft, paddingRight;

// 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
if (isRotated) {
paddingLeft = position === 'bottom' ?
paddingLeft = isBottom ?
cosRotation * firstLabelSize.width + sinRotation * firstLabelSize.offset :
sinRotation * (firstLabelSize.height - firstLabelSize.offset);
paddingRight = position === 'bottom' ?
paddingRight = isBottom ?
sinRotation * (lastLabelSize.height - lastLabelSize.offset) :
cosRotation * lastLabelSize.width + sinRotation * lastLabelSize.offset;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/scales/scale.category.js
Expand Up @@ -60,7 +60,7 @@ module.exports = Scale.extend({
return me.getRightValue(chart.data.datasets[datasetIndex].data[index]);
}

return me.ticks[index - me.minIndex];
return me._getLabels()[index];
Copy link
Member

Choose a reason for hiding this comment

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

Is altering of ticks in ticks.callback or afterBuildTicks supported?
me.getTicks()[index].label instead, in that case?

Copy link
Contributor Author

@benmccann benmccann Jul 1, 2019

Choose a reason for hiding this comment

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

I believe it wouldn't be supported because there's no other scale that accesses ticks in getLabelForIndex and this method is only used by the tooltip which a separate concept from ticks. The way we've always suggested customizing the tooltip is via https://www.chartjs.org/docs/latest/configuration/tooltip.html?q=#external-custom-tooltips

},

_configure: function() {
Expand Down