본문 바로가기

프론트엔드/리액트

리액트로 radar chart 구현하기, 변형하기 (chart.js)

 Radar chart 구현

 

 

 

 설치하기

 

npm install react-chartjs-2 chart.js

 

 

 import 하기

 

import {

  Chart as ChartJS,

  RadialLinearScale,

  PointElement,

  LineElement,

  Filler,

  Tooltip,

  Legend,

} from "chart.js";

import { Radar } from "react-chartjs-2";

 

 Data set

 

export const data = {

  labels: [

    "가",

    "나",

    "다",

    "라",

    "마",

    "바",

  ],

  datasets: [

    {

      label: "그램그램",

      data: [1, 1, 1, 1, 1, 1],

      fill: true,                      // 선 안쪽 색상 채워짐 

      backgroundColor: "rgba(98, 181, 255, 0.2)",

      borderColor: "#377EF8",

      pointRadius: 0,

      pointHoverBackgroundColor: "#fff",

      pointHoverBorderColor: "#377EF8",

    },

  ],

};

 

Option set

 

const options = {

  responsive: false,

  scales: {

    r: {

      grid: {

        circular: true,

      },

      pointLabels: {

        font: {

          size: 13,

        },

        color: [

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

        ],

      },

    },

  },

  scale: {

    beginAtZero: true,

    max: 3,

    min: 0,

    stepSize: 1,

  },

  plugins: {        //꼭다리

    legend: {

      display: false,

    },

  },

  elements: {      //차트요소 설정

    line: {

      borderWidth: 1, //선의 굵기

      //tension: 3,       //선의 굴곡 비율

    },

  },

};

 

 

 

전체코드

 

import React from "react";

import {

  Chart as ChartJS,

  RadialLinearScale,

  PointElement,

  LineElement,

  Filler,

  Tooltip,

  Legend,

} from "chart.js";

import { Radar } from "react-chartjs-2";

 

ChartJS.register(

  RadialLinearScale,

  PointElement,

  LineElement,

  Filler,

  Tooltip,

  Legend

);

 

export const data = {

  labels: ["가", "나", "다", "라", "마", "바"],

 

  datasets: [

    {

      label: "그램그램",

 

      data: [1, 1, 1, 1, 1, 1],

 

      fill: true, // 안쪽 색상 채워짐

 

      backgroundColor: "rgba(98, 181, 255, 0.2)",

 

      borderColor: "#377EF8",

 

      pointRadius: 0,

 

      pointHoverBackgroundColor: "#fff",

 

      pointHoverBorderColor: "#377EF8",

    },

  ],

};

const options = {

  responsive: false,

  scales: {

    r: {

      grid: {

        circular: true,

      },

      pointLabels: {

        font: {

          size: 13,

        },

        color: [

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

          "#A6A6A6",

        ],

      },

    },

  },

  scale: {

    beginAtZero: true,

    max: 3,

    min: 0,

    stepSize: 1,

  },

  plugins: {

    //꼭다리

    legend: {

      display: false,

    },

  },

  elements: {

    //차트요소 설정

    line: {

      borderWidth: 1, //선의 굵기

      //tension: 3,       //선의 굴곡 비율

    },

  },

};

 

export default function App() {

  return <Radar data={data} options={options} width={500} height={500} />;

}

 

 

 

 

 

결과물