๐Ÿ‘ฉ‍๐Ÿ’ป/Vue

[Vue-todo-list] #4 chart.js๋กœ todo data ์‹œ๊ฐํ™”ํ•˜๊ธฐ (2) - Pie Chart

ํ•œ๋‚˜ 2021. 2. 8. 02:42

2021/02/08 - [๐Ÿ‘ฉ‍๐Ÿ’ป/Vue.js] - [Vue-todo-list] #3 chart.js๋กœ todo data ์‹œ๊ฐํ™”ํ•˜๊ธฐ (1) - Line Chart

2021/02/08 - [๐Ÿ‘ฉ‍๐Ÿ’ป/Vue.js] - [Vue-todo-list] #2 ์นดํ…Œ๊ณ ๋ฆฌ๊ฐ€ ์žˆ๋Š” Todo List ๋งŒ๋“ค๊ธฐ

2021/02/08 - [๐Ÿ‘ฉ‍๐Ÿ’ป/Vue.js] - [Vue-todo-list] #1 Date ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๋‹ฌ๋ ฅ ์ˆซ์ž ๊ตฌํ•˜๊ธฐ/์ง€๋‚œ ๋‚ ์งœ, ์˜ค๋Š˜ ๋‚ ์งœ ๊ตฌ๋ณ„์„ ์œ„ํ•œ CSS ์Šคํƒ€์ผ๋ง

 


Pie chart๋Š” ์ด์ „ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค˜๋˜ Line chart์— ๋น„ํ•ด ์—„์ฒญ ์—„์ฒญ ์‰ฌ์šด ํŽธ์— ์†ํ•ด ํฌ๊ฒŒ ๊ณ ๋ฏผํ•˜์ง€ ์•Š๊ณ  import ํ•ด์ค€ ๋’ค ๋ฐ”๋กœ ์ผ๋‹ค.

 

import { Vue } from "vue-property-decorator";
import { Pie } from "vue-chartjs";

export default Vue.extend({
  extends: Pie,
  data: () => ({
    msg: "๐ŸŽจ My Pie Stats",
    stats: [],
    chartdata: {
      labels: [],
      datasets: [
        {
          label: [],
          data: [],
          backgroundColor: []
        }
      ]
    },
    options: {
      hoverBorderWidth: 20
    }
  }),
  mounted() {
    this.renderChart(this.chartdata, this.options);
  },
  created() {
    this.stats = this.$store.state.categories;
    const obj = this.chartdata.datasets[0];

    for (const stat of this.stats) {
      this.chartdata.labels.push(stat.title);
      obj.label.push(stat.title);
      obj.data.push(stat.value);
      obj.backgroundColor.push(stat.backgroundColor);
    }
  }
});

ํฌ๋งท์„ ๋ณด๋ฉด chart.js ๊ณต์‹ documnet์—์„œ ๋ณต์‚ฌํ•ด์˜จ ๊ตฌ์กฐ์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๊ณ , datasets ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋งŒ reactive ํ•˜๊ฒŒ ์—ฐ๋™ํ•ด์ฃผ๊ธฐ๋งŒ ํ–ˆ๋‹ค.

renderChart() ๋ฉ”์„œ๋“œ๊ฐ€ mounted์—์„œ ์‹คํ–‰๋˜๊ธฐ ์ „ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค์€ created() ํ›…์—์„œ ์ƒ์„ฑ๋˜๋„๋ก ํ•œ๋‹ค.

 

ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋Š” category์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ด์—ˆ์œผ๋‹ˆ store์— ์ €์žฅ๋œ categories๋ฅผ this.$store.state.categories๋กœ ์ ‘๊ทผํ•œ๋‹ค. todo page์—์„œ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋Œ€ํ•œ ์ปฌ๋Ÿฌ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ์œผ๋ฏ€๋กœ, ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด ๋ฐ”๋กœ chart ์ชฝ์—์„œ๋„ ๋ฐ˜์˜๋œ๋‹ค.

 

๐Ÿ’Œ PieChart.js ์ปดํฌ๋„ŒํŠธ์˜ ์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ๋Š” ์ด๊ณณ์—์„œ