Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Im trying to generate bar chart.js

When I create a line chart with chart.js everything works fine, but when I try to create a bar chart it shows a white background. Please help. This is my working code (Line Chart.js)

import React from 'react';
import { useEffect, useState } from "react";
import { Line, Bar } from 'react-chartjs-2';

export function BarChart() {
   const [chartData, chartDataSet] = useState(null);

   useEffect( () => {
           const data = {
             labels: ['n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7'],
             datasets: [{
               label: 'My First Dataset',
               data: [65, 59, 80, 81, 56, 55, 40],
               fill: false,
               borderColor: 'rgb(75, 192, 192)',
               tension: 0.1
             }]
           };
           chartDataSet(data);
   }, []);
   if (!chartData) {
       return null;
   }
   return <Line data={chartData}/>;
}

This code don't work and generate nothing (bar chart.js):

import React from 'react';
import { useEffect, useState } from "react";
import { Line, Bar } from 'react-chartjs-2';

export function BarChart() {
   const [chartData, chartDataSet] = useState(null);

   useEffect( () => {
           const data = {
             labels: ['n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7'],
             datasets: [{
               label: 'My First Dataset',
               data: [65, 59, 80, 81, 56, 55, 40],
               fill: false,
               borderColor: 'rgb(75, 192, 192)',
               tension: 0.1
             }]
           };
           chartDataSet(data);
   }, []);
   if (!chartData) {
       return null;
   }
   return <Bar data={chartData}/>;
}

I run everything in a new file at this place:

export function Mytest() {

   return (
          <div className="App">
              <BarChart/>
          </div>

   );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Problem solved. You had to add some extra imports and add them to the register.

Working code (bar chart.js):

import React from 'react';
import {Bar} from 'react-chartjs-2';
import {
  Chart as ChartJS,
  CategoryScale,
  Title,
  Tooltip,
  Legend,
  BarElement,
  BarController
} from 'chart.js'
import { Chart } from 'react-chartjs-2'

ChartJS.register(
  CategoryScale,
  Title,
  Tooltip,
  Legend,
  BarElement,
  BarController
)
const data = {
  labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  datasets: [
    {
      label: 'My First dataset',
      backgroundColor: 'rgba(255,99,132,0.2)',
      borderColor: 'rgba(255,99,132,1)',
      borderWidth: 1,
      hoverBackgroundColor: 'rgba(255,99,132,0.4)',
      hoverBorderColor: 'rgba(255,99,132,1)',
      data: [65, 59, 80, 81, 56, 55, 40]
    }
  ]
};
export function BarChart() {
  return (
    <div>
        <h2>Bar Example (custom size)</h2>
        <Bar
          data={data}
          width={100}
          height={50}
          options={{
            maintainAspectRatio: true
          }}
        />
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda