11import { XDomain } from '../series/domains/x_domain' ;
22import { YDomain } from '../series/domains/y_domain' ;
3- import { Position } from '../series/specs' ;
3+ import { AxisSpec , DomainRange , Position } from '../series/specs' ;
44import { LIGHT_THEME } from '../themes/light_theme' ;
5- import { getAxisId , getGroupId } from '../utils/ids' ;
5+ import { getAxisId , getGroupId , GroupId } from '../utils/ids' ;
66import { ScaleType } from '../utils/scales/scales' ;
77import {
88 centerRotationOrigin ,
@@ -14,15 +14,17 @@ import {
1414 getAxisTicksPositions ,
1515 getHorizontalAxisGridLineProps ,
1616 getHorizontalAxisTickLineProps ,
17- getHorizontalDomain ,
1817 getMaxBboxDimensions ,
1918 getMinMaxRange ,
2019 getScaleForAxisSpec ,
2120 getTickLabelProps ,
2221 getVerticalAxisGridLineProps ,
2322 getVerticalAxisTickLineProps ,
24- getVerticalDomain ,
2523 getVisibleTicks ,
24+ isHorizontal ,
25+ isVertical ,
26+ isYDomain ,
27+ mergeDomainsByGroupId ,
2628} from './axis_utils' ;
2729import { CanvasTextBBoxCalculator } from './canvas_text_bbox_calculator' ;
2830import { SvgTextBBoxCalculator } from './svg_text_bbox_calculator' ;
@@ -71,7 +73,7 @@ describe('Axis computational utils', () => {
7173 maxLabelTextWidth : 10 ,
7274 maxLabelTextHeight : 10 ,
7375 } ;
74- const verticalAxisSpec = {
76+ const verticalAxisSpec : AxisSpec = {
7577 id : getAxisId ( 'axis_1' ) ,
7678 groupId : getGroupId ( 'group_1' ) ,
7779 hide : false ,
@@ -86,7 +88,7 @@ describe('Axis computational utils', () => {
8688 showGridLines : true ,
8789 } ;
8890
89- const horizontalAxisSpec = {
91+ const horizontalAxisSpec : AxisSpec = {
9092 id : getAxisId ( 'axis_2' ) ,
9193 groupId : getGroupId ( 'group_1' ) ,
9294 hide : false ,
@@ -149,6 +151,21 @@ describe('Axis computational utils', () => {
149151 bboxCalculator . destroy ( ) ;
150152 } ) ;
151153
154+ test ( 'should not compute axis dimensions when spec is configured to hide' , ( ) => {
155+ const bboxCalculator = new CanvasTextBBoxCalculator ( ) ;
156+ verticalAxisSpec . hide = true ;
157+ const axisDimensions = computeAxisTicksDimensions (
158+ verticalAxisSpec ,
159+ xDomain ,
160+ [ yDomain ] ,
161+ 1 ,
162+ bboxCalculator ,
163+ 0 ,
164+ axes ,
165+ ) ;
166+ expect ( axisDimensions ) . toBe ( null ) ;
167+ } ) ;
168+
152169 test ( 'should compute dimensions for the bounding box containing a rotated label' , ( ) => {
153170 expect ( computeRotatedLabelDimensions ( { width : 1 , height : 2 } , 0 ) ) . toEqual ( {
154171 width : 1 ,
@@ -914,13 +931,94 @@ describe('Axis computational utils', () => {
914931 expect ( horizontalAxisGridLines ) . toEqual ( [ 25 , 0 , 25 , 100 ] ) ;
915932 } ) ;
916933
917- test ( 'should return correct domain based on rotation' , ( ) => {
918- const chartRotation = 180 ;
919- expect ( getHorizontalDomain ( xDomain , [ yDomain ] , chartRotation ) ) . toEqual ( xDomain ) ;
920- expect ( getVerticalDomain ( xDomain , [ yDomain ] , chartRotation ) ) . toEqual ( [ yDomain ] ) ;
934+ test ( 'should determine orientation of axis position' , ( ) => {
935+ expect ( isVertical ( Position . Left ) ) . toBe ( true ) ;
936+ expect ( isVertical ( Position . Right ) ) . toBe ( true ) ;
937+ expect ( isVertical ( Position . Top ) ) . toBe ( false ) ;
938+ expect ( isVertical ( Position . Bottom ) ) . toBe ( false ) ;
939+
940+ expect ( isHorizontal ( Position . Left ) ) . toBe ( false ) ;
941+ expect ( isHorizontal ( Position . Right ) ) . toBe ( false ) ;
942+ expect ( isHorizontal ( Position . Top ) ) . toBe ( true ) ;
943+ expect ( isHorizontal ( Position . Bottom ) ) . toBe ( true ) ;
944+ } ) ;
945+
946+ test ( 'should determine if axis belongs to yDomain' , ( ) => {
947+ const verticalY = isYDomain ( Position . Left , 0 ) ;
948+ expect ( verticalY ) . toBe ( true ) ;
949+
950+ const verticalX = isYDomain ( Position . Left , 90 ) ;
951+ expect ( verticalX ) . toBe ( false ) ;
952+
953+ const horizontalX = isYDomain ( Position . Top , 0 ) ;
954+ expect ( horizontalX ) . toBe ( false ) ;
955+
956+ const horizontalY = isYDomain ( Position . Top , 90 ) ;
957+ expect ( horizontalY ) . toBe ( true ) ;
958+ } ) ;
959+
960+ test ( 'should merge axis domains by group id' , ( ) => {
961+ const groupId = getGroupId ( 'group_1' ) ;
962+ const domainRange1 = {
963+ min : 2 ,
964+ max : 9 ,
965+ } ;
966+
967+ verticalAxisSpec . domain = domainRange1 ;
968+
969+ const axesSpecs = new Map ( ) ;
970+ axesSpecs . set ( verticalAxisSpec . id , verticalAxisSpec ) ;
971+
972+ // Base case
973+ const expectedSimpleMap = new Map < GroupId , DomainRange > ( ) ;
974+ expectedSimpleMap . set ( groupId , { min : 2 , max : 9 } ) ;
975+
976+ const simpleDomainsByGroupId = mergeDomainsByGroupId ( axesSpecs , 0 ) ;
977+ expect ( simpleDomainsByGroupId ) . toEqual ( expectedSimpleMap ) ;
978+
979+ // Multiple definitions for the same group
980+ const domainRange2 = {
981+ min : 0 ,
982+ max : 7 ,
983+ } ;
984+
985+ const altVerticalAxisSpec = { ...verticalAxisSpec , id : getAxisId ( 'axis2' ) } ;
986+
987+ altVerticalAxisSpec . domain = domainRange2 ;
988+ axesSpecs . set ( altVerticalAxisSpec . id , altVerticalAxisSpec ) ;
989+
990+ const expectedMergedMap = new Map < GroupId , DomainRange > ( ) ;
991+ expectedMergedMap . set ( groupId , { min : 0 , max : 9 } ) ;
992+
993+ const mergedDomainsByGroupId = mergeDomainsByGroupId ( axesSpecs , 0 ) ;
994+ expect ( mergedDomainsByGroupId ) . toEqual ( expectedMergedMap ) ;
995+
996+ // xDomain limit (bad config)
997+ horizontalAxisSpec . domain = {
998+ min : 5 ,
999+ max : 15 ,
1000+ } ;
1001+ axesSpecs . set ( horizontalAxisSpec . id , horizontalAxisSpec ) ;
1002+
1003+ const attemptToMerge = ( ) => { mergeDomainsByGroupId ( axesSpecs , 0 ) ; } ;
1004+
1005+ expect ( attemptToMerge ) . toThrowError ( '[Axis axis_2]: custom domain for xDomain should be defined in Settings' ) ;
1006+ } ) ;
1007+
1008+ test ( 'should throw on invalid domain' , ( ) => {
1009+ const domainRange1 = {
1010+ min : 9 ,
1011+ max : 2 ,
1012+ } ;
1013+
1014+ verticalAxisSpec . domain = domainRange1 ;
1015+
1016+ const axesSpecs = new Map ( ) ;
1017+ axesSpecs . set ( verticalAxisSpec . id , verticalAxisSpec ) ;
1018+
1019+ const attemptToMerge = ( ) => { mergeDomainsByGroupId ( axesSpecs , 0 ) ; } ;
1020+ const expectedError = '[Axis axis_1]: custom domain is invalid, min is greater than max' ;
9211021
922- const skewChartRotation = 45 ;
923- expect ( getHorizontalDomain ( xDomain , [ yDomain ] , skewChartRotation ) ) . toEqual ( [ yDomain ] ) ;
924- expect ( getVerticalDomain ( xDomain , [ yDomain ] , skewChartRotation ) ) . toEqual ( xDomain ) ;
1022+ expect ( attemptToMerge ) . toThrowError ( expectedError ) ;
9251023 } ) ;
9261024} ) ;
0 commit comments