2016-02-15 15:23:31 +01:00
|
|
|
// Test the rectangle element
|
|
|
|
describe('Legend block tests', function() {
|
2019-03-23 10:25:17 +01:00
|
|
|
describe('auto', jasmine.fixture.specs('plugin.legend'));
|
|
|
|
|
2016-02-15 15:23:31 +01:00
|
|
|
it('should have the correct default config', function() {
|
2020-07-13 15:41:42 +02:00
|
|
|
expect(Chart.defaults.plugins.legend).toEqual({
|
2016-02-15 15:23:31 +01:00
|
|
|
display: true,
|
|
|
|
position: 'top',
|
2019-03-23 10:25:17 +01:00
|
|
|
align: 'center',
|
2016-02-15 15:23:31 +01:00
|
|
|
fullWidth: true, // marks that this box should take the full width of the canvas (pushing down other boxes)
|
2016-02-17 20:42:09 +01:00
|
|
|
reverse: false,
|
2017-04-23 19:58:11 +02:00
|
|
|
weight: 1000,
|
2016-02-15 15:23:31 +01:00
|
|
|
|
|
|
|
// a callback that will handle
|
|
|
|
onClick: jasmine.any(Function),
|
2016-09-07 16:59:33 +02:00
|
|
|
onHover: null,
|
2019-02-24 10:58:22 +01:00
|
|
|
onLeave: null,
|
2016-02-15 15:23:31 +01:00
|
|
|
|
|
|
|
labels: {
|
2020-12-14 22:08:40 +01:00
|
|
|
color: Chart.defaults.color,
|
2016-02-15 15:23:31 +01:00
|
|
|
boxWidth: 40,
|
|
|
|
padding: 10,
|
|
|
|
generateLabels: jasmine.any(Function)
|
2020-01-11 00:28:51 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
title: {
|
2020-12-14 22:08:40 +01:00
|
|
|
color: Chart.defaults.color,
|
2020-01-11 00:28:51 +01:00
|
|
|
display: false,
|
|
|
|
position: 'center',
|
|
|
|
text: '',
|
2016-02-15 15:23:31 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-09 15:33:19 +02:00
|
|
|
it('should update bar chart correctly', function() {
|
2016-04-27 19:42:01 +02:00
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'bar',
|
2016-02-15 15:23:31 +01:00
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [2, 2],
|
2016-04-27 19:42:01 +02:00
|
|
|
borderDashOffset: 5.5,
|
|
|
|
data: []
|
2016-02-15 15:23:31 +01:00
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
hidden: true,
|
|
|
|
borderJoinStyle: 'miter',
|
2016-04-27 19:42:01 +02:00
|
|
|
data: []
|
2016-02-15 15:23:31 +01:00
|
|
|
}, {
|
|
|
|
label: 'dataset3',
|
|
|
|
borderWidth: 10,
|
2016-04-27 19:42:01 +02:00
|
|
|
borderColor: 'green',
|
2016-07-06 04:08:29 +02:00
|
|
|
pointStyle: 'crossRot',
|
2016-04-27 19:42:01 +02:00
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
2016-02-15 15:23:31 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-27 19:42:01 +02:00
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
2016-02-15 15:23:31 +01:00
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: '#f31',
|
2016-04-27 19:42:01 +02:00
|
|
|
hidden: false,
|
2019-05-09 15:33:19 +02:00
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
2016-02-15 15:23:31 +01:00
|
|
|
lineJoin: undefined,
|
2019-05-09 15:33:19 +02:00
|
|
|
lineWidth: 0,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
2016-07-06 04:08:29 +02:00
|
|
|
pointStyle: undefined,
|
2019-05-09 15:33:19 +02:00
|
|
|
rotation: undefined,
|
2016-02-15 15:23:31 +01:00
|
|
|
datasetIndex: 0
|
|
|
|
}, {
|
|
|
|
text: 'dataset2',
|
2019-05-09 15:33:19 +02:00
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
2016-02-15 15:23:31 +01:00
|
|
|
hidden: true,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
2019-05-09 15:33:19 +02:00
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 0,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
2016-07-06 04:08:29 +02:00
|
|
|
pointStyle: undefined,
|
2019-05-09 15:33:19 +02:00
|
|
|
rotation: undefined,
|
2016-02-15 15:23:31 +01:00
|
|
|
datasetIndex: 1
|
|
|
|
}, {
|
|
|
|
text: 'dataset3',
|
2019-05-09 15:33:19 +02:00
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
2016-04-27 19:42:01 +02:00
|
|
|
hidden: false,
|
2016-02-15 15:23:31 +01:00
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 10,
|
|
|
|
strokeStyle: 'green',
|
2019-05-09 15:33:19 +02:00
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 2
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update line chart correctly', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'round',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
hidden: true,
|
|
|
|
borderJoinStyle: 'round',
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset3',
|
|
|
|
borderWidth: 10,
|
|
|
|
borderColor: 'green',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
fill: false,
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: 'round',
|
|
|
|
lineDash: [2, 2],
|
|
|
|
lineDashOffset: 5.5,
|
|
|
|
lineJoin: 'miter',
|
|
|
|
lineWidth: 3,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}, {
|
|
|
|
text: 'dataset2',
|
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
hidden: true,
|
|
|
|
lineCap: 'butt',
|
|
|
|
lineDash: [],
|
|
|
|
lineDashOffset: 0,
|
|
|
|
lineJoin: 'round',
|
|
|
|
lineWidth: 3,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 1
|
|
|
|
}, {
|
|
|
|
text: 'dataset3',
|
2019-12-27 23:13:24 +01:00
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
2019-05-09 15:33:19 +02:00
|
|
|
hidden: false,
|
|
|
|
lineCap: 'butt',
|
|
|
|
lineDash: [],
|
|
|
|
lineDashOffset: 0,
|
|
|
|
lineJoin: 'miter',
|
|
|
|
lineWidth: 10,
|
|
|
|
strokeStyle: 'green',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
2016-02-15 15:23:31 +01:00
|
|
|
datasetIndex: 2
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2019-10-23 01:14:54 +02:00
|
|
|
it('should reverse correctly', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'round',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
hidden: true,
|
|
|
|
borderJoinStyle: 'round',
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset3',
|
|
|
|
borderWidth: 10,
|
|
|
|
borderColor: 'green',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
fill: false,
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
reverse: true
|
|
|
|
}
|
2019-10-23 01:14:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset3',
|
2019-12-27 23:13:24 +01:00
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
2019-10-23 01:14:54 +02:00
|
|
|
hidden: false,
|
|
|
|
lineCap: 'butt',
|
|
|
|
lineDash: [],
|
|
|
|
lineDashOffset: 0,
|
|
|
|
lineJoin: 'miter',
|
|
|
|
lineWidth: 10,
|
|
|
|
strokeStyle: 'green',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 2
|
|
|
|
}, {
|
|
|
|
text: 'dataset2',
|
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
hidden: true,
|
|
|
|
lineCap: 'butt',
|
|
|
|
lineDash: [],
|
|
|
|
lineDashOffset: 0,
|
|
|
|
lineJoin: 'round',
|
|
|
|
lineWidth: 3,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 1
|
|
|
|
}, {
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: 'round',
|
|
|
|
lineDash: [2, 2],
|
|
|
|
lineDashOffset: 5.5,
|
|
|
|
lineJoin: 'miter',
|
|
|
|
lineWidth: 3,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2016-11-08 03:46:31 +01:00
|
|
|
it('should filter items', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
hidden: true,
|
|
|
|
borderJoinStyle: 'miter',
|
|
|
|
data: [],
|
|
|
|
legendHidden: true
|
|
|
|
}, {
|
|
|
|
label: 'dataset3',
|
|
|
|
borderWidth: 10,
|
|
|
|
borderColor: 'green',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
filter: function(legendItem, data) {
|
|
|
|
var dataset = data.datasets[legendItem.datasetIndex];
|
|
|
|
return !dataset.legendHidden;
|
|
|
|
}
|
2016-11-08 03:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
2019-05-09 15:33:19 +02:00
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
2016-11-08 03:46:31 +01:00
|
|
|
lineJoin: undefined,
|
2019-05-09 15:33:19 +02:00
|
|
|
lineWidth: 0,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
2016-11-08 03:46:31 +01:00
|
|
|
pointStyle: undefined,
|
2019-05-09 15:33:19 +02:00
|
|
|
rotation: undefined,
|
2016-11-08 03:46:31 +01:00
|
|
|
datasetIndex: 0
|
|
|
|
}, {
|
|
|
|
text: 'dataset3',
|
2019-05-09 15:33:19 +02:00
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
2016-11-08 03:46:31 +01:00
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 10,
|
|
|
|
strokeStyle: 'green',
|
2019-05-09 15:33:19 +02:00
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
2016-11-08 03:46:31 +01:00
|
|
|
datasetIndex: 2
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2020-10-06 04:15:38 +02:00
|
|
|
it('should sort items', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'round',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
hidden: true,
|
|
|
|
borderJoinStyle: 'round',
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset3',
|
|
|
|
borderWidth: 10,
|
|
|
|
borderColor: 'green',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
fill: false,
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
sort: function(a, b) {
|
|
|
|
return b.datasetIndex > a.datasetIndex ? 1 : -1;
|
|
|
|
}
|
2020-10-06 04:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset3',
|
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: 'butt',
|
|
|
|
lineDash: [],
|
|
|
|
lineDashOffset: 0,
|
|
|
|
lineJoin: 'miter',
|
|
|
|
lineWidth: 10,
|
|
|
|
strokeStyle: 'green',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 2
|
|
|
|
}, {
|
|
|
|
text: 'dataset2',
|
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
hidden: true,
|
|
|
|
lineCap: 'butt',
|
|
|
|
lineDash: [],
|
|
|
|
lineDashOffset: 0,
|
|
|
|
lineJoin: 'round',
|
|
|
|
lineWidth: 3,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 1
|
|
|
|
}, {
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: 'round',
|
|
|
|
lineDash: [2, 2],
|
|
|
|
lineDashOffset: 5.5,
|
|
|
|
lineJoin: 'miter',
|
|
|
|
lineWidth: 3,
|
|
|
|
strokeStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2017-06-24 17:35:46 +02:00
|
|
|
it('should not throw when the label options are missing', function() {
|
|
|
|
var makeChart = function() {
|
|
|
|
window.acquireChart({
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: false,
|
|
|
|
}
|
2017-06-24 17:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
expect(makeChart).not.toThrow();
|
|
|
|
});
|
|
|
|
|
2018-11-14 11:12:57 +01:00
|
|
|
it('should not draw legend items outside of the chart bounds', function() {
|
|
|
|
var chart = window.acquireChart(
|
|
|
|
{
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [1, 2, 3].map(function(n) {
|
|
|
|
return {
|
|
|
|
label: 'dataset' + n,
|
|
|
|
data: []
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
position: 'right'
|
|
|
|
}
|
2018-11-14 11:12:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
canvas: {
|
|
|
|
width: 512,
|
|
|
|
height: 105
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check some basic assertions about the test setup
|
|
|
|
expect(chart.width).toBe(512);
|
|
|
|
expect(chart.legend.legendHitBoxes.length).toBe(3);
|
|
|
|
|
|
|
|
// Check whether any legend items reach outside the established bounds
|
|
|
|
chart.legend.legendHitBoxes.forEach(function(item) {
|
|
|
|
expect(item.left + item.width).toBeLessThanOrEqual(chart.width);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-05 01:53:40 +02:00
|
|
|
it('should draw items with a custom boxHeight', function() {
|
|
|
|
var chart = window.acquireChart(
|
|
|
|
{
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
position: 'right',
|
|
|
|
labels: {
|
|
|
|
boxHeight: 40
|
|
|
|
}
|
2020-06-05 01:53:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
canvas: {
|
|
|
|
width: 512,
|
|
|
|
height: 105
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const hitBox = chart.legend.legendHitBoxes[0];
|
|
|
|
expect(hitBox.height).toBe(40);
|
|
|
|
});
|
|
|
|
|
2019-05-09 15:33:19 +02:00
|
|
|
it('should pick up the first item when the property is an array', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: ['#f31', '#666', '#14e'],
|
|
|
|
borderWidth: [5, 10, 15],
|
|
|
|
borderColor: ['red', 'green', 'blue'],
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 5,
|
|
|
|
strokeStyle: 'red',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the value for the first item when the property is a function', function() {
|
|
|
|
var helpers = window.Chart.helpers;
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: function(ctx) {
|
|
|
|
var value = ctx.dataset.data[ctx.dataIndex] || 0;
|
|
|
|
return helpers.color({r: value * 10, g: 0, b: 0}).rgbString();
|
|
|
|
},
|
|
|
|
borderWidth: function(ctx) {
|
|
|
|
var value = ctx.dataset.data[ctx.dataIndex] || 0;
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
borderColor: function(ctx) {
|
|
|
|
var value = ctx.dataset.data[ctx.dataIndex] || 0;
|
|
|
|
return helpers.color({r: 255 - value * 10, g: 0, b: 0}).rgbString();
|
|
|
|
},
|
|
|
|
data: [5, 10, 15, 20]
|
|
|
|
}],
|
|
|
|
labels: ['A', 'B', 'C', 'D']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: 'rgb(50, 0, 0)',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 5,
|
|
|
|
strokeStyle: 'rgb(205, 0, 0)',
|
|
|
|
pointStyle: undefined,
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should draw correctly when usePointStyle is true', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
borderWidth: 0,
|
|
|
|
borderColor: '#f31',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
pointBackgroundColor: 'rgba(0,0,0,0.1)',
|
|
|
|
pointBorderWidth: 5,
|
|
|
|
pointBorderColor: 'green',
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderJoinStyle: 'miter',
|
|
|
|
borderWidth: 2,
|
|
|
|
borderColor: '#f31',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
pointRotation: 15,
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
usePointStyle: true
|
|
|
|
}
|
2019-05-09 15:33:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 5,
|
|
|
|
strokeStyle: 'green',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}, {
|
|
|
|
text: 'dataset2',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 2,
|
|
|
|
strokeStyle: '#f31',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
rotation: 15,
|
|
|
|
datasetIndex: 1
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2020-10-26 13:16:00 +01:00
|
|
|
it('should draw correctly when usePointStyle is true and pointStyle override is set', function() {
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [2, 2],
|
|
|
|
borderDashOffset: 5.5,
|
|
|
|
borderWidth: 0,
|
|
|
|
borderColor: '#f31',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
pointBackgroundColor: 'rgba(0,0,0,0.1)',
|
|
|
|
pointBorderWidth: 5,
|
|
|
|
pointBorderColor: 'green',
|
|
|
|
data: []
|
|
|
|
}, {
|
|
|
|
label: 'dataset2',
|
|
|
|
backgroundColor: '#f31',
|
|
|
|
borderJoinStyle: 'miter',
|
|
|
|
borderWidth: 2,
|
|
|
|
borderColor: '#f31',
|
|
|
|
pointStyle: 'crossRot',
|
|
|
|
pointRotation: 15,
|
|
|
|
data: []
|
|
|
|
}],
|
|
|
|
labels: []
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
labels: {
|
|
|
|
usePointStyle: true,
|
|
|
|
pointStyle: 'star'
|
|
|
|
}
|
2020-10-26 13:16:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.legendItems).toEqual([{
|
|
|
|
text: 'dataset1',
|
|
|
|
fillStyle: 'rgba(0,0,0,0.1)',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 5,
|
|
|
|
strokeStyle: 'green',
|
|
|
|
pointStyle: 'star',
|
|
|
|
rotation: undefined,
|
|
|
|
datasetIndex: 0
|
|
|
|
}, {
|
|
|
|
text: 'dataset2',
|
|
|
|
fillStyle: '#f31',
|
|
|
|
hidden: false,
|
|
|
|
lineCap: undefined,
|
|
|
|
lineDash: undefined,
|
|
|
|
lineDashOffset: undefined,
|
|
|
|
lineJoin: undefined,
|
|
|
|
lineWidth: 2,
|
|
|
|
strokeStyle: '#f31',
|
|
|
|
pointStyle: 'star',
|
|
|
|
rotation: 15,
|
|
|
|
datasetIndex: 1
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2020-12-14 11:04:10 +01:00
|
|
|
it('should not crash when the legend defaults are false', function() {
|
|
|
|
const oldDefaults = Chart.defaults.plugins.legend;
|
|
|
|
|
|
|
|
Chart.defaults.set({
|
|
|
|
plugins: {
|
|
|
|
legend: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var chart = window.acquireChart({
|
|
|
|
type: 'doughnut',
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: 'dataset1',
|
|
|
|
data: [1, 2, 3, 4]
|
|
|
|
}],
|
|
|
|
labels: ['', '', '', '']
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(chart).toBeDefined();
|
|
|
|
|
|
|
|
Chart.defaults.set({
|
|
|
|
plugins: {
|
|
|
|
legend: oldDefaults,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-13 04:38:25 +01:00
|
|
|
describe('config update', function() {
|
|
|
|
it ('should update the options', function() {
|
|
|
|
var chart = acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: ['A', 'B', 'C', 'D'],
|
|
|
|
datasets: [{
|
|
|
|
data: [10, 20, 30, 100]
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
display: true
|
|
|
|
}
|
2016-11-13 04:38:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(chart.legend.options.display).toBe(true);
|
|
|
|
|
2020-11-25 07:50:12 +01:00
|
|
|
chart.options.plugins.legend.display = false;
|
2016-11-13 04:38:25 +01:00
|
|
|
chart.update();
|
|
|
|
expect(chart.legend.options.display).toBe(false);
|
|
|
|
});
|
|
|
|
|
2017-04-23 19:58:11 +02:00
|
|
|
it ('should update the associated layout item', function() {
|
|
|
|
var chart = acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
fullWidth: true,
|
|
|
|
position: 'top',
|
|
|
|
weight: 150
|
|
|
|
}
|
2017-04-23 19:58:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(chart.legend.fullWidth).toBe(true);
|
|
|
|
expect(chart.legend.position).toBe('top');
|
|
|
|
expect(chart.legend.weight).toBe(150);
|
|
|
|
|
2020-11-25 07:50:12 +01:00
|
|
|
chart.options.plugins.legend.fullWidth = false;
|
|
|
|
chart.options.plugins.legend.position = 'left';
|
|
|
|
chart.options.plugins.legend.weight = 42;
|
2017-04-23 19:58:11 +02:00
|
|
|
chart.update();
|
|
|
|
|
|
|
|
expect(chart.legend.fullWidth).toBe(false);
|
|
|
|
expect(chart.legend.position).toBe('left');
|
|
|
|
expect(chart.legend.weight).toBe(42);
|
|
|
|
});
|
|
|
|
|
2020-11-28 21:57:45 +01:00
|
|
|
it ('should remove the legend if the new options are false', function() {
|
2016-11-13 04:38:25 +01:00
|
|
|
var chart = acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: ['A', 'B', 'C', 'D'],
|
|
|
|
datasets: [{
|
|
|
|
data: [10, 20, 30, 100]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(chart.legend).not.toBe(undefined);
|
|
|
|
|
2020-11-25 07:50:12 +01:00
|
|
|
chart.options.plugins.legend = false;
|
2016-11-13 04:38:25 +01:00
|
|
|
chart.update();
|
|
|
|
expect(chart.legend).toBe(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should create the legend if the legend options are changed to exist', function() {
|
|
|
|
var chart = acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: ['A', 'B', 'C', 'D'],
|
|
|
|
datasets: [{
|
|
|
|
data: [10, 20, 30, 100]
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: false
|
|
|
|
}
|
2016-11-13 04:38:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(chart.legend).toBe(undefined);
|
|
|
|
|
2020-11-25 07:50:12 +01:00
|
|
|
chart.options.plugins.legend = {};
|
2016-11-13 04:38:25 +01:00
|
|
|
chart.update();
|
|
|
|
expect(chart.legend).not.toBe(undefined);
|
2020-07-13 15:41:42 +02:00
|
|
|
expect(chart.legend.options).toEqual(jasmine.objectContaining(Chart.defaults.plugins.legend));
|
2016-11-13 04:38:25 +01:00
|
|
|
});
|
|
|
|
});
|
2019-02-24 10:58:22 +01:00
|
|
|
|
|
|
|
describe('callbacks', function() {
|
2020-01-14 13:34:08 +01:00
|
|
|
it('should call onClick, onHover and onLeave at the correct times', function(done) {
|
2019-02-24 10:58:22 +01:00
|
|
|
var clickItem = null;
|
|
|
|
var hoverItem = null;
|
|
|
|
var leaveItem = null;
|
|
|
|
|
|
|
|
var chart = acquireChart({
|
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: ['A', 'B', 'C', 'D'],
|
|
|
|
datasets: [{
|
|
|
|
data: [10, 20, 30, 100]
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
legend: {
|
|
|
|
onClick: function(_, item) {
|
|
|
|
clickItem = item;
|
|
|
|
},
|
|
|
|
onHover: function(_, item) {
|
|
|
|
hoverItem = item;
|
|
|
|
},
|
|
|
|
onLeave: function(_, item) {
|
|
|
|
leaveItem = item;
|
|
|
|
}
|
2019-02-24 10:58:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var hb = chart.legend.legendHitBoxes[0];
|
|
|
|
var el = {
|
|
|
|
x: hb.left + (hb.width / 2),
|
|
|
|
y: hb.top + (hb.height / 2)
|
|
|
|
};
|
|
|
|
|
2020-01-14 13:34:08 +01:00
|
|
|
afterEvent(chart, 'click', function() {
|
|
|
|
expect(clickItem).toBe(chart.legend.legendItems[0]);
|
2019-02-24 10:58:22 +01:00
|
|
|
|
2020-01-14 13:34:08 +01:00
|
|
|
afterEvent(chart, 'mousemove', function() {
|
|
|
|
expect(hoverItem).toBe(chart.legend.legendItems[0]);
|
2019-02-24 10:58:22 +01:00
|
|
|
|
2020-01-14 13:34:08 +01:00
|
|
|
afterEvent(chart, 'mousemove', function() {
|
|
|
|
expect(leaveItem).toBe(chart.legend.legendItems[0]);
|
2019-02-24 10:58:22 +01:00
|
|
|
|
2020-01-14 13:34:08 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
jasmine.triggerMouseEvent(chart, 'mousemove', chart.getDatasetMeta(0).data[0]);
|
|
|
|
});
|
|
|
|
jasmine.triggerMouseEvent(chart, 'mousemove', el);
|
|
|
|
});
|
|
|
|
jasmine.triggerMouseEvent(chart, 'click', el);
|
2019-02-24 10:58:22 +01:00
|
|
|
});
|
|
|
|
});
|
2016-02-17 20:42:09 +01:00
|
|
|
});
|
2019-03-23 10:25:17 +01:00
|
|
|
|