donut-update.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Simple Pie</title>
  8. <link href="../../assets/styles.css" rel="stylesheet" />
  9. <style>
  10. #chart {
  11. max-width: 480px;
  12. margin: 35px auto;
  13. padding: 0;
  14. }
  15. .actions {
  16. top: -10px;
  17. position: relative;
  18. z-index: 10;
  19. max-width: 400px;
  20. margin: 0 auto;
  21. }
  22. button {
  23. color: #fff;
  24. background: #20b2aa;
  25. padding: 5px 10px;
  26. margin: 2px;
  27. font-weight: bold;
  28. font-size: 13px;
  29. border-radius: 5px;
  30. }
  31. p {
  32. margin: 10px 0;
  33. }
  34. @media only screen and (max-width: 480px) {
  35. .actions {
  36. margin-top: 0;
  37. left: 0
  38. }
  39. }
  40. </style>
  41. <script>
  42. window.Promise ||
  43. document.write(
  44. '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
  45. )
  46. window.Promise ||
  47. document.write(
  48. '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
  49. )
  50. window.Promise ||
  51. document.write(
  52. '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
  53. )
  54. </script>
  55. <script src="https://cdn.jsdelivr.net/npm/react@16.12/umd/react.production.min.js"></script>
  56. <script src="https://cdn.jsdelivr.net/npm/react-dom@16.12/umd/react-dom.production.min.js"></script>
  57. <script src="https://cdn.jsdelivr.net/npm/prop-types@15.7.2/prop-types.min.js"></script>
  58. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
  59. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  60. <script src="https://cdn.jsdelivr.net/npm/react-apexcharts@1.3.6/dist/react-apexcharts.iife.min.js"></script>
  61. </head>
  62. <body>
  63. <div id="app"></div>
  64. <div id="html">
  65. &lt;div&gt;
  66. &lt;div class=&quot;chart-wrap&quot;&gt;
  67. &lt;div id=&quot;chart&quot;&gt;
  68. &lt;ReactApexChart options={this.state.options} series={this.state.series} type=&quot;donut&quot; width={380} /&gt;
  69. &lt;/div&gt;
  70. &lt;/div&gt;
  71. &lt;div class=&quot;actions&quot;&gt;
  72. &lt;button
  73. onClick={() =&gt; this.appendData()}
  74. &gt;
  75. + ADD
  76. &lt;/button&gt;
  77. &amp;nbsp;
  78. &lt;button
  79. onClick={() =&gt; this.removeData()}
  80. &gt;
  81. - REMOVE
  82. &lt;/button&gt;
  83. &amp;nbsp;
  84. &lt;button
  85. onClick={() =&gt; this.randomize()}
  86. &gt;
  87. RANDOMIZE
  88. &lt;/button&gt;
  89. &amp;nbsp;
  90. &lt;button
  91. onClick={() =&gt; this.reset()}
  92. &gt;
  93. RESET
  94. &lt;/button&gt;
  95. &lt;/div&gt;
  96. &lt;/div&gt;
  97. </div>
  98. <script type="text/babel">
  99. class ApexChart extends React.Component {
  100. constructor(props) {
  101. super(props);
  102. this.state = {
  103. series: [44, 55, 13, 33],
  104. options: {
  105. chart: {
  106. width: 380,
  107. type: 'donut',
  108. },
  109. dataLabels: {
  110. enabled: false
  111. },
  112. responsive: [{
  113. breakpoint: 480,
  114. options: {
  115. chart: {
  116. width: 200
  117. },
  118. legend: {
  119. show: false
  120. }
  121. }
  122. }],
  123. legend: {
  124. position: 'right',
  125. offsetY: 0,
  126. height: 230,
  127. }
  128. },
  129. };
  130. }
  131. appendData() {
  132. var arr = this.state.series.slice()
  133. arr.push(Math.floor(Math.random() * (100 - 1 + 1)) + 1)
  134. this.setState({
  135. series: arr
  136. })
  137. }
  138. removeData() {
  139. if(this.state.series.length === 1) return
  140. var arr = this.state.series.slice()
  141. arr.pop()
  142. this.setState({
  143. series: arr
  144. })
  145. }
  146. randomize() {
  147. this.setState({
  148. series: this.state.series.map(function() {
  149. return Math.floor(Math.random() * (100 - 1 + 1)) + 1
  150. })
  151. })
  152. }
  153. reset() {
  154. this.setState({
  155. series: [44, 55, 13, 33]
  156. })
  157. }
  158. render() {
  159. return (
  160. <div>
  161. <div>
  162. <div class="chart-wrap">
  163. <div id="chart">
  164. <ReactApexChart options={this.state.options} series={this.state.series} type="donut" width={380} />
  165. </div>
  166. </div>
  167. <div class="actions">
  168. <button
  169. onClick={() => this.appendData()}
  170. >
  171. + ADD
  172. </button>
  173. &nbsp;
  174. <button
  175. onClick={() => this.removeData()}
  176. >
  177. - REMOVE
  178. </button>
  179. &nbsp;
  180. <button
  181. onClick={() => this.randomize()}
  182. >
  183. RANDOMIZE
  184. </button>
  185. &nbsp;
  186. <button
  187. onClick={() => this.reset()}
  188. >
  189. RESET
  190. </button>
  191. </div>
  192. </div>
  193. <div id="html-dist"></div>
  194. </div>
  195. );
  196. }
  197. }
  198. const domContainer = document.querySelector('#app');
  199. ReactDOM.render(React.createElement(ApexChart), domContainer);
  200. </script>
  201. </body>
  202. </html>