1717* under the License.
1818*/
1919
20- import * as zrUtil from 'zrender/src/core/util' ;
20+ import { isArray } from 'zrender/src/core/util' ;
2121import { parsePercent } from '../../util/number' ;
2222import type GlobalModel from '../../model/Global' ;
23- import BoxplotSeriesModel from './BoxplotSeries' ;
24- import Axis2D from '../../coord/cartesian/Axis2D' ;
25-
26- const each = zrUtil . each ;
27-
28- interface GroupItem {
29- seriesModels : BoxplotSeriesModel [ ]
30- axis : Axis2D
31- boxOffsetList : number [ ]
32- boxWidthList : number [ ]
33- }
23+ import BoxplotSeriesModel , { SERIES_TYPE_BOXPLOT } from './BoxplotSeries' ;
24+ import {
25+ eachCollectedAxis , eachCollectedSeries , getCollectedSeriesLength ,
26+ requireAxisStatistics
27+ } from '../../coord/axisStatistics' ;
28+ import { makeCallOnlyOnce } from '../../util/model' ;
29+ import { EChartsExtensionInstallRegisters } from '../../extension' ;
30+ import Axis from '../../coord/Axis' ;
31+ import { registerAxisContainShapeHandler } from '../../coord/scaleRawExtentInfo' ;
32+ import { calcBandWidth } from '../../coord/axisBand' ;
33+ import {
34+ makeAxisStatKey ,
35+ createSimpleAxisStatClient , createBandWidthBasedAxisContainShapeHandler
36+ } from '../helper/axisSnippets' ;
37+
38+
39+ const callOnlyOnce = makeCallOnlyOnce ( ) ;
3440
3541export interface BoxplotItemLayout {
3642 ends : number [ ] [ ]
3743 initBaseline : number
3844}
3945
40- export default function boxplotLayout ( ecModel : GlobalModel ) {
41-
42- const groupResult = groupSeriesByAxis ( ecModel ) ;
43-
44- each ( groupResult , function ( groupItem ) {
45- const seriesModels = groupItem . seriesModels ;
46-
47- if ( ! seriesModels . length ) {
46+ export function boxplotLayout ( ecModel : GlobalModel ) {
47+ const axisStatKey = makeAxisStatKey ( SERIES_TYPE_BOXPLOT ) ;
48+ eachCollectedAxis ( ecModel , axisStatKey , function ( axis ) {
49+ const seriesCount = getCollectedSeriesLength ( axis , axisStatKey ) ;
50+ if ( ! seriesCount ) {
4851 return ;
4952 }
50-
51- calculateBase ( groupItem ) ;
52-
53- each ( seriesModels , function ( seriesModel , idx ) {
53+ const baseResult = calculateBase ( axis , seriesCount ) ;
54+ eachCollectedSeries ( axis , axisStatKey , function ( seriesModel : BoxplotSeriesModel , idx ) {
5455 layoutSingleSeries (
5556 seriesModel ,
56- groupItem . boxOffsetList [ idx ] ,
57- groupItem . boxWidthList [ idx ]
57+ baseResult . boxOffsetList [ idx ] ,
58+ baseResult . boxWidthList [ idx ]
5859 ) ;
5960 } ) ;
6061 } ) ;
6162}
6263
63- /**
64- * Group series by axis.
65- */
66- function groupSeriesByAxis ( ecModel : GlobalModel ) {
67- const result : GroupItem [ ] = [ ] ;
68- const axisList : Axis2D [ ] = [ ] ;
69-
70- ecModel . eachSeriesByType ( 'boxplot' , function ( seriesModel : BoxplotSeriesModel ) {
71- const baseAxis = seriesModel . getBaseAxis ( ) ;
72- let idx = zrUtil . indexOf ( axisList , baseAxis ) ;
73-
74- if ( idx < 0 ) {
75- idx = axisList . length ;
76- axisList [ idx ] = baseAxis ;
77- result [ idx ] = {
78- axis : baseAxis ,
79- seriesModels : [ ]
80- } as GroupItem ;
81- }
82-
83- result [ idx ] . seriesModels . push ( seriesModel ) ;
84- } ) ;
85-
86- return result ;
87- }
88-
8964/**
9065 * Calculate offset and box width for each series.
9166 */
92- function calculateBase ( groupItem : GroupItem ) {
93- const baseAxis = groupItem . axis ;
94- const seriesModels = groupItem . seriesModels ;
95- const seriesCount = seriesModels . length ;
96-
97- const boxWidthList : number [ ] = groupItem . boxWidthList = [ ] ;
98- const boxOffsetList : number [ ] = groupItem . boxOffsetList = [ ] ;
67+ function calculateBase ( baseAxis : Axis , seriesCount : number ) : {
68+ boxOffsetList : number [ ] ;
69+ boxWidthList : number [ ] ;
70+ } {
71+ const boxWidthList : number [ ] = [ ] ;
72+ const boxOffsetList : number [ ] = [ ] ;
9973 const boundList : number [ ] [ ] = [ ] ;
10074
101- let bandWidth : number ;
102- if ( baseAxis . type === 'category' ) {
103- bandWidth = baseAxis . getBandWidth ( ) ;
104- }
105- else {
106- let maxDataCount = 0 ;
107- each ( seriesModels , function ( seriesModel ) {
108- maxDataCount = Math . max ( maxDataCount , seriesModel . getData ( ) . count ( ) ) ;
109- } ) ;
110- const extent = baseAxis . getExtent ( ) ;
111- bandWidth = Math . abs ( extent [ 1 ] - extent [ 0 ] ) / maxDataCount ;
112- }
75+ const bandWidth = calcBandWidth (
76+ baseAxis ,
77+ { fromStat : { key : makeAxisStatKey ( SERIES_TYPE_BOXPLOT ) } , min : 1 } ,
78+ ) . w ;
11379
114- each ( seriesModels , function ( seriesModel ) {
80+ eachCollectedSeries ( baseAxis , makeAxisStatKey ( SERIES_TYPE_BOXPLOT ) , function ( seriesModel : BoxplotSeriesModel ) {
11581 let boxWidthBound = seriesModel . get ( 'boxWidth' ) ;
116- if ( ! zrUtil . isArray ( boxWidthBound ) ) {
82+ if ( ! isArray ( boxWidthBound ) ) {
11783 boxWidthBound = [ boxWidthBound , boxWidthBound ] ;
11884 }
11985 boundList . push ( [
@@ -127,14 +93,19 @@ function calculateBase(groupItem: GroupItem) {
12793 const boxWidth = ( availableWidth - boxGap * ( seriesCount - 1 ) ) / seriesCount ;
12894 let base = boxWidth / 2 - availableWidth / 2 ;
12995
130- each ( seriesModels , function ( seriesModel , idx ) {
96+ eachCollectedSeries ( baseAxis , makeAxisStatKey ( SERIES_TYPE_BOXPLOT ) , function ( seriesModel , idx ) {
13197 boxOffsetList . push ( base ) ;
13298 base += boxGap + boxWidth ;
13399
134100 boxWidthList . push (
135101 Math . min ( Math . max ( boxWidth , boundList [ idx ] [ 0 ] ) , boundList [ idx ] [ 1 ] )
136102 ) ;
137103 } ) ;
104+
105+ return {
106+ boxOffsetList,
107+ boxWidthList,
108+ } ;
138109}
139110
140111/**
@@ -212,3 +183,18 @@ function layoutSingleSeries(seriesModel: BoxplotSeriesModel, offset: number, box
212183 ends . push ( from , to ) ;
213184 }
214185}
186+
187+ export function registerBoxplotAxisHandlers ( registers : EChartsExtensionInstallRegisters ) {
188+ callOnlyOnce ( registers , function ( ) {
189+ const axisStatKey = makeAxisStatKey ( SERIES_TYPE_BOXPLOT ) ;
190+ requireAxisStatistics (
191+ registers ,
192+ axisStatKey ,
193+ createSimpleAxisStatClient ( SERIES_TYPE_BOXPLOT )
194+ ) ;
195+ registerAxisContainShapeHandler (
196+ axisStatKey ,
197+ createBandWidthBasedAxisContainShapeHandler ( axisStatKey )
198+ ) ;
199+ } ) ;
200+ }
0 commit comments