graphicRemove.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <script src="lib/esl.js"></script>
  6. <script src="lib/config.js"></script>
  7. <script src="lib/jquery.min.js"></script>
  8. <script src="lib/facePrint.js"></script>
  9. <script src="lib/testHelper.js"></script>
  10. <meta name="viewport" content="width=device-width, initial-scale=1" />
  11. <link rel="stylesheet" href="lib/reset.css">
  12. </head>
  13. <body>
  14. <style>
  15. h1 {
  16. line-height: 60px;
  17. height: 60px;
  18. background: #146402;
  19. text-align: center;
  20. font-weight: bold;
  21. color: #eee;
  22. font-size: 14px;
  23. }
  24. #main {
  25. width: 100%;
  26. height: 600px;
  27. }
  28. </style>
  29. 宽度大于600时显示方块,当前宽度:<span id='wid'></span>
  30. <div id="main"></div>
  31. <script>
  32. var echarts;
  33. var chart;
  34. var myChart;
  35. var groupCategories = [];
  36. var groupColors = [];
  37. require([
  38. 'echarts'
  39. // 'echarts/component/graphic'
  40. ], function (ec) {
  41. echarts = ec;
  42. var option = {
  43. graphic: {
  44. id: 'text1',
  45. type: 'rect',
  46. shape: {
  47. x:0,
  48. y:0,
  49. width: 100,
  50. height: 100,
  51. },
  52. style: {
  53. fill: 'red',
  54. }
  55. }
  56. };
  57. var myChart = echarts.init(document.getElementById('main'));
  58. myChart.setOption(option);
  59. setTimeout(function () {
  60. // 删除上例中定义的 'text1' 元素。
  61. myChart.setOption({
  62. graphic: {
  63. id: 'text1',
  64. $action: 'remove'
  65. }
  66. });
  67. // 已经删除,此步应无效。
  68. // myChart.setOption({
  69. // graphic: {
  70. // id: 'text1',
  71. // $action: 'remove'
  72. // }
  73. // });
  74. // 删除后再添加。
  75. myChart.setOption({
  76. id: 'text1',
  77. type: 'rect',
  78. shape: {
  79. x:0,
  80. y:0,
  81. width: 100,
  82. height: 100,
  83. },
  84. style: {
  85. fill: 'red',
  86. }
  87. });
  88. }, 1000);
  89. var hasRect;
  90. window.onresize = function() {
  91. document.getElementById('wid').innerText = window.innerWidth;
  92. // var option = myChart.getOption();
  93. var option = {};
  94. if (window.innerWidth < 600) {
  95. if (hasRect) {
  96. option.graphic = { // 删除上例中定义的 'text1' 元素。
  97. id: 'text1',
  98. $action: 'remove'
  99. }
  100. hasRect = false;
  101. }
  102. }
  103. else {
  104. option.graphic = {
  105. id: 'text1',
  106. type: 'rect',
  107. shape: {
  108. x:0,
  109. y:0,
  110. width: 100,
  111. height: 100,
  112. },
  113. style: {
  114. fill: 'red',
  115. }
  116. };
  117. hasRect = true;
  118. }
  119. myChart.setOption(option);
  120. }
  121. // window.onresize();
  122. });
  123. </script>
  124. </body>
  125. </html>