Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ui/src/components/markdown/EchartsRander.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const chartsRef = ref()

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

function initChart() {
Expand Down Expand Up @@ -44,7 +44,7 @@ function evalParseOption(option_json: any) {
if (option_json.style) {
style.value = option_json.style
}
const option = {}
let option = {}
tmp.value = echarts
eval(option_json.option)
return option
Expand All @@ -62,7 +62,7 @@ watch(
initChart()
})
}
}
},
)

onMounted(() => {
Copy link
Copy Markdown
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.

Expand Down
Loading