echarts 主題動態切換

Tinypan發表於2024-06-26

echarts 主題切換

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script type="importmap">
      {
        "imports": {
          "echarts": "http://localhost:8080/FrontEnd/assets/js/echarts/echarts.esm.js"
        }
      }
    </script>
    <style>
      #main {
        width: 500px;
        height: 300px;
        border: 1px solid red;
      }
      /**樣式仿照 elementui switch 元件*/
      .switch {
        height: 20px;
        font-size: 12px;
        display: inline-flex;
        align-items: center;
        line-height: 20px;
      }

      .switch-label {
        margin-left: 10px;
        font-weight: 500;
      }

      .switch-core {
        width: 40px;
        height: 20px;
        display: inline-block;
        cursor: pointer;
        background-color: #dcdfe6;
        border-radius: 10px;
        border: 1px solid #dcdfe6;
        box-sizing: border-box;
        position: relative;
      }
      .switch-core::after {
        content: '';
        position: absolute;
        top: 1px;
        left: 1px;
        width: 16px;
        height: 16px;
        border-radius: 100%;
        background-color: #fff;
        transition: all 0.3s;
      }
      .switch.is-checked .switch-label {
        color: #409eff;
      }
      .switch.is-checked .switch-core {
        border-color: rgb(24, 20, 50);
        background-color: rgb(24, 20, 50);
      }
      .switch.is-checked .switch-core::after {
        left: 100%;
        margin-left: -17px;
      }
    </style>
  </head>
  <body>
    <div class="switch">
      <span class="switch-core"></span>
      <span class="switch-label">深色模式</span>
    </div>
    <div id="main"></div>

    <script type="module">
      import * as echarts from 'echarts';

      const options = {
        title: {
          text: 'ECharts 入門示例',
        },
        tooltip: {},
        legend: {
          data: ['銷量'],
        },
        xAxis: {
          data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子'],
        },
        yAxis: {},
        series: [
          {
            name: '銷量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      };
      var myChart = echarts.init(document.getElementById('main'));
      myChart.setOption(options);

      let switchBtn = document.querySelector('.switch');
      switchBtn.addEventListener('click', function (e) {
        e.currentTarget.classList.toggle('is-checked');

        let darkMode = e.currentTarget.classList.contains('is-checked');

        // 官網例項中切換主題核心程式碼就是 dispose() 和 init()
        if (darkMode) {
          echarts.dispose(myChart);
          myChart = echarts.init(document.getElementById('main'), 'dark');
        } else {
          echarts.dispose(myChart);
          myChart = echarts.init(document.getElementById('main'));
        }

        myChart.setOption(options);
      });
    </script>
  </body>
</html>

相關文章