Skip to content

Commit 73ba1fb

Browse files
fix(JadeJueChart):modify the style specifications (#151)
1 parent d080ba7 commit 73ba1fb

File tree

6 files changed

+145
-85
lines changed

6 files changed

+145
-85
lines changed

doc/api/JadeJueChart/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import barMinRatioMd from './barMinRatio?raw';
1818
import { angleAxis, angleAxisMd } from '../common/angleAxis';
1919
import { radiusAxis, radiusAxisMd } from '../common/radiusAxis';
2020
import typeMd from './type.md?raw';
21+
import labelContentMd from './labelContent.md?raw';
2122

2223
const data = {
2324
dataset: [
@@ -39,7 +40,8 @@ const data = {
3940
angleAxis,
4041
radiusAxis,
4142
event,
42-
['type', '设置玉玦图的类别', 'string', 'unset']
43+
['type', '设置玉玦图的类别', 'string', 'unset'],
44+
['labelContent', '设置玉玦图每个系列的展示文本', 'string', 'name']
4345
],
4446
markdown: [
4547
themeMd,
@@ -60,7 +62,8 @@ const data = {
6062
angleAxisMd,
6163
radiusAxisMd,
6264
eventMd,
63-
typeMd
65+
typeMd,
66+
labelContentMd
6467
],
6568
};
6669

doc/api/JadeJueChart/labelContent.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
默认值:`name`
2+
3+
可选值:`name``nameWithRatio`
4+
5+
说明:设置玉玦图每个系列的展示文本,当存在radiusAxis.axisLabel.formatter,此属性失效。
6+
name:取data中的name字段。
7+
nameWidthRatio:玉玦图组件提供的可选formatter,将data中的nam字段与其占比拼接构成。

public/option/JadeJueChart/JadeJueChart-1.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ const option = {
1616

1717
}
1818
}
19-
}
19+
},
20+
labelContent: 'nameWithRatio'
2021
};

public/option/JadeJueChart/JadeJueChart-5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ const option = {
4646
// 自定义最大值,用于展示data中每项数据在最大值的占据比例,一般可取value最大值。
4747
// 未定义则data中每项数据的占比为其value值与总value值的比值。
4848
max: 120,
49+
labelContent: 'nameWithRatio'
4950
};

src/components/JadeJueChart/handleSeries.js

Lines changed: 128 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import { getColor } from '../../util/color';
1313
import cloneDeep from '../../util/cloneDeep';
1414
import { handleMinRatio } from './handleOption';
1515
import chartToken from './chartToken';
16+
import merge from '../../util/merge';
1617

1718
function createSeries(iChartOption, baseOpt, sum, legendData) {
19+
const { showBackground = true } = iChartOption;
1820
// 添加series中的数据
1921
let length = 0;
2022
iChartOption.data.forEach(dataItem => {
@@ -29,7 +31,7 @@ function createSeries(iChartOption, baseOpt, sum, legendData) {
2931
type: 'bar',
3032
data: '',
3133
stack: 'a',
32-
roundCap: false,
34+
roundCap: true,
3335
z: 2,
3436
coordinateSystem: 'polar',
3537
});
@@ -42,12 +44,17 @@ function createSeries(iChartOption, baseOpt, sum, legendData) {
4244
borderColor: chartToken.itemBorderColor,
4345
borderWidth: 2,
4446
};
45-
if (index === 0) {
46-
series.z = 1;
47-
} else if (index === baseOpt.series.length - 1) {
48-
series.z = 0;
49-
} else {
50-
series.roundCap = false;
47+
switch (index) {
48+
case 0:
49+
series.z = 1;
50+
break;
51+
case baseOpt.series.length - 1:
52+
series.z = 2;
53+
break;
54+
default:
55+
series.z = 3;
56+
series.roundCap = false;
57+
break;
5158
}
5259
});
5360
// 将iChartOption的data数据依次赋给series
@@ -58,38 +65,81 @@ function createSeries(iChartOption, baseOpt, sum, legendData) {
5865
baseOpt.series[i].name = legendData[i];
5966
});
6067
}
68+
if (showBackground) {
69+
const placeHolderData = [];
70+
iChartOption.data.forEach((_, index) => {
71+
let typeSum = 0;
72+
baseOpt.series.forEach((v) => {
73+
typeSum += v.data[index].value;
74+
});
75+
placeHolderData.push({
76+
type: 'stack背景占位',
77+
value: baseOpt.angleAxis.sum - typeSum,
78+
itemStyle: { color: chartToken.itemColor },
79+
sum: 0
80+
});
81+
});
82+
baseOpt.series.push({
83+
type: 'bar',
84+
data: placeHolderData,
85+
stack: 'a',
86+
roundCap: true,
87+
z: 1,
88+
coordinateSystem: 'polar',
89+
silent: true,
90+
});
91+
}
6192
}
6293

