-
Notifications
You must be signed in to change notification settings - Fork 27
fix(Axistip): add supports the formatter and fix the tips display abnormally #248
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
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,12 +15,17 @@ import { isArray, isBoolean } from '../../util/type'; | |||||||||||||||||||||||||||||||||||||||
const distanceX = 16; // 鼠标与气泡框之间的左右偏移量 | ||||||||||||||||||||||||||||||||||||||||
const distanceY = 24; // 鼠标与气泡框之间的上偏移量 | ||||||||||||||||||||||||||||||||||||||||
const axisType = ['xAxis', 'yAxis']; | ||||||||||||||||||||||||||||||||||||||||
let textFormatter = {}; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// 设置 TriggerEvent | ||||||||||||||||||||||||||||||||||||||||
function setAxisTriggerEvent(eChartOption, type) { | ||||||||||||||||||||||||||||||||||||||||
if (!eChartOption[type]) return; | ||||||||||||||||||||||||||||||||||||||||
if (isArray(eChartOption[type])) { | ||||||||||||||||||||||||||||||||||||||||
eChartOption[type].forEach((subitem) => { | ||||||||||||||||||||||||||||||||||||||||
textFormatter[type] = undefined; | ||||||||||||||||||||||||||||||||||||||||
if(subitem.axisLabel?.formatter){ | ||||||||||||||||||||||||||||||||||||||||
textFormatter[type] = subitem.axisLabel.formatter | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+25
to
+28
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. Add formatter handling for non-array axis configuration The formatter is only extracted when } else {
+ textFormatter[type] = undefined;
+ if(eChartOption[type].axisLabel?.formatter){
+ textFormatter[type] = eChartOption[type].axisLabel.formatter
+ }
eChartOption[type].triggerEvent = true;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
subitem.triggerEvent = true; | ||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||
|
@@ -78,6 +83,7 @@ function axistip(echartsDom, echartsIns, eChartOption, axistip) { | |||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Object.keys(axistip).forEach(item => { | ||||||||||||||||||||||||||||||||||||||||
if(!axistip[item]) return; | ||||||||||||||||||||||||||||||||||||||||
setAxisTriggerEvent(eChartOption, item); | ||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||
// 气泡容器 | ||||||||||||||||||||||||||||||||||||||||
|
@@ -86,8 +92,12 @@ function axistip(echartsDom, echartsIns, eChartOption, axistip) { | |||||||||||||||||||||||||||||||||||||||
tipContainer.style.display = 'inline-block'; | ||||||||||||||||||||||||||||||||||||||||
tipContainer.style.opacity = '0'; | ||||||||||||||||||||||||||||||||||||||||
echartsIns.on('mousemove', (param) => { | ||||||||||||||||||||||||||||||||||||||||
tipContainer.textContent = param.value; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
let type = param.componentType; | ||||||||||||||||||||||||||||||||||||||||
if(param.name){ | ||||||||||||||||||||||||||||||||||||||||
tipContainer.textContent = param.name; | ||||||||||||||||||||||||||||||||||||||||
}else{ | ||||||||||||||||||||||||||||||||||||||||
tipContainer.textContent = textFormatter[type] ? textFormatter[type](param.value) : param.value; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+95
to
+100
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. 🛠️ Refactor suggestion Add error handling for formatter functions The current implementation directly calls the formatter function without any error handling. If the formatter throws an error, it could break the entire chart's functionality. -tipContainer.textContent = textFormatter[type] ? textFormatter[type](param.value) : param.value;
+try {
+ tipContainer.textContent = textFormatter[type] ? textFormatter[type](param.value) : param.value;
+} catch (error) {
+ console.error('Error applying axis formatter:', error);
+ tipContainer.textContent = param.value;
+} Also, maintain consistent use of spaces in conditional expressions: -if(param.name){
+if (param.name) { 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
if(axisType.indexOf(param.componentType) !== -1) { | ||||||||||||||||||||||||||||||||||||||||
setPosition(tipContainer, echartsDom, param); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
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.
Consider scoping
textFormatter
to avoid state persistence between callsThe
textFormatter
variable is declared at module scope usinglet
, which means its state will persist between multiple calls to theaxistip
function. This could lead to unexpected behavior if formatters from previous chart instances are applied to new charts.-let textFormatter = {};
Move the declaration inside the
axistip
function to reset it on each call:function axistip(echartsDom, echartsIns, eChartOption, axistip) { if (!axistip) return; + let textFormatter = {}; if (isBoolean(axistip)) { axistip = {}; axisType.forEach(item => { axistip[item] = true; }) }
📝 Committable suggestion