LoginSignup
8
5

More than 3 years have passed since last update.

vue-chartjsで自動色割り当て

Last updated at Posted at 2019-01-29

まずvue-chartjsの簡単な記事はこちら

これは何?

Qiitaのマイページのグラフっぽいのを作りたくてvue-chartjsで自動色割り当ての方法を探していたらgoogle-paletteに行き着いたので忘備録

google-palette

インストール

プロジェクト配下で実行

$ npm i google-palette

使い方

グラフ本体

Chart.vue
<script>
import { Doughnut } from 'vue-chartjs'
import * as palette from 'google-palette'

export default {
  extends: Doughnut,
  data() {
    return {
      languages: ['Vue.js', 'C', 'C++', 'Python'],
      chartStatus: [40, 30, 20, 10]
    }
  },
  mounted: function() {
    this.renderChart(
      {
        labels: this.languages,
        datasets: [
          {
            backgroundColor: palette('mpn65', this.chartStatus.length).map(
              function(hex) {
                return '#' + hex
              }
            ),
            data: this.chartStatus
          }
        ]
      },
      { responsive: true, maintainAspectRatio: false }
    )
  }
}
</script>

ページ

index.vue
<template>
  <div>
    <chart-graph />
  </div>
</template>

<script>
import Vue from 'vue'
import Chart from '~/components/Chart'

export default {
  components: {
    Chart
  }
}

Vue.component('chart-graph', Chart)
</script>

ブラウザ

出ました。

スクリーンショット 2019-01-29 23.30.07.png

8
5
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
8
5