Skip to content

Commit

Permalink
优化饼图性能
Browse files Browse the repository at this point in the history
  • Loading branch information
杨骥 committed Jul 4, 2022
1 parent bafb0f9 commit 55a9f77
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 20 deletions.
28 changes: 12 additions & 16 deletions src/chart/pie/PieSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import createSeriesDataSimply from '../helper/createSeriesDataSimply';
import * as zrUtil from 'zrender/src/core/util';
import * as modelUtil from '../../util/model';
import { getPercentWithPrecision } from '../../util/number';
import { getPercentSeats } from '../../util/number';
import { makeSeriesEncodeForNameBased } from '../../data/helper/sourceHelper';
import LegendVisualProvider from '../../visual/LegendVisualProvider';
import SeriesModel from '../../model/Series';
Expand Down Expand Up @@ -133,6 +133,8 @@ class PieSeriesModel extends SeriesModel<PieSeriesOption> {

static type = 'series.pie' as const;

seats: number[];

/**
* @overwrite
*/
Expand All @@ -159,31 +161,25 @@ class PieSeriesModel extends SeriesModel<PieSeriesOption> {
* @overwrite
*/
getInitialData(this: PieSeriesModel): SeriesData {
return createSeriesDataSimply(this, {
const data = createSeriesDataSimply(this, {
coordDimensions: ['value'],
encodeDefaulter: zrUtil.curry(makeSeriesEncodeForNameBased, this)
});
const valueList:number[] = [];
data.each(data.mapDimension('value'), function (value: number) {
valueList.push(value);
});

this.seats = getPercentSeats(valueList, data.hostModel.get('percentPrecision'));
return data;
}

/**
* @overwrite
*/
getDataParams(dataIndex: number): PieCallbackDataParams {
const data = this.getData();
const params = super.getDataParams(dataIndex) as PieCallbackDataParams;
// FIXME toFixed?

const valueList: number[] = [];
data.each(data.mapDimension('value'), function (value: number) {
valueList.push(value);
});

params.percent = getPercentWithPrecision(
valueList,
dataIndex,
data.hostModel.get('percentPrecision')
);

params.percent = this.seats[dataIndex] / 100;
params.$vars.push('percent');
return params;
}
Expand Down
24 changes: 20 additions & 4 deletions src/util/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,33 @@ export function getPixelPrecision(dataExtent: [number, number], pixelExtent: [nu
* @param precision integer number showing digits of precision
* @return percent ranging from 0 to 100
*/
export function getPercentWithPrecision(valueList: number[], idx: number, precision: number): number {
export function getPercentWithPrecision(valueList: number[], idx: number, precision: number): number {
if (!valueList[idx]) {
return 0;
}

const seats = getPercentSeats(valueList, precision);
const digits = Math.pow(10, precision);

return (seats[idx] || 0) / digits;
}

/**
* Get a data of given precision, assuring the sum of percentages
* in valueList is 1.
* The largest remainer method is used.
* https://en.wikipedia.org/wiki/Largest_remainder_method
*
* @param valueList a list of all data
* @param precision integer number showing digits of precision
* @return {Array<number>} percent ranging from 0 to 100
*/
export function getPercentSeats(valueList: number[], precision: number): number[] {
const sum = zrUtil.reduce(valueList, function (acc, val) {
return acc + (isNaN(val) ? 0 : val);
}, 0);
if (sum === 0) {
return 0;
return [];
}

const digits = Math.pow(10, precision);
Expand Down Expand Up @@ -275,8 +292,7 @@ export function getPercentWithPrecision(valueList: number[], idx: number, precis
remainder[maxId] = 0;
++currentSum;
}

return seats[idx] / digits;
return seats;
}

/**
Expand Down
83 changes: 83 additions & 0 deletions test/pie-percent.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 55a9f77

Please sign in to comment.