| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- // Test the rectangle element
- describe('Legend block tests', function() {
- describe('auto', jasmine.fixture.specs('plugin.legend'));
- it('should have the correct default config', function() {
- expect(Chart.defaults.global.legend).toEqual({
- display: true,
- position: 'top',
- align: 'center',
- fullWidth: true, // marks that this box should take the full width of the canvas (pushing down other boxes)
- reverse: false,
- weight: 1000,
- // a callback that will handle
- onClick: jasmine.any(Function),
- onHover: null,
- onLeave: null,
- labels: {
- boxWidth: 40,
- padding: 10,
- generateLabels: jasmine.any(Function)
- }
- });
- });
- it('should update bar chart correctly', 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: []
- }, {
- label: 'dataset3',
- borderWidth: 10,
- borderColor: 'green',
- pointStyle: 'crossRot',
- data: []
- }],
- labels: []
- }
- });
- expect(chart.legend.legendItems).toEqual([{
- text: 'dataset1',
- fillStyle: '#f31',
- hidden: false,
- lineCap: undefined,
- lineDash: undefined,
- lineDashOffset: undefined,
- lineJoin: undefined,
- lineWidth: 0,
- 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: undefined,
- lineDash: undefined,
- lineDashOffset: undefined,
- lineJoin: undefined,
- lineWidth: 0,
- strokeStyle: 'rgba(0,0,0,0.1)',
- pointStyle: undefined,
- rotation: undefined,
- datasetIndex: 1
- }, {
- text: 'dataset3',
- fillStyle: 'rgba(0,0,0,0.1)',
- hidden: false,
- lineCap: undefined,
- lineDash: undefined,
- lineDashOffset: undefined,
- lineJoin: undefined,
- lineWidth: 10,
- strokeStyle: 'green',
- 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',
- fillStyle: 'rgba(0,0,0,0)',
- hidden: false,
- lineCap: 'butt',
- lineDash: [],
- lineDashOffset: 0,
- lineJoin: 'miter',
- lineWidth: 10,
- strokeStyle: 'green',
- pointStyle: undefined,
- rotation: undefined,
- datasetIndex: 2
- }]);
- });
- 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: {
- legend: {
- labels: {
- filter: function(legendItem, data) {
- var dataset = data.datasets[legendItem.datasetIndex];
- return !dataset.legendHidden;
- }
- }
- }
- }
- });
- expect(chart.legend.legendItems).toEqual([{
- text: 'dataset1',
- fillStyle: '#f31',
- hidden: false,
- lineCap: undefined,
- lineDash: undefined,
- lineDashOffset: undefined,
- lineJoin: undefined,
- lineWidth: 0,
- strokeStyle: 'rgba(0,0,0,0.1)',
- pointStyle: undefined,
- rotation: undefined,
- datasetIndex: 0
- }, {
- text: 'dataset3',
- fillStyle: 'rgba(0,0,0,0.1)',
- hidden: false,
- lineCap: undefined,
- lineDash: undefined,
- lineDashOffset: undefined,
- lineJoin: undefined,
- lineWidth: 10,
- strokeStyle: 'green',
- pointStyle: undefined,
- rotation: undefined,
- datasetIndex: 2
- }]);
- });
- 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: {
- legend: {
- labels: false,
- }
- }
- });
- };
- expect(makeChart).not.toThrow();
- });
- 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: {
- legend: {
- position: 'right'
- }
- }
- },
- {
- 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);
- });
- });
- 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: {
- legend: {
- labels: {
- usePointStyle: true
- }
- }
- }
- });
- 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
- }]);
- });
- 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: {
- legend: {
- display: true
- }
- }
- });
- expect(chart.legend.options.display).toBe(true);
- chart.options.legend.display = false;
- chart.update();
- expect(chart.legend.options.display).toBe(false);
- });
- it ('should update the associated layout item', function() {
- var chart = acquireChart({
- type: 'line',
- data: {},
- options: {
- legend: {
- fullWidth: true,
- position: 'top',
- weight: 150
- }
- }
- });
- expect(chart.legend.fullWidth).toBe(true);
- expect(chart.legend.position).toBe('top');
- expect(chart.legend.weight).toBe(150);
- chart.options.legend.fullWidth = false;
- chart.options.legend.position = 'left';
- chart.options.legend.weight = 42;
- chart.update();
- expect(chart.legend.fullWidth).toBe(false);
- expect(chart.legend.position).toBe('left');
- expect(chart.legend.weight).toBe(42);
- });
- it ('should remove the legend if the new options are false', function() {
- var chart = acquireChart({
- type: 'line',
- data: {
- labels: ['A', 'B', 'C', 'D'],
- datasets: [{
- data: [10, 20, 30, 100]
- }]
- }
- });
- expect(chart.legend).not.toBe(undefined);
- chart.options.legend = false;
- 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: {
- legend: false
- }
- });
- expect(chart.legend).toBe(undefined);
- chart.options.legend = {};
- chart.update();
- expect(chart.legend).not.toBe(undefined);
- expect(chart.legend.options).toEqual(jasmine.objectContaining(Chart.defaults.global.legend));
- });
- });
- describe('callbacks', function() {
- it('should call onClick, onHover and onLeave at the correct times', function() {
- 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: {
- legend: {
- onClick: function(_, item) {
- clickItem = item;
- },
- onHover: function(_, item) {
- hoverItem = item;
- },
- onLeave: function(_, item) {
- leaveItem = item;
- }
- }
- }
- });
- var hb = chart.legend.legendHitBoxes[0];
- var el = {
- x: hb.left + (hb.width / 2),
- y: hb.top + (hb.height / 2)
- };
- jasmine.triggerMouseEvent(chart, 'click', el);
- expect(clickItem).toBe(chart.legend.legendItems[0]);
- jasmine.triggerMouseEvent(chart, 'mousemove', el);
- expect(hoverItem).toBe(chart.legend.legendItems[0]);
- jasmine.triggerMouseEvent(chart, 'mousemove', chart.getDatasetMeta(0).data[0]);
- expect(leaveItem).toBe(chart.legend.legendItems[0]);
- });
- });
- });
|