123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- describe('c3 chart tooltip', function () {
- 'use strict';
- var chart;
- var tooltipConfiguration = {};
- var dataOrder = 'desc';
- var dataGroups;
- var args = function () {
- return {
- data: {
- columns: [
- ['data1', 30, 200, 100, 400, 150, 250], // 1130
- ['data2', 50, 20, 10, 40, 15, 25], // 160
- ['data3', 150, 120, 110, 140, 115, 125] // 760
- ],
- order: dataOrder,
- groups: dataGroups,
- },
- tooltip: tooltipConfiguration
- };
- };
- beforeEach(function (done) {
- chart = window.initChart(chart, args(), done);
- dataOrder = 'desc';
- dataGroups = undefined;
- });
- describe('tooltip position', function () {
- beforeAll(function () {
- tooltipConfiguration = {};
- });
- describe('without left margin', function () {
- it('should show tooltip on proper position', function () {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(1),
- y = chart.internal.y(200);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var tooltipContainer = d3.select('.c3-tooltip-container'),
- top = Math.floor(+tooltipContainer.style('top').replace(/px/, '')),
- left = Math.floor(+tooltipContainer.style('left').replace(/px/, ''));
- expect(top).toBeGreaterThan(0);
- expect(left).toBeGreaterThan(0);
- });
- });
- describe('with left margin', function () {
- beforeAll(function () {
- d3.select('#chart').style('margin-left', '300px');
- });
- it('should show tooltip on proper position', function () {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(1) + 300, // add margin-left
- y = chart.internal.y(200);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var tooltipContainer = d3.select('.c3-tooltip-container'),
- top = Math.floor(+tooltipContainer.style('top').replace(/px/, '')),
- left = Math.floor(+tooltipContainer.style('left').replace(/px/, ''));
- expect(top).toBeGreaterThan(0);
- expect(left).toBeGreaterThan(0);
- });
- afterAll(function () {
- d3.select('#chart').style('margin-left', null);
- });
- });
- });
- describe('tooltip positionFunction', function () {
- var topExpected = 37, leftExpected = 79;
- beforeAll(function () {
- tooltipConfiguration = {
- position: function (data, width, height, element) {
- expect(data.length).toBe(args().data.columns.length);
- expect(data[0]).toEqual(jasmine.objectContaining({
- index: 2,
- value: 100,
- id: 'data1'
- }));
- expect(width).toBeGreaterThan(0);
- expect(height).toBeGreaterThan(0);
- expect(element).toBe(d3.select('.c3-event-rect').node());
- return {top: topExpected, left: leftExpected};
- }
- };
- });
- it('should be set to the coordinate where the function returned', function () {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var tooltipContainer = d3.select('.c3-tooltip-container'),
- top = Math.floor(+tooltipContainer.style('top').replace(/px/, '')),
- left = Math.floor(+tooltipContainer.style('left').replace(/px/, ''));
- expect(top).toBeGreaterThan(0);
- expect(left).toBeGreaterThan(0);
- });
- });
- describe('tooltip getTooltipContent', function () {
- beforeAll(function () {
- tooltipConfiguration = {
- data_order: 'desc'
- };
- });
- it('should sort values desc', function () {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data3');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data2');
- });
- });
- describe('tooltip with data_order as desc with grouped data', function() {
- beforeAll(function() {
- dataOrder = 'desc';
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each data in descending order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(220);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data1'); // 1130
- expect(classes[2]).toBe('c3-tooltip-name--data3'); // 760
- expect(classes[3]).toBe('c3-tooltip-name--data2'); // 160
- });
- });
- describe('tooltip with data_order as asc with grouped data', function() {
- beforeAll(function() {
- dataOrder = 'asc';
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each data in ascending order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(220);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2'); // 160
- expect(classes[2]).toBe('c3-tooltip-name--data3'); // 760
- expect(classes[3]).toBe('c3-tooltip-name--data1'); // 1130
- });
- });
- describe('tooltip with data_order as NULL with grouped data', function() {
- beforeAll(function() {
- dataOrder = null;
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each data in given order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(220);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data1');
- expect(classes[2]).toBe('c3-tooltip-name--data2');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with data_order as Function with grouped data', function() {
- beforeAll(function() {
- var order = [ 'data2', 'data1', 'data3' ];
- dataOrder = function(data1, data2) {
- return order.indexOf(data1.id) - order.indexOf(data2.id);
- };
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each data in order given by function', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(220);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with data_order as Array with grouped data', function() {
- beforeAll(function() {
- dataOrder = [ 'data2', 'data1', 'data3' ];
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each data in order given by array', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(220);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with data_order as desc with un-grouped data', function() {
- beforeAll(function() {
- dataOrder = 'desc';
- });
- it('should display each tooltip value descending order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data3'); // 110
- expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
- expect(classes[3]).toBe('c3-tooltip-name--data2'); // 10
- });
- });
- describe('tooltip with data_order as asc with un-grouped data', function() {
- beforeAll(function() {
- dataOrder = 'asc';
- });
- it('should display each tooltip value in ascending order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2'); // 10
- expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
- expect(classes[3]).toBe('c3-tooltip-name--data3'); // 110
- });
- });
- describe('tooltip with data_order as NULL with un-grouped data', function() {
- beforeAll(function() {
- dataOrder = null;
- });
- it('should display each tooltip value in given data order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data1');
- expect(classes[2]).toBe('c3-tooltip-name--data2');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with data_order as Function with un-grouped data', function() {
- beforeAll(function() {
- var order = [ 'data2', 'data1', 'data3' ];
- dataOrder = function(data1, data2) {
- return order.indexOf(data1.id) - order.indexOf(data2.id);
- };
- });
- it('should display each tooltip value in data order given by function', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with data_order as Array with un-grouped data', function() {
- beforeAll(function() {
- dataOrder = [ 'data2', 'data1', 'data3' ];
- });
- it('should display each tooltip value in data order given by array', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with tooltip_order as desc', function() {
- beforeAll(function() {
- tooltipConfiguration = {
- order: 'desc'
- };
- // this should be ignored
- dataOrder = 'asc';
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each tooltip value descending order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data3'); // 110
- expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
- expect(classes[3]).toBe('c3-tooltip-name--data2'); // 10
- });
- });
- describe('tooltip with tooltip_order as asc', function() {
- beforeAll(function() {
- tooltipConfiguration = {
- order: 'asc'
- };
- // this should be ignored
- dataOrder = 'desc';
- dataGroups = [ [ 'data1', 'data2', 'data3' ]];
- });
- it('should display each tooltip value in ascending order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(220);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2'); // 10
- expect(classes[2]).toBe('c3-tooltip-name--data1'); // 100
- expect(classes[3]).toBe('c3-tooltip-name--data3'); // 110
- });
- });
- describe('tooltip with tooltip_order as NULL', function() {
- beforeAll(function() {
- tooltipConfiguration = {
- order: null
- };
- });
- it('should display each tooltip value in given order', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data1');
- expect(classes[2]).toBe('c3-tooltip-name--data2');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with tooltip_order as Function', function() {
- beforeAll(function() {
- var order = [ 'data2', 'data1', 'data3' ];
- tooltipConfiguration = {
- order: function(data1, data2) {
- return order.indexOf(data1.id) - order.indexOf(data2.id);
- }
- };
- });
- it('should display each tooltip value in data order given by function', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- describe('tooltip with tooltip_order as Array', function() {
- beforeAll(function() {
- tooltipConfiguration = {
- order: [ 'data2', 'data1', 'data3' ]
- };
- });
- it('should display each tooltip value in data order given by array', function() {
- var eventRect = d3.select('.c3-event-rect').node(),
- x = chart.internal.x(2),
- y = chart.internal.y(100);
- window.setMouseEvent(chart, 'mousemove', x, y, eventRect);
- var classes = d3.selectAll('.c3-tooltip tr').nodes().map(function(node) {
- return node.className;
- });
- expect(classes[0]).toBe(''); // header
- expect(classes[1]).toBe('c3-tooltip-name--data2');
- expect(classes[2]).toBe('c3-tooltip-name--data1');
- expect(classes[3]).toBe('c3-tooltip-name--data3');
- });
- });
- });
|