# chartjs でチャートコンポネント作ってみた時のメモ bar+line+pie

# index.vue

<template>
    <div>
        <barchart :labels="bar.labels" :datasets="bar.datasets"/>
        <linechart :labels="line.labels" :datasets="line.datasets"/>
        <piechart :labels="pie.labels" :datasets="pie.datasets"/>
    </div>
</template>

<script>
import barchart from "@/components/jschart/bar";
import linechart from "@/components/jschart/line";
import piechart from "@/components/jschart/pie";

export default {
    components: {
        barchart,
        linechart,
        piechart,
    },
    data() {
    return {
      bar: {
        labels: [
          "January",
          "February",
          "March",
          "April",
          "May",
          "June",
          "July",
          "August",
          "September",
          "October",
          "November",
          "December"
        ],
        datasets: [
          {
            label: "Git",
            data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11],
            backgroundColor: "rgba(255, 99, 132, 0.8)"
          }
        ]
      },
      line: {
        labels: [
          "January",
          "February",
          "March",
          "April",
          "May",
          "June",
          "July",
          "August",
          "September",
          "October",
          "November",
          "December"
        ],
        datasets: [
          {
            label: "Git",
            data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11],
            backgroundColor: "rgba(255, 99, 132, 0.8)",
            borderColor: "red"
          },
          {
            label: "Git",
            data: [20, 30, 14, 19, 12, 14, 9, 18, 30, 20, 32, 110],
            backgroundColor: "green",
            borderColor: "green"
          }
        ]
      },
      pie: {
        labels: [
          "January",
          "February",
          "March",
          "April",
          "May",
          "June",
          "July",
          "August",
          "September",
          "October",
          "November",
          "December"
        ],
        datasets: [
          {
            label: "Git",
            data: [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11],
            backgroundColor: [
              "rgba(255,167,0, 0.8)",
              "rgba(55, 126, 184, 0.8)",
              "rgba(102, 166, 30, 0.8)",
              "rgba(152, 78, 163, 0.8)",
              "rgba(0, 210, 213, 0.8)",
              "rgba(255, 127, 0, 0.8)",
              "rgba(175, 141, 0, 0.8)",
              "rgba(127, 128, 205, 0.8)",
              "rgba(179, 233, 0, 0.8)",
              "rgba(196, 46, 96, 0.8)",
              "rgba(166, 86, 40, 0.8)",
              "rgba(247, 129, 191, 0.8)"
            ]
          },
          {
            label: "Git",
            data: [20, 30, 14, 19, 12, 14, 9, 18, 30, 20, 32, 110],
            backgroundColor: [
              "rgba(255,167,0, 0.8)",
              "rgba(55, 126, 184, 0.8)",
              "rgba(102, 166, 30, 0.8)",
              "rgba(152, 78, 163, 0.8)",
              "rgba(0, 210, 213, 0.8)",
              "rgba(255, 127, 0, 0.8)",
              "rgba(175, 141, 0, 0.8)",
              "rgba(127, 128, 205, 0.8)",
              "rgba(179, 233, 0, 0.8)",
              "rgba(196, 46, 96, 0.8)",
              "rgba(166, 86, 40, 0.8)",
              "rgba(247, 129, 191, 0.8)"
            ]
          }
        ]
      }
    };
  }
};
</script>

# components

# 🎉 bar

<script>
import { Bar } from "vue-chartjs";

export default {
  extends: Bar,
  props: {
    labels: Array,
    datasets: Array
  },
  mounted() {
    // Overwriting base render method with actual data.
    this.renderChart({
      labels: this.labels,
      datasets: this.datasets
    });
  }
};
</script>

<style>
</style>

# 🎉 line


<script>
import { Line } from "vue-chartjs";

export default {
  extends: Line,
  props: {
    labels: Array,
    datasets: Array,
    options: {
      type: Object,
      default: () => {
        return {
          elements: {
            line: {
              fill: false,
              borderCapStyle: "round",
              borderJoinStyle: "round",
              tension: 0
            }
          },
          responsive: true,
          maintainAspectRatio: false,
          segmentStrokeColor: "transparent"
        };
      }
    }
  },
  mounted: function() {
    this.renderChart(
      {
        labels: this.labels,
        datasets: this.datasets
      },
      this.options
    );
  }
};
</script>

# 🎉 pie


<script>
import { Doughnut } from "vue-chartjs";

export default {
  extends: Doughnut,
  props: {
    labels: Array,
    datasets: Array,
    options: {
      type: Object,
      default: () => {
        return {
          elements: {
            line: {
              fill: false,
              borderCapStyle: "round",
              borderJoinStyle: "round",
              tension: 0
            }
          },
          responsive: true,
          maintainAspectRatio: false,
          segmentStrokeColor: "transparent"
        };
      }
    }
  },
  mounted: function() {
    this.renderChart(
      {
        labels: this.labels,
        datasets: this.datasets
      },
      this.options
    );
  }
};
</script>

2019-06-01
  • vuejs

関連記事

Nuxtjs 3 に Ant design Vue を使う
JavaScript ライブラリ aos.js 使ってスクロール連動アニメーション
vuejs back to top button component 作成
bootstrap vuejs 使って generate する際に babel が icons ファイル max 500KB エラー
Nuxtjs 動的なルーティング静的ページ自動生成
vuejs v-if と v-show の違い
vuejs で omisejapan の決済フォーム作成した時のメモ
vuejs vuex が難しいわけではないけど覚えるのに時間かかる
vuejs スクロールでナビバー表示非表示
nuxtjs vue router params query ルートから取得できるもの
vuejs 開発時に遭遇したエラーリスト
javascript reduce 連想配列の合計計算覚えよう
値段をカンマと円マーク表示 vuejs money-format filter 自作
vuejs-datepicker 使ってカレンダー表示 日本語多言語対応
スクロールバー自由にカスタマイズできる vue-perfect-scrollbar について
vue hook 使って vuejs コンポーネントライフサイクル監視
Javascript vuejs の validation 正規表現でフォームチェック作ったときのメモ
javascript password generator ランダム文字列パスワード作成
nuxt cordova web ハイブリッドアプリ作成した時のメモ
laravel に vuejs 使うための初期設定
polyfill という宝物で開発時の ie11 対応した時のメモ