it('multiple labels', function() { const points = [{ x: [ 90, 100 ], y: [ 20, 20 ], z: [ '1', '2' ], _origin: { x: [ 90, 100 ], y: [ 20, 20 ], z: [ '1', '2' ] } }, { x: [ 30, 40 ], y: [ 40, 40 ], z: [ '3', '4' ], _origin: { x: [ 30, 40 ], y: [ 40, 40 ], z: [ '3', '4' ] } }]; const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale1 ] }, geomType: 'interval' }); const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(80); expect(first.y).to.equal(points[0].y[0]); const second = items[1]; expect(second.x).to.equal(110); expect(second.y).to.equal(points[0].y[0]); });
it('reset', function() { const count = labelsGroup.getCount(); a.resetLabels(null); expect(labelsGroup.getCount()).to.equal(count); const items = [ { x: 10, y: 20, text: '一' }, { x: 10, y: 40, text: '二' }, { x: 10, y: 60, text: '三' }, { x: 10, y: 80, text: '四' }, { x: 100, y: 100, text: '五', font: '10px Arial', fill: 'red' }, { x: 10, y: 120, text: '六' }, { x: 10, y: 140, text: '' } ]; a.resetLabels(items); expect(labelsGroup.getCount()).to.equal(items.length); canvas.draw(); });
it('init', function() { gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: -10, label: { textAlign: 'left' }, labelLine: true }, scales: [ labelScale1 ] }, geomType: 'interval' }); const cfg = gLabels.get('label'); expect(cfg.offset).to.equal(-10); expect(cfg.textStyle.fill).to.equal('#fff'); });
it('get label items', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale1 ] }, geomType: 'point' }); const items = gLabels.getLabelsItems(points1); expect(items.length).to.equal(points1.length * 2); const first = items[0]; const second = items[1]; expect(first.x).to.equal(points1[0].x); expect(first.y).to.equal(points1[0].y[0] + 10); expect(second.x).to.equal(points1[0].x); expect(second.y).to.equal(points1[0].y[1] - 10); });
describe('one point one label', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale ] }, geomType: 'point' }); it('get default label cfg', function() { const cfg = gLabels.get('label'); expect(cfg.offset).to.equal(10); expect(cfg.textStyle).not.to.equal(undefined); }); it('get label items', function() { const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(points[0].x); expect(first.y).to.equal(points[0].y - 10); }); it('show labels', function() { gLabels.showLabels(points); expect(gLabels.get('labelsGroup').get('children').length).to.equal(points.length); canvas.draw(); }); it('destroy', function() { gLabels.remove(); expect(gLabels.get('destroyed')).to.equal(true); expect(canvas.get('children').length).to.equal(0); }); });
it('show labels', function() { gLabels.showLabels(points); expect(gLabels.get('labelsGroup').get('children').length).to.equal(points.length); canvas.draw(); });
describe('geom labels', function() { describe('labels constructor', function() { it('test default', function() { expect(Labels.getLabelsClass()).to.equal(GeomLabels); expect(Labels.getLabelsClass('rect')).to.equal(GeomLabels); expect(Labels.getLabelsClass('rect', 'line')).to.equal(GeomLabels); }); it('test polar', function() { expect(Labels.getLabelsClass('polar')).to.equal(PolarLabels); }); it('test pie', function() { expect(Labels.getLabelsClass('theta')).to.equal(PieLabels); }); }); const div = document.createElement('div'); div.id = 'gl1'; document.body.appendChild(div); const canvas = new Canvas({ containerId: 'gl1', width: 500, height: 500 }); const coord = new Coord.Cartesian({ start: { x: 0, y: 100 }, end: { x: 100, y: 0 } }); const labelScale = Scale.cat({ field: 'z', values: [ '1', '2' ] }); const points = [ { x: 100, y: 10, z: 0, _origin: { x: 100, y: 10, z: '1' } }, { x: 100, y: 20, z: 1, _origin: { x: 100, y: 20, z: '2' } } ]; const labelScale1 = Scale.cat({ field: 'z', values: [[ '1', '2' ], [ '3', '4' ]] }); const points1 = [ { x: 100, y: [ 10, 20 ], z: [ '1', '2' ], _origin: { x: 100, y: [ 10, 20 ], z: [ '1', '2' ] } }, { x: 100, y: [ 30, 40 ], z: [ '3', '4' ], _origin: { x: 100, y: [ 30, 40 ], z: [ '3', '4' ] } } ]; describe('one point one label', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale ] }, geomType: 'point' }); it('get default label cfg', function() { const cfg = gLabels.get('label'); expect(cfg.offset).to.equal(10); expect(cfg.textStyle).not.to.equal(undefined); }); it('get label items', function() { const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(points[0].x); expect(first.y).to.equal(points[0].y - 10); }); it('show labels', function() { gLabels.showLabels(points); expect(gLabels.get('labelsGroup').get('children').length).to.equal(points.length); canvas.draw(); }); it('destroy', function() { gLabels.remove(); expect(gLabels.get('destroyed')).to.equal(true); expect(canvas.get('children').length).to.equal(0); }); }); describe('one point two labels', function() { it('get label items', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale1 ] }, geomType: 'point' }); const items = gLabels.getLabelsItems(points1); expect(items.length).to.equal(points1.length * 2); const first = items[0]; const second = items[1]; expect(first.x).to.equal(points1[0].x); expect(first.y).to.equal(points1[0].y[0] + 10); expect(second.x).to.equal(points1[0].x); expect(second.y).to.equal(points1[0].y[1] - 10); }); }); describe('one point inner label', function() { let gLabels; it('init', function() { gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: -10 }, scales: [ labelScale ] }, geomType: 'interval' }); const cfg = gLabels.get('label'); // console.log(gLabels, cfg); expect(cfg.offset).to.equal(-10); expect(cfg.textStyle.fill).to.equal('#fff'); }); it('get labels', function() { const items = gLabels.getLabelsItems(points); expect(items.length).to.equal(points.length); const first = items[0]; expect(first.x).to.equal(points[0].x); expect(first.y).to.equal(points[0].y + 10); });/**/ }); describe('two point inner label', function() { let gLabels; it('init', function() { gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: -10, label: { textAlign: 'left' }, labelLine: true }, scales: [ labelScale1 ] }, geomType: 'interval' }); const cfg = gLabels.get('label'); expect(cfg.offset).to.equal(-10); expect(cfg.textStyle.fill).to.equal('#fff'); }); it('get labels', function() { const items = gLabels.getLabelsItems(points1); expect(items.length).to.equal(points1.length * 2); const first = items[0]; const second = items[1]; expect(first.x).to.equal(points1[0].x); expect(first.y).to.equal(points1[0].y[0] - 10); expect(first.textAlign).to.equal('left'); expect(second.x).to.equal(points1[0].x); expect(second.y).to.equal(points1[0].y[1] + 10); expect(second.textAlign).to.equal('left'); }); }); describe('stack points', function() { const scale = Scale.cat({ field: 'text', values: [ 'a', 'b' ] }); const points = [ { x: 0, y: 10, text: 'a', _origin: { x: 0, y: 10, text: 'a' } }, { x: 0, y: [ 10, 20 ], text: 'b', _origin: { x: 0, y: [ 10, 20 ], text: 'b' } } ]; let gLabels; it('init', function() { gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10, label: { textAlign: 'right' }, labelLine: true }, scales: [ scale ] }, geomType: 'interval' }); const cfg = gLabels.get('label'); expect(cfg.offset).to.equal(10); }); it('get labels', function() { const items = gLabels.getLabelsItems(points); expect(items.length).to.equal(points.length); const first = items[0]; const second = items[1]; expect(first.x).to.equal(points[0].x); expect(first.y).to.equal(points[0].y - 10); expect(first.textAlign).to.equal('right'); expect(second.x).to.equal(points[1].x); expect(second.y).to.equal(points[1].y[1] - 10); expect(second.textAlign).to.equal('right'); }); }); describe('transposed label', function() { const coord = new Coord.Cartesian({ start: { x: 0, y: 100 }, end: { x: 100, y: 0 } }); coord.transpose(); it('offset > 0', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale ] }, geomType: 'interval' }); const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(points[0].x + 10); expect(first.y).to.equal(points[0].y); }); it('offset = 0', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 0 }, scales: [ labelScale ] }, geomType: 'interval' }); const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(points[0].x); expect(first.y).to.equal(points[0].y); }); it('offset < 0', function() { const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: -10 }, scales: [ labelScale ] }, geomType: 'interval' }); const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(points[0].x - 10); expect(first.y).to.equal(points[0].y); }); it('multiple labels', function() { const points = [{ x: [ 90, 100 ], y: [ 20, 20 ], z: [ '1', '2' ], _origin: { x: [ 90, 100 ], y: [ 20, 20 ], z: [ '1', '2' ] } }, { x: [ 30, 40 ], y: [ 40, 40 ], z: [ '3', '4' ], _origin: { x: [ 30, 40 ], y: [ 40, 40 ], z: [ '3', '4' ] } }]; const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: 10 }, scales: [ labelScale1 ] }, geomType: 'interval' }); const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(80); expect(first.y).to.equal(points[0].y[0]); const second = items[1]; expect(second.x).to.equal(110); expect(second.y).to.equal(points[0].y[0]); }); it('multiple labels inner', function() { const points = [{ x: [ 90, 100 ], y: [ 20, 20 ], z: [ '1', '2' ], _origin: { x: [ 90, 100 ], y: [ 20, 20 ], z: [ '1', '2' ] } }, { x: [ 30, 40 ], y: [ 40, 40 ], z: [ '3', '4' ], _origin: { x: [ 30, 40 ], y: [ 40, 40 ], z: [ '3', '4' ] } }]; const gLabels = canvas.addGroup(GeomLabels, { coord, labelCfg: { cfg: { offset: -10 }, scales: [ labelScale1 ] }, geomType: 'interval' }); const items = gLabels.getLabelsItems(points); const first = items[0]; expect(first.x).to.equal(100); expect(first.y).to.equal(points[0].y[0]); const second = items[1]; expect(second.x).to.equal(90); expect(second.y).to.equal(points[0].y[0]); }); it('clear', function() { div.parentNode.removeChild(div); }); }); });
describe('LabelsRenderer mixin', function() { class A extends Group { getDefaultCfg() { return { label: null }; } } assign(A.prototype, LabelsRenderer); const canvas = new Canvas({ containerId: 'c1', width: 500, height: 500 }); const items = [ { x: 10, y: 20, text: '1' }, { x: 10, y: 40, text: '2' }, { x: 10, y: 60, text: '3' }, { x: 10, y: 80, text: '4' }, { x: 10, y: 100, text: '5' }, { x: 10, y: 120, text: '6' }, { x: 10, y: 140, text: '7' }, { x: 10, y: 160, text: '8' } ]; const a = canvas.addGroup(A, { label: { items, textStyle: { font: '20px/1.5 "Helvetica Neue",Helvetica,Arial,sans-serif', fill: '#333', rotate: 90 } } }); a.renderLabels(); canvas.draw(); const labelsGroup = a.get('labelsGroup'); it('create', function() { expect(labelsGroup).not.to.be.undefined; expect(labelsGroup.getCount()).to.equal(items.length); }); it('测试防御分支', function() { const a1 = canvas.addGroup(A, { label: null }); a1.renderLabels(); a1.resetLabels(); }); it('reset', function() { const count = labelsGroup.getCount(); a.resetLabels(null); expect(labelsGroup.getCount()).to.equal(count); const items = [ { x: 10, y: 20, text: '一' }, { x: 10, y: 40, text: '二' }, { x: 10, y: 60, text: '三' }, { x: 10, y: 80, text: '四' }, { x: 100, y: 100, text: '五', font: '10px Arial', fill: 'red' }, { x: 10, y: 120, text: '六' }, { x: 10, y: 140, text: '' } ]; a.resetLabels(items); expect(labelsGroup.getCount()).to.equal(items.length); canvas.draw(); }); it('reset back', function() { const items = [ { x: 10, y: 20, text: '1' }, { x: 10, y: 40, text: '2' }, { x: 10, y: 60, text: '3' }, { x: 10, y: 80, text: '4' }, { x: 10, y: 100, text: '5', font: '10px Arial', fill: 'red' }, { x: 10, y: 120, text: '6' }, { x: 10, y: 140, text: '7' }, { x: 10, y: 160, text: '8' } ]; a.resetLabels(items); expect(labelsGroup.getCount()).to.equal(items.length); canvas.draw(); }); it('change add', function() { const count = labelsGroup.getCount(); const item = { x: 10, y: 183, rotate: 10 }; a.addLabel('新的', item); expect(labelsGroup.getCount()).to.equal(count + 1); canvas.draw(); }); it('remove', function() { a.removeLabels(); expect(a.get('labelsGroup')).to.be.null; canvas.draw(); }); });
it('默认', function() { const items = []; for (let i = 0; i < 5; i++) { items.push({ value: 'test ' + i, attrValue: colors[i], marker: i !== 2 ? { symbol: symbols[i], radius: 5, fill: colors[i] } : null, checked: i === 2// 选中状态 }); } const legend = canvas.addGroup(Legend, { items, itemGap: 10, title: { fill: '#f80', fontSize: 12, textAlign: 'start', textBaseline: 'top', text: '水平图例' } }); canvas.draw(); const itemsGroup = legend.get('itemsGroup'); expect(legend.get('children')[0].get('type')).to.equal('rect'); expect(legend.getCount()).to.equal(3); expect(itemsGroup.getCount()).to.equal(5); expect(legend._wrap__onClick).to.be.an.instanceof(Function); // expect(legend._wrap__onMousemove).to.be.an.instanceof(Function); // 点击事件测试1:不允许全部取消选中并且当前只有一个图例项被选中 const targetItem = itemsGroup.get('children')[2]; const event1 = new Event('click', { clientX: 100, clientY: 316 }, true, true); event1.currentTarget = targetItem.get('children')[0]; expect(targetItem.get('checked')).to.be.true; legend.trigger('click', [ event1 ]); expect(itemsGroup.get('children')[0].get('checked')).to.be.false; expect(itemsGroup.get('children')[1].get('checked')).to.be.false; expect(itemsGroup.get('children')[2].get('checked')).to.be.true; expect(itemsGroup.get('children')[3].get('checked')).to.be.false; expect(itemsGroup.get('children')[4].get('checked')).to.be.false; // 点击事件测试2:不允许全部取消选中并且当前只有一个图例项被选中 const event2 = new Event('click', { clientX: 100, clientY: 316 }, true, true); event2.currentTarget = itemsGroup.get('children')[0].get('children')[2]; expect(targetItem.get('checked')).to.be.true; legend.trigger('click', [ event2 ]); expect(itemsGroup.get('children')[0].get('checked')).to.be.true; expect(itemsGroup.get('children')[1].get('checked')).to.be.false; expect(itemsGroup.get('children')[2].get('checked')).to.be.true; expect(itemsGroup.get('children')[3].get('checked')).to.be.false; expect(itemsGroup.get('children')[4].get('checked')).to.be.false; });
it('垂直布局图例', function() { const items = []; for (let i = 0; i < 5; i++) { items.push({ value: 'test ' + i, attrValue: colors[i], marker: { symbol: symbols[i], radius: 5, fill: colors[i] }, checked: !(i === 3) }); } const legend = canvas.addGroup(Legend, { items, allowAllCanceled: true, itemGap: 15, layout: 'vertical', title: { fill: '#f80', fontSize: 14, textAlign: 'start', textBaseline: 'middle', text: '垂直图例' }, unCheckStyle: { fill: '#ccc', fontWeight: 'bold' }, background: { fill: '#ccc', fillOpacity: 0.2 }, textStyle: { fill: '#000' } }); legend.move(0, 150); canvas.draw(); expect(legend.getCount()).to.equal(3); const itemsGroup = legend.get('itemsGroup'); // expect(Util.snapEqual(itemsGroup.getBBox().width, 50.34765625)).to.be.true; expect(itemsGroup.getCount()).to.equal(5); const children = itemsGroup.get('children'); expect(children[0].get('children')[0].attr('fill')).to.equal('#ff6600'); expect(children[0].get('children')[1].attr('fill')).to.equal('#000'); expect(children[3].get('children')[0].attr('fill')).to.equal('#ccc'); expect(children[3].get('children')[1].attr('fill')).to.equal('#ccc'); // 有效点击事件测试 const event = new Event('click', { clientX: 100, clientY: 316 }, true, true); event.currentTarget = children[0].get('children')[0]; legend.trigger('click', [ event ]); expect(children[0].get('children')[0].attr('fill')).to.equal('#ccc'); expect(children[0].get('checked')).to.be.false; });
it('默认,只可单次点击。', function() { const items = []; for (let i = 0; i < 5; i++) { items.push({ value: 'test ' + i, attrValue: colors[i], marker: { symbol: symbols[i], radius: 5, fill: colors[i] }, checked: i === 2 }); } const legend = canvas.addGroup(Legend, { items, allowAllCanceled: true, itemGap: 10, title: { fill: '#f80', fontSize: 12, textAlign: 'start', textBaseline: 'top', text: '水平图例' }, selectedMode: 'single' }); legend.id = '3'; legend.move(0, 100); canvas.draw(); const itemGroups = legend.get('itemsGroup').get('children'); expect(itemGroups[0].get('checked')).to.be.false; expect(itemGroups[1].get('checked')).to.be.false; expect(itemGroups[2].get('checked')).to.be.true; expect(itemGroups[3].get('checked')).to.be.false; expect(itemGroups[4].get('checked')).to.be.false; // 无效点击事件 const unusedEvent = new Event('click', { clientX: 100, clientY: 316 }, true, true); unusedEvent.currentTarget = legend.get('children')[0]; legend.trigger('click', [ unusedEvent ]); expect(itemGroups[0].get('checked')).to.be.false; expect(itemGroups[1].get('checked')).to.be.false; expect(itemGroups[2].get('checked')).to.be.true; expect(itemGroups[3].get('checked')).to.be.false; expect(itemGroups[4].get('checked')).to.be.false; // 有效点击事件测试 const event = new Event('click', { clientX: 100, clientY: 316 }, true, true); event.currentTarget = itemGroups[1].get('children')[0]; legend.trigger('click', [ event ]); expect(itemGroups[0].get('checked')).to.be.false; expect(itemGroups[1].get('checked')).to.be.true; expect(itemGroups[2].get('checked')).to.be.false; expect(itemGroups[3].get('checked')).to.be.false; expect(itemGroups[4].get('checked')).to.be.false; });
describe('Helix 螺旋坐标轴', function() { const xAxis = canvas.addGroup(HelixAxis, { center: { x: 260, y: 260 }, line: { lineWidth: 1, stroke: '#C0D0E0' }, ticks: [ 0, 60, 120, 180, 240 ], tickLine: { lineWidth: 1, length: 10, stroke: '#C0D0E0' }, label: { textStyle: { fill: '#444' }, offset: 30 }, grid: { line: { lineWidth: 1, stroke: '#C0D0E0' }, items: [ { _id: 'test1', points: [{ x: 260, y: 260 }, { x: 260, y: 60 }] }, { _id: 'test2', points: [{ x: 260, y: 260 }, { x: 460, y: 260 }] }, { _id: 'test3', points: [{ x: 260, y: 260 }, { x: 260, y: 460 }] }, { _id: 'test4', points: [{ x: 260, y: 260 }, { x: 60, y: 260 }] } ] }, a: coord.a, crp: (function() { const index = 100; const crp = []; for (let i = 0; i <= index; i++) { const point = coord.convertPoint({ x: i / 100, y: 0 }); crp.push(point.x); crp.push(point.y); } return crp; })(), axisStart: coord.convertPoint({ x: 0, y: 0 }) }); canvas.draw(); it('测试坐标轴生成', function() { expect(xAxis).not.to.be.undefined; expect(xAxis.get('type')).to.equal('helix'); }); it('测试线', function() { const lineShape = xAxis.get('lineShape'); expect(lineShape).not.to.be.undefined; expect(lineShape.attr('path').length).not.to.equal(0); }); it('测试 ticks', function() { const ticks = xAxis.get('ticks'); expect(ticks.length).to.equal(5); }); it('测试 labels', function() { const labelsGroup = xAxis.get('labelsGroup'); expect(labelsGroup).not.to.null; expect(labelsGroup.getCount()).to.equal(5); }); it('测试栅格', function() { const gridGroup = xAxis.get('gridGroup'); expect(gridGroup).not.to.be.undefined; expect(gridGroup.getCount()).to.equal(4); }); });
it('remove', function() { a.removeLabels(); expect(a.get('labelsGroup')).to.be.null; canvas.draw(); });
it('destroy', function() { gLabels.remove(); expect(gLabels.get('destroyed')).to.equal(true); expect(canvas.get('children').length).to.equal(0); });
it('html 渲染图例,使用默认的模板', function() { canvas.clear(); const items = []; for (let i = 0; i < 5; i++) { items.push({ value: 'test ' + i, attrValue: colors[i % 10], marker: { symbol: symbols[i % 5], radius: 5, fill: colors[i % 10] }, checked: !(i > 2) }); } const legend = canvas.addGroup(Legend, { items, title: { text: '图例标题' }, useHtml: true, scroll: false }); legend.move(0, 0); canvas.draw(); const legendDom = div.getElementsByClassName('g2-legend')[0]; expect(legendDom).not.to.be.undefined; expect(legendDom.style.position).to.equal('absolute'); const legendItem = div.getElementsByClassName('g2-legend-list-item')[1]; expect(legendItem.className).to.equal('g2-legend-list-item item-1 checked'); // 模拟点击事件 const event = new MouseEvent('click', { view: window, bubbles: true, cancelable: true }); legendItem.dispatchEvent(event); expect(legendItem.className).to.equal('g2-legend-list-item item-1 unChecked'); let count = 0; legend.on('itemhover', function() { count = 1; }); // 模拟 hover 事件 const hoverEvent = new MouseEvent('mousemove', { view: window, bubbles: true, cancelable: true }); legendItem.dispatchEvent(hoverEvent); expect(count).to.equal(0); const hoveredLegendItem = div.getElementsByClassName('g2-legend-list-item')[2]; hoveredLegendItem.dispatchEvent(hoverEvent); expect(count).to.equal(1); div.removeChild(legendDom); });
it('clear', function() { canvas.destroy(); });
it('clear', function() { canvas.destroy(); document.body.removeChild(div); });