|
|
@@ -52,11 +52,8 @@ export interface XAxisDataResult {
|
|
|
}
|
|
|
|
|
|
export interface YAxisDataResult {
|
|
|
- yAxisData: {
|
|
|
- categories?: string[];
|
|
|
- rangesFormat: string[][];
|
|
|
- ranges: number[][];
|
|
|
- }[];
|
|
|
+ rangesFormat: string[][];
|
|
|
+ ranges: (number[] | string[])[];
|
|
|
yAxisWidth: {
|
|
|
position: string;
|
|
|
width: number;
|
|
|
@@ -369,56 +366,111 @@ export function calYAxisData(
|
|
|
const rangesFormatArr: string[][] = new Array(YLength);
|
|
|
const yAxisWidthArr: { position: string; width: number }[] = new Array(YLength);
|
|
|
|
|
|
- for (let i = 0; i < YLength; i++) {
|
|
|
- let yData = opts.yAxis?.data?.[i] as YAxisDataItem | undefined;
|
|
|
- if (!yData) {
|
|
|
- yData = {} as YAxisDataItem;
|
|
|
- }
|
|
|
- if (opts.yAxis?.disabled == true) {
|
|
|
- yData.disabled = true;
|
|
|
+ if (YLength > 0) {
|
|
|
+ for (let i = 0; i < YLength; i++) {
|
|
|
+ let yData = opts.yAxis?.data?.[i] as YAxisDataItem | undefined;
|
|
|
+ if (!yData) {
|
|
|
+ yData = {} as YAxisDataItem;
|
|
|
+ }
|
|
|
+ if (opts.yAxis?.disabled == true) {
|
|
|
+ yData.disabled = true;
|
|
|
+ }
|
|
|
+ if (yData.type === 'categories') {
|
|
|
+ if (!yData.formatter) {
|
|
|
+ yData.formatter = (val: any, _index: number, _opts: ChartOptions) => {
|
|
|
+ return String(val) + (yData?.unit || '');
|
|
|
+ };
|
|
|
+ }
|
|
|
+ yData.categories = yData.categories || opts.categories;
|
|
|
+ rangesArr[i] = yData.categories || [];
|
|
|
+ } else {
|
|
|
+ if (!yData.formatter) {
|
|
|
+ yData.formatter = (val: any, _index: number, _opts: ChartOptions) => {
|
|
|
+ return String(util.toFixed(Number(val), yData?.tofix || 0)) + (yData?.unit || '');
|
|
|
+ };
|
|
|
+ }
|
|
|
+ rangesArr[i] = getYAxisTextList(newSeries[i] || [], opts, config, columnstyle.type || '', yData, i);
|
|
|
+ }
|
|
|
+ const yAxisFontSizes = (yData.fontSize || opts.yAxis?.fontSize || config.fontSize) * (opts.pix || 1);
|
|
|
+ yAxisWidthArr[i] = {
|
|
|
+ position: yData.position ? yData.position : 'left',
|
|
|
+ width: 0
|
|
|
+ };
|
|
|
+ rangesFormatArr[i] = (rangesArr[i] as number[]).map(function(items, index) {
|
|
|
+ const formatted = yData.formatter!(items, index, opts);
|
|
|
+ yAxisWidthArr[i].width = Math.max(yAxisWidthArr[i].width, measureText(formatted, yAxisFontSizes, context) + 5);
|
|
|
+ return formatted;
|
|
|
+ });
|
|
|
+ const calibration = yData.calibration ? 4 * (opts.pix || 1) : 0;
|
|
|
+ yAxisWidthArr[i].width += calibration + 3 * (opts.pix || 1);
|
|
|
+ if (yData.disabled === true) {
|
|
|
+ yAxisWidthArr[i].width = 0;
|
|
|
+ }
|
|
|
}
|
|
|
- if (yData.type === 'categories') {
|
|
|
- if (!yData.formatter) {
|
|
|
- yData.formatter = (val: any, _index: number, _opts: ChartOptions) => {
|
|
|
- return String(val) + (yData?.unit || '');
|
|
|
+ } else {
|
|
|
+ // 当没有配置多Y轴时,使用默认配置
|
|
|
+ rangesArr.length = 1;
|
|
|
+ rangesFormatArr.length = 1;
|
|
|
+ yAxisWidthArr.length = 1;
|
|
|
+
|
|
|
+ if (opts.type === 'bar') {
|
|
|
+ rangesArr[0] = opts.categories || [];
|
|
|
+ if (!opts.yAxis?.formatter) {
|
|
|
+ if (!opts.yAxis) {
|
|
|
+ opts.yAxis = {} as any;
|
|
|
+ }
|
|
|
+ (opts.yAxis as any).formatter = (val: any, _index: number, _opts: ChartOptions) => {
|
|
|
+ return String(val) + (opts.yAxis?.unit || '');
|
|
|
};
|
|
|
}
|
|
|
- yData.categories = yData.categories || opts.categories;
|
|
|
- rangesArr[i] = yData.categories || [];
|
|
|
} else {
|
|
|
- if (!yData.formatter) {
|
|
|
- yData.formatter = (val: any, _index: number, _opts: ChartOptions) => {
|
|
|
- return String(util.toFixed(Number(val), yData?.tofix || 0)) + (yData?.unit || '');
|
|
|
+ if (!opts.yAxis?.formatter) {
|
|
|
+ if (!opts.yAxis) {
|
|
|
+ opts.yAxis = {} as any;
|
|
|
+ }
|
|
|
+ (opts.yAxis as any).formatter = (val: any, _index: number, _opts: ChartOptions) => {
|
|
|
+ return String(util.toFixed(Number(val), (opts.yAxis as any)?.tofix || 0)) + (opts.yAxis?.unit || '');
|
|
|
};
|
|
|
}
|
|
|
- rangesArr[i] = getYAxisTextList(newSeries[i] || [], opts, config, columnstyle.type || '', yData, i);
|
|
|
+ rangesArr[0] = getYAxisTextList(series, opts, config, columnstyle.type || '', {});
|
|
|
}
|
|
|
- const yAxisFontSizes = (yData.fontSize || opts.yAxis?.fontSize || config.fontSize) * (opts.pix || 1);
|
|
|
- yAxisWidthArr[i] = {
|
|
|
- position: yData.position ? yData.position : 'left',
|
|
|
+ yAxisWidthArr[0] = {
|
|
|
+ position: 'left',
|
|
|
width: 0
|
|
|
};
|
|
|
- rangesFormatArr[i] = (rangesArr[i] as number[]).map(function(items, index) {
|
|
|
- const formatted = yData.formatter!(items, index, opts);
|
|
|
- yAxisWidthArr[i].width = Math.max(yAxisWidthArr[i].width, measureText(formatted, yAxisFontSizes, context) + 5);
|
|
|
- return formatted;
|
|
|
+ const yAxisFontSize = ((opts.yAxis as any)?.fontSize || config.fontSize) * (opts.pix || 1);
|
|
|
+ rangesFormatArr[0] = rangesArr[0].map(function(item, index) {
|
|
|
+ const formatted = (opts.yAxis as any)?.formatter!(item, index, opts);
|
|
|
+ yAxisWidthArr[0].width = Math.max(yAxisWidthArr[0].width, measureText(formatted, yAxisFontSize, context) + 5);
|
|
|
+ return formatted as string;
|
|
|
});
|
|
|
- const calibration = yData.calibration ? 4 * (opts.pix || 1) : 0;
|
|
|
- yAxisWidthArr[i].width += calibration + 3 * (opts.pix || 1);
|
|
|
- if (yData.disabled === true) {
|
|
|
- yAxisWidthArr[i].width = 0;
|
|
|
+ yAxisWidthArr[0].width += 3 * (opts.pix || 1);
|
|
|
+ if ((opts.yAxis as any)?.disabled === true) {
|
|
|
+ yAxisWidthArr[0] = {
|
|
|
+ position: 'left',
|
|
|
+ width: 0
|
|
|
+ };
|
|
|
+ (opts.yAxis as any).data = [{
|
|
|
+ disabled: true
|
|
|
+ }];
|
|
|
+ } else {
|
|
|
+ (opts.yAxis as any).data = [{
|
|
|
+ disabled: false,
|
|
|
+ position: 'left',
|
|
|
+ max: (opts.yAxis as any)?.max,
|
|
|
+ min: (opts.yAxis as any)?.min,
|
|
|
+ formatter: (opts.yAxis as any)?.formatter
|
|
|
+ }];
|
|
|
+ if (opts.type === 'bar') {
|
|
|
+ (opts.yAxis as any).data[0].categories = opts.categories;
|
|
|
+ (opts.yAxis as any).data[0].type = 'categories';
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
- yAxisData: rangesArr.map((ranges, i) => {
|
|
|
- const isCategories = typeof ranges[0] === 'string';
|
|
|
- return {
|
|
|
- ranges: !isCategories ? [ranges as number[]] : [],
|
|
|
- rangesFormat: [rangesFormatArr[i]],
|
|
|
- categories: isCategories ? ranges as string[] : undefined
|
|
|
- };
|
|
|
- }),
|
|
|
+ rangesFormat: rangesFormatArr,
|
|
|
+ ranges: rangesArr,
|
|
|
yAxisWidth: yAxisWidthArr
|
|
|
};
|
|
|
}
|