echarts option Json 生成

echarts option json 生成

  1. // ==UserScript==
  2. // @name echarts option Json 生成
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.0
  5. // @description echarts option json 生成
  6. // @author aries.zhou
  7. // @match https://echarts.apache.org/examples/en/editor.html?**
  8. // @match https://echarts.apache.org/examples/zh/editor.html?**
  9. // @icon https://www.google.com/s2/favicons?domain=apache.org
  10. // @grant GM_setClipboard
  11. // ==/UserScript==
  12.  
  13.  
  14. function sleep(ms) {
  15. return new Promise(resolve => setTimeout(resolve, ms));
  16. }
  17.  
  18. window.addEventListener('load', async function () {
  19. await sleep(1000);
  20. const container = document.getElementsByClassName('left-panel').item(0);
  21. //add text
  22. {
  23. const newText = document.createElement("textarea");
  24. newText.style.height = '100px';
  25. newText.style.width = '500px';
  26. newText.cols = 10;
  27. newText.value = '数据';
  28. newText.className = 'newoptionjson';
  29. newText.id = 'newoptionjson';
  30. container.appendChild(newText);
  31. }
  32.  
  33. //set text value
  34. {
  35. const textBox = document.getElementsByClassName('newoptionjson').item(0);
  36. var chart = echarts.getInstanceByDom(document.getElementsByClassName('chart-container').item(0));
  37.  
  38. try {
  39. textBox.value = JSON.stringify(chart.getOption());
  40. } catch (exception_var) {
  41. console.log(exception_var);
  42. JSON.safeStringify = (obj, indent = 2) => {
  43. let cache = [];
  44. const retVal = JSON.stringify(
  45. obj,
  46. (key, value) =>
  47. typeof value === "object" && value !== null
  48. ? cache.includes(value)
  49. ? undefined // Duplicate reference found, discard key
  50. : cache.push(value) && value // Store value in our collection
  51. : value,
  52. indent
  53. );
  54. cache = null;
  55. return retVal;
  56. };
  57. textBox.value = JSON.safeStringify(chart.getOption());
  58. } finally {
  59. console.log("finally");
  60. }
  61. }
  62. //copy
  63. {
  64. await sleep(1000);
  65. document.getElementById('newoptionjson').select();
  66. var targetText = document.getElementsByClassName('newoptionjson').item(0);
  67. GM_setClipboard(targetText.value);
  68. }
  69.  
  70. }, false);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址