63-
const setRadiusAxis = (baseOpt, data, type) => {
94+
const setRadiusAxis = (baseOpt, data, type, iChartOption) => {
95+
const { labelContent = 'name', showBackground = true } = iChartOption;
6496
baseOpt.radiusAxis.z = 10;
6597
baseOpt.radiusAxis.data = [];
6698
if (type === 'process') return; // 进度图不展示左侧文本
6799
data.forEach((item, index) => {
68100
baseOpt.radiusAxis.data.push(item.name);
69101
});
70-
71-
// if (type === 'base') {
72-
// baseOpt.radiusAxis.axisLabel.rich = {
73-
// value: {
74-
// color: chartToken.labelValueColor,
75-
// padding: [0, chartToken.labelPadding, 0, 0]
76-
// },
77-
// ratio: chartToken.labelRatioColor
78-
// };
79-
// baseOpt.radiusAxis.axisLabel.formatter = (params, index) => {
80-
// let value;
81-
// baseOpt.series[index].data.forEach((dataItem) => {
82-
// if (dataItem.name === params) {
83-
// value = dataItem.beforeChangeValue;
84-
// }
85-
// });
86-
// if (value === undefined) {
87-
// value = data[index].value;
88-
// }
89-
// const ratio = (value / baseOpt.angleAxis.sum * 100).toFixed(2) + '%';
90-
// return `{value|${value}}{ratio|${ratio}}`;
91-
// };
92-
// }
102+
const getRatio = (params) => {
103+
const index = params[1];
104+
let value = 0;
105+
let ratio = 0;
106+
if (iChartOption.type === 'stack') {
107+
if (!showBackground) {
108+
baseOpt.series.forEach(val => {
109+
value += val.data[index].value;
110+
});
111+
} else {
112+
baseOpt.series.slice(0, -1).forEach(val => {
113+
value += val.data[index].value;
114+
});
115+
}
116+
} else {
117+
value = baseOpt.series[index].data[index].beforeChangeValue ?? baseOpt.series[index].data[index].value;
118+
}
119+
ratio = value / baseOpt.angleAxis.sum;
120+
return ratio;
121+
};
122+
const iChartLabelFormatter = iChartOption?.radiusAxis?.axisLabel?.formatter;
123+
if (iChartLabelFormatter) {
124+
baseOpt.radiusAxis.axisLabel.formatter = (...params) => iChartLabelFormatter(...params, getRatio(params));
125+
} else {
126+
if (labelContent === 'name') {
127+
return;
128+
} else if (labelContent === 'nameWithRatio') {
129+
const iChartLabelRich = iChartOption?.radiusAxis?.axisLabel?.rich ?? {};
130+
const defaultLabelRich = {
131+
name: {
132+
color: chartToken.labelValueColor,
133+
padding: [0, chartToken.labelPadding, 0, 0]
134+
},
135+
ratio: {
136+
color: chartToken.labelRatioColor,
137+
}
138+
};
139+
baseOpt.radiusAxis.axisLabel.rich = merge(defaultLabelRich, iChartLabelRich);
140+
baseOpt.radiusAxis.axisLabel.formatter = (...params) => `{name|${params[0]}} {ratio|${(getRatio(params) * 100).toFixed(0) + '%'}}`;
141+
}
142+
}
93143
};
94144

95145
// 配置玉玦图的seriesData数据(value,name,color)
@@ -147,64 +197,62 @@ export function setSeriesData(iChartOption, baseOpt) {
147197
handleMinRatio(iChartOption, baseOpt, type);
148198

149199
// 配置展示每项data的名称,及显示层级
150-
setRadiusAxis(baseOpt, data, type);
200+
setRadiusAxis(baseOpt, data, type, iChartOption);
151201

152202
if (type === 'process') {
153203
baseOpt.series.forEach((item) => {
154204
item.showBackground = showBackground;
155205
});
156206
} else {
157-
data.forEach(dataItem => {
158-
let sum = 0;
159-
// data中的每项如果没有children
160-
if (type === 'base') {
161-
// 是否显示柱条背景样式---补全75%数据及颜色
162-
if (showBackground) {
163-
// 当数据全为0时,手动设置sum和max使其不为0,避免视图丢失
164-
if (baseOpt.angleAxis.sum === 0) {
165-
baseOpt.angleAxis.sum = 1;
166-
baseOpt.angleAxis.max = 4 / 3;
167-
}
168-
const newData = cloneDeep(data);
169-
newData.forEach(newItem => {
170-
newItem.itemStyle = {
171-
color: chartToken.itemColor,
172-
};
173-
newItem.value = baseOpt.angleAxis.sum - newItem.value;
174-
newItem.sum = 0;
175-
});
176-
baseOpt.series[data.length] = {
177-
type: 'bar',
178-
data: newData,
179-
stack: 'a',
180-
z: 1,
181-
roundCap,
182-
coordinateSystem: 'polar',
183-
// 关闭灰色进度鼠标hover事件
184-
silent: true,
185-
};
207+
let sum = 0;
208+
// data中的每项如果没有children
209+
if (type === 'base') {
210+
// 是否显示柱条背景样式---补全75%数据及颜色
211+
if (showBackground) {
212+
// 当数据全为0时,手动设置sum和max使其不为0,避免视图丢失
213+
if (baseOpt.angleAxis.sum === 0) {
214+
baseOpt.angleAxis.sum = 1;
215+
baseOpt.angleAxis.max = 4 / 3;
186216
}
187-
} else {
188-
// data中的value是数组类型,处理数据;配置初始数据及颜色;获取图例type,后续赋给series
189-
let legendData = [];
190-
data.forEach(dataItem => {
191-
dataItem.children.forEach(child => {
192-
sum += child.value;
193-
});
194-
});
195-
data.forEach(dataItem => {
196-
dataItem.children.forEach(child => {
197-
child.sum = sum;
198-
legendData.push(child.type);
199-
});
217+
const newData = cloneDeep(data);
218+
newData.forEach(newItem => {
219+
newItem.itemStyle = {
220+
color: chartToken.itemColor,
221+
};
222+
newItem.value = baseOpt.angleAxis.sum - newItem.value;
223+
newItem.sum = 0;
200224
});
201-
// 添加series中的数据
202-
legendData = Array.from(new Set(legendData));
203-
createSeries(iChartOption, baseOpt, sum, legendData);
204-
// 配置图例颜色
205-
baseOpt.color = color;
225+
baseOpt.series[data.length] = {
226+
type: 'bar',
227+
data: newData,
228+
stack: 'a',
229+
z: 1,
230+
roundCap,
231+
coordinateSystem: 'polar',
232+
// 关闭灰色进度鼠标hover事件
233+
silent: true,
234+
};
206235
}
207-
});
236+
} else {
237+
// data中的value是数组类型,处理数据;配置初始数据及颜色;获取图例type,后续赋给series
238+
let legendData = [];
239+
data.forEach(dataItem => {
240+
dataItem.children.forEach(child => {
241+
sum += child.value;
242+
});
243+
});
244+
data.forEach(dataItem => {
245+
dataItem.children.forEach(child => {
246+
child.sum = sum;
247+
legendData.push(child.type);
248+
});
249+
});
250+
// 添加series中的数据
251+
legendData = Array.from(new Set(legendData));
252+
createSeries(iChartOption, baseOpt, sum, legendData);
253+
// 配置图例颜色
254+
baseOpt.color = color;
255+
}
208256
}
209257
}
210258

src/feature/token/factory/chartsToken/JadeJueChart.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
*
1111
*/
1212
function JadeJueChart(aliasToken) {
13-
const { colorBorder, colorBgEmpty, colorLabel, colorTextSecondary, paddingLG } = aliasToken;
13+
const { colorBorder, colorBgEmpty, colorLabel, colorTextSecondary, padding } = aliasToken;
1414

1515
return {
1616
itemBorderColor: colorBorder,
1717
itemColor: colorBgEmpty,
1818
labelValueColor: colorTextSecondary,
1919
labelRatioColor: colorLabel,
20-
labelPadding: paddingLG
20+
labelPadding: padding
2121
};
2222

2323
};

0 commit comments

Comments
 (0)