Skip to content

Comments

fix: Ecarts rendering error#4009

Merged
shaohuzhang1 merged 1 commit intov2from
pr@v2@fix_echarts
Sep 9, 2025
Merged

fix: Ecarts rendering error#4009
shaohuzhang1 merged 1 commit intov2from
pr@v2@fix_echarts

Conversation

@shaohuzhang1
Copy link
Contributor

fix: Ecarts rendering error

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Sep 9, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Sep 9, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

},
)

onMounted(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code is mostly clean and follows best practices for setting up a Vue.js component with an ECharts chart, but there are some minor improvements that can be made:

  1. Remove Redundant Code: The style.value = option_json.style line inside the initChart() function does not actually have any effect because it reassigns style.value to itself, which doesn't change anything.

  2. Optimize Option JSON Handling: Instead of creating an empty object ({}) manually and then immediately overwriting it, you can simply use the properties from option_json. This avoids unnecessary operations.

  3. Use watchEffect Properly: Since you're using watch, make sure to pass an asynchronous function if evalParseOption returns a promise or uses asynchronous operations internally.

Here's the updated version of the code:

const chartsRef = ref()

const style = ref({
  height: '220px',
  width: '100%',
})

function initChart() {
}

function evalParseOption(option_json: any) {
  if (option_json?.style) {
    style.value = { ...style.value, ...option_json.style };
  }

  let option = {};
  try {
    // Ensure safe usage of eval by only allowing certain functions
    option = new Function('return ' + option_json.option)();
  } catch (error) {
    console.error("Error parsing options:", error);
  }
  
  return option;
}

// Use watch instead of manual evaluation
watch(chartsData, async () => {
  await initChart();
});

onMounted(async () => {
  // Initial setup
  chartsData.onValue((newVal) => {
    await initChart(newVal);
  });
});

Key Changes:

  • Removed Unnecessary Line: Removed style.value = option_json.style;
  • Merged Style Updates: Used {...style.value, ...option_json.style} to merge styles safely.
  • Async Evaluation: Used try-catch block in case eval fails to prevent runtime errors.
  • Used Function API Safely: Wrapped eval call inside Function to avoid executing arbitrary code.

These changes should improve the robustness and maintainability of your component while maintaining its functionality.

@shaohuzhang1 shaohuzhang1 merged commit 6ddad88 into v2 Sep 9, 2025
3 of 6 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@v2@fix_echarts branch September 9, 2025 05:43
shaohuzhang1 added a commit that referenced this pull request Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant