Skip to content

Commit 2a96d83

Browse files
kurklesimonbrunel
authored andcommitted
Implement layers (z-index) for layout items (#6241)
1 parent 95b9953 commit 2a96d83

File tree

10 files changed

+544
-188
lines changed

10 files changed

+544
-188
lines changed

docs/axes/styling.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The grid line configuration is nested under the scale configuration in the `grid
2323
| `zeroLineBorderDash` | `number[]` | `[]` | Length and spacing of dashes of the grid line for the first index (index 0). See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash).
2424
| `zeroLineBorderDashOffset` | `number` | `0.0` | Offset for line dashes of the grid line for the first index (index 0). See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset).
2525
| `offsetGridLines` | `boolean` | `false` | If true, grid lines will be shifted to be between labels. This is set to `true` for a category scale in a bar chart by default.
26+
| `z` | `number` | `0` | z-index of gridline layer. Values <= 0 are drawn under datasets, > 0 on top.
2627

2728
## Tick Configuration
2829
The tick configuration is nested under the scale configuration in the `ticks` key. It defines options for the tick marks that are generated by the axis.
@@ -40,6 +41,7 @@ The tick configuration is nested under the scale configuration in the `ticks` ke
4041
| `minor` | `object` | `{}` | Minor ticks configuration. Omitted options are inherited from options above.
4142
| `major` | `object` | `{}` | Major ticks configuration. Omitted options are inherited from options above.
4243
| `padding` | `number` | `0` | Sets the offset of the tick labels from the axis
44+
| `z` | `number` | `0` | z-index of tick layer. Useful when ticks are drawn on chart area. Values <= 0 are drawn under datasets, > 0 on top.
4345

4446
## Minor Tick Configuration
4547
The minorTick configuration is nested under the ticks configuration in the `minor` key. It defines options for the minor tick marks that are generated by the axis. Omitted options are inherited from `ticks` configuration.

src/core/core.controller.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
169169
me.aspectRatio = height ? width / height : null;
170170
me.options = config.options;
171171
me._bufferedRender = false;
172+
me._layers = [];
172173

173174
/**
174175
* Provided for backward compatibility, Chart and Chart.Controller have been merged,
@@ -495,6 +496,12 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
495496
// Do this before render so that any plugins that need final scale updates can use it
496497
plugins.notify(me, 'afterUpdate');
497498

499+
me._layers.sort(function(a, b) {
500+
return a.z === b.z
501+
? a._idx - b._idx
502+
: a.z - b.z;
503+
});
504+
498505
if (me._bufferedRender) {
499506
me._bufferedRequest = {
500507
duration: config.duration,
@@ -520,6 +527,15 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
520527

521528
layouts.update(this, this.width, this.height);
522529

530+
me._layers = [];
531+
helpers.each(me.boxes, function(box) {
532+
me._layers.push.apply(me._layers, box._layers());
533+
}, me);
534+
535+
me._layers.forEach(function(item, index) {
536+
item._idx = index;
537+
});
538+
523539
/**
524540
* Provided for backward compatibility, use `afterLayout` instead.
525541
* @method IPlugin#afterScaleUpdate
@@ -626,6 +642,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
626642

627643
draw: function(easingValue) {
628644
var me = this;
645+
var i, layers;
629646

630647
me.clear();
631648

@@ -643,12 +660,21 @@ helpers.extend(Chart.prototype, /** @lends Chart */ {
643660
return;
644661
}
645662

646-
// Draw all the scales
647-
helpers.each(me.boxes, function(box) {
648-
box.draw(me.chartArea);
649-
}, me);
663+
// Because of plugin hooks (before/afterDatasetsDraw), datasets can't
664+
// currently be part of layers. Instead, we draw
665+
// layers <= 0 before(default, backward compat), and the rest after
666+
layers = me._layers;
667+
for (i = 0; i < layers.length && layers[i].z <= 0; ++i) {
668+
layers[i].draw(me.chartArea);
669+
}
650670

651671
me.drawDatasets(easingValue);
672+
673+
// Rest of layers
674+
for (; i < layers.length; ++i) {
675+
layers[i].draw(me.chartArea);
676+
}
677+
652678
me._drawTooltip(easingValue);
653679

654680
plugins.notify(me, 'afterDraw', [easingValue]);

src/core/core.layouts.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ module.exports = {
103103
item.fullWidth = item.fullWidth || false;
104104
item.position = item.position || 'top';
105105
item.weight = item.weight || 0;
106+
item._layers = item._layers || function() {
107+
return [{
108+
z: 0,
109+
draw: function() {
110+
item.draw.apply(item, arguments);
111+
}
112+
}];
113+
};
106114

107115
chart.boxes.push(item);
108116
},

0 commit comments

Comments
 (0)