-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(图表): 桑基图提示信息支持配置总出占比显示 #16476 #16535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,6 +456,9 @@ watch( | |
} | ||
} | ||
) | ||
const showTotalPercent = computed(() => { | ||
return props.chart.type === 'sankey' | ||
}) | ||
onMounted(() => { | ||
init() | ||
useEmitt({ name: 'addAxis', callback: updateSeriesTooltipFormatter }) | ||
|
@@ -697,6 +700,15 @@ onMounted(() => { | |
:label="t('chart.value_formatter_thousand_separator')" | ||
/> | ||
</el-form-item> | ||
<el-form-item v-if="showTotalPercent" class="form-item" :class="'form-item-' + themes"> | ||
<el-checkbox | ||
size="small" | ||
:effect="props.themes" | ||
v-model="state.tooltipForm.tooltipFormatter.showTotalPercent" | ||
@change="changeTooltipAttr('tooltipFormatter.showTotalPercent')" | ||
:label="t('chart.value_formatter_total_out_percent')" | ||
/> | ||
</el-form-item> | ||
</template> | ||
<div v-if="showSeriesTooltipFormatter"> | ||
<el-form-item> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码中有一个变量名可能引起歧义或混淆:
建议为 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,13 @@ export class SankeyBar extends G2PlotChartView<SankeyOptions, Sankey> { | |
if (customAttr.tooltip) { | ||
const t = JSON.parse(JSON.stringify(customAttr.tooltip)) | ||
if (t.show) { | ||
// 计算每个source节点的总出 | ||
const outTotal = {} | ||
if (Array.isArray(options.data)) { | ||
options.data?.forEach(d => { | ||
outTotal[d.source] = (outTotal[d.source] || 0) + d.value | ||
}) | ||
} | ||
tooltip = { | ||
showTitle: false, | ||
showMarkers: false, | ||
|
@@ -186,6 +193,16 @@ export class SankeyBar extends G2PlotChartView<SankeyOptions, Sankey> { | |
}, | ||
formatter: (datum: Datum) => { | ||
const { source, target, value } = datum | ||
// 总出占比 | ||
if (t.tooltipFormatter.showTotalPercent) { | ||
const decimalCount = | ||
t.tooltipFormatter.type !== 'auto' ? t.tooltipFormatter.decimalCount : 2 | ||
const ratio = (value / outTotal[source]).toFixed(decimalCount) | ||
return { | ||
name: source + ' -> ' + target, | ||
value: valueFormatter(value, t.tooltipFormatter) + ` (${ratio}%)` | ||
} | ||
} | ||
return { | ||
name: source + ' -> ' + target, | ||
value: valueFormatter(value, t.tooltipFormatter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在检查代码差异时,存在以下问题:
改进措施:
以下是改进建议: // 定义数值四舍五入函数
function toFixed(num: number, digits: number): string {
const factor = Math.pow(10, digits);
return (Math.round(num * factor) / factor).toString();
}
export class SankeyBar extends G2PlotChartView<SankeyOptions, Sankey> {
public static readonly type = 'sankey-bar';
// 其他代码...
if ( customAttr.tooltip ) {
let t = JSON.parse(JSON.stringify(customAttr.tooltip));
if(t && t.show){
// 计算每个source节点的总出
let outTotal:{[index:string]:number}={};
Array.isArray(options.data)&&options.data.forEach(d=>{
outTotal[d.source]=(outTotal[d.source]|0)+d.value;
});
tooltip={
showTitle:false,
showMarkers:true,
...others...,
formatter:(datum:Datum):{}=>{
const {source,target,value}=datum;
// 总出占比
let outData=outTotal[source];
if(outData&&t.tooltipFormatter['showTotalPercent']){
let ratio=toFixed(value/outData,t.tooltipFormatter['decimalCount']||2);
return {
name:`${source}->${target}`,
value:valueFormatter(value,t.tooltipFormatter)+' ('+ratio+'%)'
};
}
return {...datum,...otherValues};
},
};
}
else{
console.log('Tooltip is not shown.');
}
}
} 请注意上述代码示例中的逻辑变化以及对 |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
该代码的变化没有明显的不规范或潜在问题。优化建议是保持代码清晰和一致,并在必要时添加更多的注释以提高可读性。具体来说:
thousandSeparator
对应的是千位分隔符号。showTotalPercent
则是用来显示总出占比的标志。以下是修改后的代码示例:
这样可以使得代码更易于理解和维护。