@@ -21,6 +21,8 @@ import { StageHandler, SeriesOption, SeriesSamplingOptionMixin } from '../util/t
2121import { Dictionary } from 'zrender/src/core/types' ;
2222import SeriesModel from '../model/Series' ;
2323import { isFunction , isString } from 'zrender/src/core/util' ;
24+ import SeriesData from '../data/SeriesData' ;
25+ import { CoordinateSystem } from '../coord/CoordinateSystem' ;
2426
2527
2628type Sampler = ( frame : ArrayLike < number > ) => number ;
@@ -88,6 +90,44 @@ const indexSampler = function (frame: ArrayLike<number>) {
8890 return Math . round ( frame . length / 2 ) ;
8991} ;
9092
93+ export function doDataSampling (
94+ data : SeriesData ,
95+ sampling : SeriesSamplingOptionMixin [ 'sampling' ] ,
96+ coordSys : CoordinateSystem ,
97+ dpr : number
98+ ) {
99+ const count = data . count ( ) ;
100+ // Only cartesian2d support down sampling. Disable it when there is few data.
101+ if ( count > 10 && coordSys . type === 'cartesian2d' && sampling ) {
102+ const baseAxis = coordSys . getBaseAxis ( ) ;
103+ const valueAxis = coordSys . getOtherAxis ( baseAxis ) ;
104+ const extent = baseAxis . getExtent ( ) ;
105+ // Coordinste system has been resized
106+ const size = Math . abs ( extent [ 1 ] - extent [ 0 ] ) * ( dpr || 1 ) ;
107+ const rate = Math . round ( count / size ) ;
108+
109+ if ( isFinite ( rate ) && rate > 1 ) {
110+ if ( sampling === 'lttb' ) {
111+ return data . lttbDownSample ( data . mapDimension ( valueAxis . dim ) , 1 / rate ) ;
112+ }
113+ let sampler ;
114+ if ( isString ( sampling ) ) {
115+ sampler = samplers [ sampling ] ;
116+ }
117+ else if ( isFunction ( sampling ) ) {
118+ sampler = sampling ;
119+ }
120+ if ( sampler ) {
121+ // Only support sample the first dim mapped from value axis.
122+ return data . downSample (
123+ data . mapDimension ( valueAxis . dim ) , 1 / rate , sampler , indexSampler
124+ ) ;
125+ }
126+ }
127+ }
128+ return null ;
129+ }
130+
91131export default function dataSample ( seriesType : string ) : StageHandler {
92132 return {
93133
@@ -100,35 +140,10 @@ export default function dataSample(seriesType: string): StageHandler {
100140 const data = seriesModel . getData ( ) ;
101141 const sampling = seriesModel . get ( 'sampling' ) ;
102142 const coordSys = seriesModel . coordinateSystem ;
103- const count = data . count ( ) ;
104- // Only cartesian2d support down sampling. Disable it when there is few data.
105- if ( count > 10 && coordSys . type === 'cartesian2d' && sampling ) {
106- const baseAxis = coordSys . getBaseAxis ( ) ;
107- const valueAxis = coordSys . getOtherAxis ( baseAxis ) ;
108- const extent = baseAxis . getExtent ( ) ;
109- const dpr = api . getDevicePixelRatio ( ) ;
110- // Coordinste system has been resized
111- const size = Math . abs ( extent [ 1 ] - extent [ 0 ] ) * ( dpr || 1 ) ;
112- const rate = Math . round ( count / size ) ;
113-
114- if ( isFinite ( rate ) && rate > 1 ) {
115- if ( sampling === 'lttb' ) {
116- seriesModel . setData ( data . lttbDownSample ( data . mapDimension ( valueAxis . dim ) , 1 / rate ) ) ;
117- }
118- let sampler ;
119- if ( isString ( sampling ) ) {
120- sampler = samplers [ sampling ] ;
121- }
122- else if ( isFunction ( sampling ) ) {
123- sampler = sampling ;
124- }
125- if ( sampler ) {
126- // Only support sample the first dim mapped from value axis.
127- seriesModel . setData ( data . downSample (
128- data . mapDimension ( valueAxis . dim ) , 1 / rate , sampler , indexSampler
129- ) ) ;
130- }
131- }
143+ const dpr = api . getDevicePixelRatio ( ) ;
144+ const resampled = doDataSampling ( data , sampling , coordSys , dpr ) ;
145+ if ( resampled ) {
146+ seriesModel . setData ( resampled ) ;
132147 }
133148 }
134149 } ;
0 commit comments