Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react chart.js

import { useState, useEffect, useRef } from "react";
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js";
import { Pie } from "react-chartjs-2";

ChartJS.register(ArcElement, Tooltip, Legend);

export const data = {
  labels: ["Case on Hold", "Submitted", "In Production", "Shipped"],
  datasets: [
    {
      label: "# of Votes",
      data: [12, 19, 3, 5],
      backgroundColor: ["#F2CC59", "#BA68C8", "#407BFF", "#E6E5E6"],
      borderColor: ["#F2CC59", "#BA68C8", "#407BFF", "#E6E5E6"],
      borderWidth: 1,
    },
  ],
};

const pieOptions = {
  plugins: {
    legend: {
      display: false,
      labels: {
        font: {
          size: 12,
        },
      },
    },
  },
};

export default function ChartView() {
  const [charView, setChatView] = useState<any>([]);
  const summaryRef: any = useRef(null);

  useEffect(() => {
    setChatView(summaryRef?.current?.legend?.legendItems);
  }, []);

  return (
    <div>
		<Pie data={data} options={pieOptions} ref={summaryRef} />    
        {charView?.map((data: any, i: number) => (
          <Box display="flex" sx={{ mt: 2 }} key={i}>
                        <Box
              sx={{
                  height: 16,
                  width: 16,
                  background: `${data?.fillStyle}`,
                  borderRadius: 5,
                  mr: 0.5,
                  }}
              />
              	<Typography variant="body2"> {data?.text}</Typography>
              </Box>
		))}
    </div>
  );
}
Comment

chart js react

yarn add react-chartjs-2 chart.js // npm install  react-chartjs-2 chart.js
Comment

PREVIOUS NEXT
Code Example
Javascript :: socket.io how do i get a list of connected sockets clients 
Javascript :: routes in node js 
Javascript :: sfc in react 
Javascript :: mapgetters with parameter 
Javascript :: create new angular project with specific version 
Javascript :: js char array to string 
Javascript :: ngrok live port vue js 
Javascript :: sorting in javascript 
Javascript :: mapbox remove marker 
Javascript :: js does object contain value 
Javascript :: react add class to each children 
Javascript :: javascript explode space 
Javascript :: encrypt decrypt in vanilla javascript 
Javascript :: javascript count digits 
Javascript :: multiply arrays javascript 
Javascript :: how to remove quotes using regex 
Javascript :: reactjs node sass incompatible with ^4.0.0 ^5.0.0 
Javascript :: enzyme-adapter-react-17 
Javascript :: using fetch api 
Javascript :: regex must match exactly 
Javascript :: api fetch in react js 
Javascript :: vuetify autocomplete get input value 
Javascript :: foreach in javascript skip first 
Javascript :: angular router outlet 
Javascript :: javascript how to take off a decimal 
Javascript :: react date format 
Javascript :: jshint ignore line 
Javascript :: Addition aruments in javascript 
Javascript :: javascript number format indian currency 
Javascript :: remove letter until vowel javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =