12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373 |
- describe('c3 chart data', function () {
- 'use strict';
- var chart, args;
- beforeEach(function (done) {
- chart = window.initChart(chart, args, done);
- });
- describe('load json', function () {
- beforeAll(function () {
- args = {
- data: {
- json: {
- data1: [30, 20, 50],
- data2: [200, 130, 90]
- }
- }
- };
- });
- it('should draw correctly', function () {
- var expectedCx = [6, 299, 593],
- expectedCy = [371, 391, 332];
- d3.selectAll('.c3-circles-data1 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[i], 0);
- });
- });
- describe('more data', function(){
- beforeAll(function () {
- args = {
- data: {
- json: [{
- "date": "2014-06-03",
- "443": "3000",
- "995": "500"
- }, {
- "date": "2014-06-04",
- "443": "1000"
- }, {
- "date": "2014-06-05",
- "443": "5000",
- "995": "1000"
- }],
- keys: {
- x: 'date',
- value: [ "443", "995" ]
- }
- },
- axis: {
- x: {
- type: "category"
- }
- }
- };
- });
- it('should draw correctly', function () {
- var expectedCx = {443: [98, 294, 490], 995: [98, 294, 490]},
- expectedCy = {443: [194, 351, 36], 995: [391, 430, 351]};
- d3.selectAll('.c3-circles-443 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[443][i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[443][i], 0);
- });
- d3.selectAll('.c3-circles-995 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[995][i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[995][i], 0);
- });
- });
- });
- describe('with nested JSON args', function () {
- beforeAll(function(){
- args = {
- data: {
- json: [{
- "date": "2014-06-03",
- "443": "3000",
- "995": {"996": "500"},
- "112": ["600"],
- "223": [{"224": "100"}],
- "334": [[],[{"335": "300"}]],
- "556": {"557" : {"558" : ["1000"]}}
- }, {
- "date": "2014-06-04",
- "443": "1000",
- "112": ["700"],
- "223": [{"224": "200"}],
- "556": {"557" : {"558" : ["2000"]}}
- }, {
- "date": "2014-06-05",
- "995": {"996": "1000"},
- "112": ["800"],
- "223": [{"224": "300"}],
- "443": "5000",
- "334": [[],[{"335": "500"}]],
- "556": {"557" : {"558" : ["3000"]}}
- }],
- keys: {
- x: 'date',
- value: [ "443","995.996","112[0]","223[0].224","334[1][0].335","556.557.558[0]"]
- }
- },
- axis: {
- x: {
- type: "category"
- }
- }
- };
- });
- it('should draw nested JSON correctly', function () {
- var expectedCx = [98, 294, 490],
- expectedCy = {
- 443: [181, 326, 36],
- 995: [362, 398, 326],
- 112: [354, 347, 340],
- 223: [391, 383, 376],
- 334: [376, 398, 362],
- 556: [326, 253, 181]
- };
- d3.selectAll('.c3-circles-443 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[443][i], 0);
- });
- d3.selectAll('.c3-circles-995-996 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[995][i], 0);
- });
- d3.selectAll('.c3-circles-112-0- .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[112][i], 0);
- });
- d3.selectAll('.c3-circles-223-0--224 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[223][i], 0);
- });
- d3.selectAll('.c3-circles-334-1--0--335 .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[334][i], 0);
- });
- d3.selectAll('.c3-circles-556-557-558-0- .c3-circle').each(function (d, i) {
- var circle = d3.select(this);
- expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
- expect(+circle.attr('cy')).toBeCloseTo(expectedCy[556][i], 0);
- });
- });
- });
- });
- describe('function in data.order', function () {
- beforeAll(function () {
- args = {
- data: {
- columns: [
- ['data1', 30, 200, 100, 400, 150, 250],
- ['data2', 50, 20, 10, 40, 15, 25],
- ['data3', 150, 120, 110, 140, 115, 125]
- ],
- order: function () {
- return 0;
- }
- }
- };
- });
- it('should return false in isOrderAsc and isOrderDesc functions', function () {
- expect(chart.internal.isOrderAsc() || chart.internal.isOrderDesc()).toBe(false);
- });
- });
- describe('addHiddenTargetIds if not already hidden', function () {
- it('should update args', function () {
- args = {
- data: {
- columns: [
- ['data1', 30, 200, 100, 400, 150, 250],
- ['data2', 150, 120, 110, 140, 115, 125]
- ]
- }
- };
- expect(true).toBeTruthy();
- });
- it('length of hiddenTargetIds should not change if same key added twice', function () {
- chart.internal.addHiddenTargetIds('data1');
- expect(chart.internal.hiddenTargetIds.length).toBe(1);
- chart.internal.addHiddenTargetIds('data1');
- expect(chart.internal.hiddenTargetIds.length).toBe(1);
- chart.hide('data1');
- expect(chart.internal.hiddenTargetIds.length).toBe(1);
- chart.internal.addHiddenTargetIds('data2');
- expect(chart.internal.hiddenTargetIds.length).toBe(2);
- chart.show();
- chart.hide(['data1', 'data2']);
- expect(chart.internal.hiddenTargetIds.length).toBe(2);
- chart.show();
- chart.hide();
- expect(chart.internal.hiddenTargetIds.length).toBe(2);
- });
- });
- describe('addHiddenLegendIds if not already hidden', function () {
- it('should update args', function () {
- args = {
- data: {
- columns: [
- ['data1', 30, 200, 100, 400, 150, 250],
- ['data2', 150, 120, 110, 140, 115, 125]
- ]
- }
- };
- expect(true).toBeTruthy();
- });
- it('length of hiddenLegendIds should not change if same key added twice', function () {
- chart.internal.addHiddenLegendIds('data1');
- expect(chart.internal.hiddenLegendIds.length).toBe(1);
- chart.internal.addHiddenLegendIds('data1');
- expect(chart.internal.hiddenLegendIds.length).toBe(1);
- chart.hide('data1', {withLegend: true});
- expect(chart.internal.hiddenLegendIds.length).toBe(1);
- chart.hide('data2', {withLegend: true});
- expect(chart.internal.hiddenLegendIds.length).toBe(2);
- chart.show(['data1', 'data2'], {withLegend: true});
- chart.hide(['data1', 'data2'], {withLegend: true});
- expect(chart.internal.hiddenLegendIds.length).toBe(2);
- });
- });
- describe('data.xs', function () {
- beforeAll(function () {
- args = {
- data: {
- columns: [
- ['data1', 30, 200, 100, 400, 150, 250],
- ['data2', 50, 20, 10, 40, 15, 25],
- ['data3', 150, 120, 110, 140, 115, 125]
- ],
- }
- };
- });
- describe('normal x', function () {
- it('should have correct number of xs for each', function () {
- expect(Object.keys(chart.internal.data.xs).length).toBe(3);
- expect(chart.internal.data.xs.data1.length).toBe(6);
- expect(chart.internal.data.xs.data2.length).toBe(6);
- expect(chart.internal.data.xs.data3.length).toBe(6);
- });
- it('should have integer index as x', function () {
- for (var i = 0; i < chart.internal.data.xs.data3.length; i++) {
- expect(chart.internal.data.xs.data1[i]).toBe(i);
- expect(chart.internal.data.xs.data2[i]).toBe(i);
- expect(chart.internal.data.xs.data3[i]).toBe(i);
- }
- });
- });
- describe('timeseries x', function () {
- describe('without xFormat', function () {
- beforeAll(function () {
- args = {
- data: {
- x : 'date',
- columns: [
- ['date', '2013-01-01', '2013-01-02', '2013-01-03'],
- ['data1', 30, 200, 100],
- ['data2', 130, 300, 200]
- ]
- },
- axis : {
- x : {
- type : 'timeseries'
- }
- }
- };
- });
- it('should have correct number of xs', function () {
- expect(Object.keys(chart.internal.data.xs).length).toBe(2);
- expect(chart.internal.data.xs.data1.length).toBe(3);
- expect(chart.internal.data.xs.data2.length).toBe(3);
- });
- it('should have Date object as x', function () {
- var xs = chart.internal.data.xs;
- expect(+xs.data1[0]).toBe(+new Date(2013, 0, 1, 0, 0, 0));
- expect(+xs.data1[1]).toBe(+new Date(2013, 0, 2, 0, 0, 0));
- expect(+xs.data1[2]).toBe(+new Date(2013, 0, 3, 0, 0, 0));
- expect(+xs.data2[0]).toBe(+new Date(2013, 0, 1, 0, 0, 0));
- expect(+xs.data2[1]).toBe(+new Date(2013, 0, 2, 0, 0, 0));
- expect(+xs.data2[2]).toBe(+new Date(2013, 0, 3, 0, 0, 0));
- });
- });
- describe('with xFormat', function () {
- describe('timeseries x with xFormat', function () {
- beforeAll(function () {
- args = {
- data: {
- x : 'date',
- xFormat: '%Y%m%d',
- columns: [
- ['date', '20130101', '20130102', '20130103'],
- ['data1', 30, 200, 100],
- ['data2', 130, 300, 200]
- ]
- },
- axis : {
- x : {
- type : 'timeseries'
- }
- }
- };
- });
- it('should have correct number of xs', function () {
- expect(Object.keys(chart.internal.data.xs).length).toBe(2);
- expect(chart.internal.data.xs.data1.length).toBe(3);
- expect(chart.internal.data.xs.data2.length).toBe(3);
- });
- it('should have Date object as x', function () {
- var xs = chart.internal.data.xs;
- expect(+xs.data1[0]).toBe(+new Date(2013, 0, 1, 0, 0, 0));
- expect(+xs.data1[1]).toBe(+new Date(2013, 0, 2, 0, 0, 0));
- expect(+xs.data1[2]).toBe(+new Date(2013, 0, 3, 0, 0, 0));
- expect(+xs.data2[0]).toBe(+new Date(2013, 0, 1, 0, 0, 0));
- expect(+xs.data2[1]).toBe(+new Date(2013, 0, 2, 0, 0, 0));
- expect(+xs.data2[2]).toBe(+new Date(2013, 0, 3, 0, 0, 0));
- });
- });
- });
- });
- describe('milliseconds timeseries x', function () {
- describe('as date string', function () {
- beforeAll(function () {
- args = {
- data: {
- x : 'date',
- xFormat: '%Y-%m-%d %H:%M:%S.%L',
- columns: [
- ['date', "2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"],
- ['data1', 30, 200],
- ['data2', 130, 300]
- ]
- },
- axis: {
- x: {
- type: 'timeseries',
- tick: {
- format: '%Y-%m-%d %H:%M:%S.%L',
- multiline: false
- }
- }
- }
- };
- });
- it('should have correct number of xs', function () {
- expect(Object.keys(chart.internal.data.xs).length).toBe(2);
- expect(chart.internal.data.xs.data1.length).toBe(2);
- expect(chart.internal.data.xs.data2.length).toBe(2);
- });
- it('should have Date object as x', function () {
- var xs = chart.internal.data.xs;
- expect(+xs.data1[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123));
- expect(+xs.data1[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345));
- expect(+xs.data2[0]).toBe(+new Date(2014, 4, 20, 17, 25, 0, 123));
- expect(+xs.data2[1]).toBe(+new Date(2014, 4, 20, 17, 30, 0, 345));
- });
- it('should have milliseconds tick format', function () {
- var expected = ["2014-05-20 17:25:00.123", "2014-05-20 17:30:00.345"];
- chart.internal.main.selectAll('.c3-axis-x g.tick text').each(function (d, i) {
- expect(d3.select(this).text()).toBe(expected[i]);
- });
- });
- });
- describe('as unixtime number', function () {
- beforeAll(function () {
- args = {
- data: {
- x : 'date',
- columns: [
- ['date', 1417622461123, 1417622522345],
- ['data1', 30, 200],
- ['data2', 130, 300]
- ]
- },
- axis: {
- x: {
- type: 'timeseries',
- tick: {
- format: '%Y-%m-%d %H:%M:%S.%L'
- }
- }
- }
- };
- });
- it('should have correct number of xs', function () {
- expect(Object.keys(chart.internal.data.xs).length).toBe(2);
- expect(chart.internal.data.xs.data1.length).toBe(2);
- expect(chart.internal.data.xs.data2.length).toBe(2);
- });
- it('should have Date object as x', function () {
- var xs = chart.internal.data.xs;
- expect(+xs.data1[0]).toBe(1417622461123);
- expect(+xs.data1[1]).toBe(1417622522345);
- expect(+xs.data2[0]).toBe(1417622461123);
- expect(+xs.data2[1]).toBe(1417622522345);
- });
- });
- });
- });
- describe('data.label', function () {
- describe('on line chart', function () {
- beforeAll(function () {
- args = {
- data: {
- columns: [
- ['data1', 1030, 2200, 2100],
- ['data2', 1150, 2010, 1200],
- ['data3', -1150, -2010, -1200],
- ['data4', -1030, -2200, -2100],
- ],
- type: 'line',
- labels: true,
- }
- };
- });
- it('should locate data labels in correct position', function () {
- var expectedTextY = {
- data1: [128, 38, 46],
- data2: [119, 53, 115],
- data3: [311, 377, 315],
- data4: [302, 392, 384],
- };
- var expectedTextX = {
- data1: [6, 294, 583],
- data2: [6, 294, 583],
- data3: [6, 294, 583],
- data4: [6, 294, 583],
- };
- Object.keys(expectedTextY).forEach(function (key) {
- d3.selectAll('.c3-texts-' + key + ' text.c3-text').each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedTextY[key][i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedTextX[key][i], -2);
- });
- });
- });
- describe('with stacked', function () {
- beforeAll(function(){
- args.data.groups = [['data1', 'data2'], ['data3', 'data4']];
- });
- it('should locate data labels in correct position', function () {
- var expectedTextY = {
- data1: [120, 38, 75],
- data2: [161, 127, 159],
- data3: [269, 303, 271],
- data4: [310, 392, 355],
- };
- var expectedTextX = {
- data1: [6, 294, 583],
- data2: [6, 294, 583],
- data3: [6, 294, 583],
- data4: [6, 294, 583],
- };
- Object.keys(expectedTextY).forEach(function (key) {
- d3.selectAll('.c3-texts-' + key + ' text.c3-text').each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedTextY[key][i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedTextX[key][i], -2);
- });
- });
- });
- });
- });
- describe('on area chart', function () {
- beforeAll(function () {
- args = {
- data: {
- columns: [
- ['data1', 1030, 2200, 2100],
- ['data2', 1150, 2010, 1200],
- ['data3', -1150, -2010, -1200],
- ['data4', -1030, -2200, -2100],
- ],
- type: 'area',
- labels: true,
- }
- };
- });
- it('should locate data labels in correct position', function () {
- var expectedTextY = {
- data1: [128, 38, 46],
- data2: [119, 53, 115],
- data3: [311, 377, 315],
- data4: [302, 392, 384],
- };
- var expectedTextX = {
- data1: [6, 294, 583],
- data2: [6, 294, 583],
- data3: [6, 294, 583],
- data4: [6, 294, 583],
- };
- Object.keys(expectedTextY).forEach(function (key) {
- d3.selectAll('.c3-texts-' + key + ' text.c3-text').each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedTextY[key][i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedTextX[key][i], -2);
- });
- });
- });
- describe('with stacked', function () {
- beforeAll(function(){
- args.data.groups = [['data1', 'data2'], ['data3', 'data4']];
- });
- it('should locate data labels in correct position', function () {
- var expectedTextY = {
- data1: [120, 38, 75],
- data2: [161, 127, 159],
- data3: [269, 303, 271],
- data4: [310, 392, 355],
- };
- var expectedTextX = {
- data1: [6, 294, 583],
- data2: [6, 294, 583],
- data3: [6, 294, 583],
- data4: [6, 294, 583],
- };
- Object.keys(expectedTextY).forEach(function (key) {
- d3.selectAll('.c3-texts-' + key + ' text.c3-text').each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedTextY[key][i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedTextX[key][i], -2);
- });
- });
- });
- });
- });
- describe('on bar chart', function () {
- beforeAll(function () {
- args = {
- data: {
- columns: [
- ['data1', 1030, 2200, 2100],
- ['data2', 1150, 2010, 1200],
- ['data3', -1150, -2010, -1200],
- ['data4', -1030, -2200, -2100],
- ],
- type: 'bar',
- labels: true,
- }
- };
- });
- it('should locate data labels in correct position', function () {
- var expectedTextY = {
- data1: [128, 38, 46],
- data2: [119, 53, 115],
- data3: [311, 377, 315],
- data4: [302, 392, 384],
- };
- var expectedTextX = {
- data1: [53, 249, 445],
- data2: [83, 279, 475],
- data3: [112, 308, 504],
- data4: [142, 338, 534],
- };
- Object.keys(expectedTextY).forEach(function (key) {
- d3.selectAll('.c3-texts-' + key + ' text.c3-text').each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedTextY[key][i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedTextX[key][i], -2);
- });
- });
- });
- describe('with stacked', function () {
- beforeAll(function(){
- args.data.groups = [['data1', 'data2'], ['data3', 'data4']];
- });
- it('should locate data labels in correct position', function () {
- var expectedTextY = {
- data1: [120, 38, 75],
- data2: [161, 127, 159],
- data3: [269, 303, 271],
- data4: [310, 392, 355],
- };
- var expectedTextX = {
- data1: [68.6, 264, 460],
- data2: [68.6, 264, 460],
- data3: [127, 323, 519],
- data4: [127, 323, 519],
- };
- Object.keys(expectedTextY).forEach(function (key) {
- d3.selectAll('.c3-texts-' + key + ' text.c3-text').each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedTextY[key][i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedTextX[key][i], -2);
- });
- });
- });
- });
- });
- describe('for all targets', function () {
- describe('with data label for all data', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', 100, 200, 100, 400, 150, 250],
- ['data2', 10, 20, 10, 40, 15, 25],
- ['data3', 1000, 2000, 1000, 4000, 1500, 2500]
- ],
- labels: true
- }
- };
- });
- it('should have data labels on all data', function () {
- d3.selectAll('.c3-texts-data1 text').each(function (d, i) {
- expect(d3.select(this).text()).toBe(args.data.columns[0][i + 1] + '');
- });
- d3.selectAll('.c3-texts-data2 text').each(function (d, i) {
- expect(d3.select(this).text()).toBe(args.data.columns[1][i + 1] + '');
- });
- d3.selectAll('.c3-texts-data3 text').each(function (d, i) {
- expect(d3.select(this).text()).toBe(args.data.columns[2][i + 1] + '');
- });
- });
- });
- });
- describe('for each target', function () {
- describe('as true', function () {
- describe('with data label for only data1', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', 100, 200, 100, 400, 150, 250],
- ['data2', 10, 20, 10, 40, 15, 25],
- ['data3', 1000, 2000, 1000, 4000, 1500, 2500]
- ],
- labels: {
- format: {
- data1: true
- }
- }
- }
- };
- });
- it('should have data labels on all data', function () {
- d3.selectAll('.c3-texts-data1 text').each(function (d, i) {
- expect(d3.select(this).text()).toBe(args.data.columns[0][i + 1] + '');
- });
- d3.selectAll('.c3-texts-data2 text').each(function () {
- expect(d3.select(this).text()).toBe('');
- });
- d3.selectAll('.c3-texts-data3 text').each(function () {
- expect(d3.select(this).text()).toBe('');
- });
- });
- });
- });
- describe('as function', function () {
- describe('with data label for only data1', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', 100, 200, 100, 400, 150, 250],
- ['data2', 10, 20, 10, 40, 15, 25],
- ['data3', 1000, 2000, 1000, 4000, 1500, 2500]
- ],
- labels: {
- format: {
- data1: d3.format('$')
- }
- }
- }
- };
- });
- it('should have data labels on all data', function () {
- d3.selectAll('.c3-texts-data1 text').each(function (d, i) {
- expect(d3.select(this).text()).toBe('$' + args.data.columns[0][i + 1]);
- });
- d3.selectAll('.c3-texts-data2 text').each(function () {
- expect(d3.select(this).text()).toBe('');
- });
- d3.selectAll('.c3-texts-data3 text').each(function () {
- expect(d3.select(this).text()).toBe('');
- });
- });
- });
- });
- });
- describe('with small values', function () {
- describe('with data label', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', 0.03, 0.2, 0.1, 0.4, 0.15, 0.250]
- ],
- labels: true
- }
- };
- });
- it('should have proper y domain', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-0.02);
- expect(domain[1]).toBeCloseTo(0.45);
- });
- });
- });
- describe('with positive values and null', function () {
- describe('on not rotated axis', function () {
- beforeAll(function () {
- args = {
- data: {
- columns: [
- ['data1', 190, 200, 190, null],
- ],
- type: 'bar',
- labels: {
- format: function (v) {
- if (v === null) {
- return 'Not Applicable';
- }
- return d3.format('$')(v);
- }
- }
- }
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(0, -1);
- expect(domain[1]).toBeCloseTo(227, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [67, 49, 67, 423],
- expectedXs = [74, 221, 368, 515];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(189, -1);
- expect(domain[1]).toBeCloseTo(201, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [375, 40, 375, 422],
- expectedXs = [6, 198, 391, 583];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- describe('on rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args.data.type = 'bar';
- args.axis = {
- rotated: true
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(0, -1);
- expect(domain[1]).toBeCloseTo(231, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [57, 163, 269, 375],
- expectedXs = [490, 516, 490, 4];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(188, -1);
- expect(domain[1]).toBeCloseTo(202, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [9, 147, 286, 424],
- expectedXs = [76, 526, 76, 4];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- });
- describe('with negative values and null', function () {
- describe('on not rotated axis', function () {
- describe('type bar', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', -190, 0, -190, null],
- ],
- type: 'bar',
- labels: {
- format: function (v) {
- if (v === null) {
- return 'Not Applicable';
- }
- return d3.format('$')(v);
- }
- }
- }
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-215, -1);
- expect(domain[1]).toBeCloseTo(0, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [368, 12, 368, 12],
- expectedXs = [74, 221, 368, 515];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-215, -1);
- expect(domain[1]).toBeCloseTo(25, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [395, 60, 395, 12],
- expectedXs = [6, 198, 391, 583];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- describe('on rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args.data.type = 'bar';
- args.axis = {
- rotated: true
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-220, -1);
- expect(domain[1]).toBeCloseTo(0, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [57, 163, 269, 375],
- expectedXs = [103, 594, 103, 526];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-220, -1);
- expect(domain[1]).toBeCloseTo(24, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [9, 147, 286, 424],
- expectedXs = [67, 537, 67, 526];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- });
- describe('with positive and negative values and null', function () {
- describe('on non rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', -190, 200, 190, null],
- ],
- type: 'bar',
- labels: {
- format: function (v) {
- if (v === null) {
- return 'Not Applicable';
- }
- return d3.format('$')(v);
- }
- }
- }
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-243, -1);
- expect(domain[1]).toBeCloseTo(253, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [392, 43, 52, 215],
- expectedXs = [74, 221, 368, 515];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-243, -1);
- expect(domain[1]).toBeCloseTo(253, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [392, 40, 49, 212],
- expectedXs = [6, 198, 391, 583];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- describe('on rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args.data.type = 'bar';
- args.axis = {
- rotated: true
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-253, -1);
- expect(domain[1]).toBeCloseTo(260, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [57, 163, 269, 375],
- expectedXs = [69, 525, 513, 295];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-253, -1);
- expect(domain[1]).toBeCloseTo(260, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [9, 147, 286, 424],
- expectedXs = [67, 527, 515, 297];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- });
- describe('with positive grouped values', function () {
- describe('on non rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', 30, 200, 100, 500],
- ['data2', 50, 20, 10, 40],
- ['data3', 250, 220, 210, 240],
- ],
- groups: [['data1', 'data2', 'data3']],
- labels: true,
- type: 'bar',
- }
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(0, -1);
- expect(domain[1]).toBeCloseTo(885, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [385, 317, 370, 164],
- expectedXs = [74, 221, 368, 515];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-94, -1);
- expect(domain[1]).toBeCloseTo(884, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [344, 284, 331, 144],
- expectedXs = [6, 198, 391, 583];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- describe('on rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args.data.type = 'bar';
- args.axis = {
- rotated: true
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(0, -1);
- expect(domain[1]).toBeCloseTo(888, -1.2);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [57, 163, 269, 375],
- expectedXs = [57, 150, 77, 363];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-87, -1);
- expect(domain[1]).toBeCloseTo(887, -1.2);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [9, 147, 286, 424],
- expectedXs = [107, 192, 125, 386];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- });
- describe('with negative grouped values', function () {
- describe('on non rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args = {
- data: {
- columns: [
- ['data1', -30, -200, -100, -500],
- ['data2', -50, -20, -10, -40],
- ['data3', -250, -220, -210, -240]
- ],
- groups: [['data1', 'data2', 'data3']],
- labels: true,
- type: 'bar',
- }
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-885, -1);
- expect(domain[1]).toBeCloseTo(0, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [51, 118, 65, 272],
- expectedXs = [74, 221, 368, 515];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-884, -1);
- expect(domain[1]).toBeCloseTo(94, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [88, 149, 101, 288],
- expectedXs = [6, 198, 391, 583];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- describe('on rotated axis', function () {
- describe('data type bar', function () {
- beforeAll(function(){
- args.data.type = 'bar';
- args.axis = {
- rotated: true
- };
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-899, -1);
- expect(domain[1]).toBeCloseTo(0, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [57, 163, 269, 375],
- expectedXs = [533, 440, 513, 230];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- describe('data type line', function () {
- beforeAll(function(){
- args.data.type = 'line';
- });
- it('should have y domain with proper padding', function () {
- var domain = chart.internal.y.domain();
- expect(domain[0]).toBeCloseTo(-893, -1);
- expect(domain[1]).toBeCloseTo(93, -1);
- });
- it('should locate labels above each data point', function () {
- var texts = chart.internal.main.selectAll('.c3-texts-data1 text'),
- expectedYs = [9, 147, 286, 424],
- expectedXs = [480, 397, 462, 205];
- texts.each(function (d, i) {
- var text = d3.select(this);
- expect(+text.attr('y')).toBeCloseTo(expectedYs[i], -2);
- expect(+text.attr('x')).toBeCloseTo(expectedXs[i], -2);
- });
- });
- });
- });
- });
- });
- });
|