開始使用

取得 Apache ECharts

Apache ECharts 支援多種下載方式,在下一個教程安裝中有更詳細的說明。在這裡,我們以從 jsDelivr CDN 取得為例,並說明如何快速安裝它。

https://www.jsdelivr.com/package/npm/echarts 中選擇 dist/echarts.js,點擊並將其另存為 echarts.js 檔案。

關於這些檔案的更多資訊,請參閱下一個教程安裝

引入 ECharts

在您剛才儲存 echarts.js 的目錄中建立一個新的 index.html 檔案,內容如下

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <!-- Include the ECharts file you just downloaded -->
    <script src="echarts.js"></script>
  </head>
</html>

當您開啟此 index.html 時,您會看到一個空白頁面。但別擔心。開啟控制台並確保沒有報告任何錯誤訊息,然後您可以繼續下一步。

繪製簡單圖表

在繪製之前,我們需要為 ECharts 準備一個 DOM 容器,並定義其高度和寬度。將以下程式碼加入到前面介紹的 </head> 標籤之後。

<body>
  <!-- Prepare a DOM with a defined width and height for ECharts -->
  <div id="main" style="width: 600px;height:400px;"></div>
</body>

然後您可以使用 echarts.init 方法初始化一個 echarts 實例,並使用 setOption 方法設定 echarts 實例,以生成一個簡單的長條圖。這是完整的程式碼。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>ECharts</title>
    <!-- Include the ECharts file you just downloaded -->
    <script src="echarts.js"></script>
  </head>
  <body>
    <!-- Prepare a DOM with a defined width and height for ECharts -->
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
      // Initialize the echarts instance based on the prepared dom
      var myChart = echarts.init(document.getElementById('main'));

      // Specify the configuration items and data for the chart
      var option = {
        title: {
          text: 'ECharts Getting Started Example'
        },
        tooltip: {},
        legend: {
          data: ['sales']
        },
        xAxis: {
          data: ['Shirts', 'Cardigans', 'Chiffons', 'Pants', 'Heels', 'Socks']
        },
        yAxis: {},
        series: [
          {
            name: 'sales',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      };

      // Display the chart using the configuration items and data just specified.
      myChart.setOption(option);
    </script>
  </body>
</html>

這就是您的第一個 Apache ECharts 圖表!

貢獻者 在 GitHub 上編輯此頁面

plainheart plainheartOvilia Oviliarandyl randylpissang pissang