#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

関連記事

polyfill という宝物で開発時の ie11 対応した時のメモ
スクロールバー自由にカスタマイズできる vue-perfect-scrollbar について
値段をカンマと円マーク表示 vuejs money-format filter 自作
vue hook 使って vuejs コンポーネントライフサイクル監視
vuejs i18n の html-lang と font 設定
vuejs スクロールでナビバー表示非表示
nuxtjs vue router params query ルートから取得できるもの
vuejs 開発時に遭遇したエラーリスト
nuxt cordova web ハイブリッドアプリ作成した時のメモ
vuejs vuex が難しいわけではないけど覚えるのに時間かかる
vuejs で omisejapan の決済フォーム作成した時のメモ
vuejs-datepicker 使ってカレンダー表示 日本語多言語対応
laravel vuejs 初期設定