LoginSignup
0
0

More than 1 year has passed since last update.

Apache EChartsを使ってグラフを書いてみる! ~1日10行コーディング~

Posted at

概要

1日10行コーディングとは

まず、1日10行コーディングというのは、1日10行でいいから新しいコード書こうよって感じのやつです(雑)
(1日1記事とか1機能とかだと続かない気がしてしまってる...)

実装するもの

とあるソシャゲのイベント走った記録とかができるサービスを作りたい。

今日はその1つの機能のグラフ表示部分を作ってみたいと思う。

手順

  1. echartsのjsファイルをダウンロード
    https://www.jsdelivr.com/package/npm/echarts?path=dist
    こちらのページのdist配下のecharts.jsを保存する。

  2. HTMLを記述
    下記は公式にあるものを参考に記載

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="echarts.js"></script>
        <title>グラフ</title>
    </head>
    <body>
        <div id="main" style="width: 800px;height:400px;"></div>
    </body>
    </html>
    

    上記のようにheadタグで1.で保存したecharts.jsを読み込むように定義して、グラフを表示したいエリアをdivで定義する。

  3. JSでグラフの描画を記述

    
    // 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);
    
  4. 開いてみる
    スクリーンショット 2022-02-14 19.05.30.png
    上のタイトル部分とラベル部分がかぶってるが一旦表示はできた。

  5. かぶらないように修正
    場所がかぶってるのはwidthの値のせいだったから

    <div id="main" style="width: 800px;height:400px;"></div>
    

    こうすることで解消。

  6. 棒グラフにする
    棒グラフにはオプション変えるだけでできた

        // 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: {
            trigger: 'item'
          },
          legend: {
            data: ['sales']
          },
          xAxis: {
            data: ['1/11', '1/12', '1/13', '1/14', '1/15', '1/16']
          },
          yAxis: {},
          series: [
            {
              name: 'sales',
              type: 'line',
              data: [1000, 2000, 3000, 3000, 5000, 6000]
            }
          ]
        };
    
        // Display the chart using the configuration items and data just specified.
        myChart.setOption(option);
    

    スクリーンショット 2022-02-14 19.26.48.png

棒グラフできた!!
以上!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0