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

207
Visualizações
Plotting bars with react with different colors, but they're all in black why?

I want to plot my historgram bar with different colors for A, B, C but I have my 3 bars with the same color i.e black, I guess it's in the fill field issue. If I plot only one bar then it's printed wirth the right color, but when it's more than 1 then it's all black. I'm using recharts

Any idea please ?

export default function Overview() {
    const { ratingBar } = graphs ?? {}

    const COLORS = {
        'A': 'red',
        'B': 'green',
        'C': 'orange',
    }

    function ratingGraph() {
        const data = ratingBar
        return ({
            graph: {
                type: 'bar',
                showTooltip: true,
                showXAxis: true,
                showYAxis: true,
                showLegend: true,
                container: {
                    data,
                },
                bars: [
                    {
                        dataKey: 'count',
                        fill: COLORS[data.map((m) => m.title)],
                    }
                ],
                xaxis: {
                    dataKey: 'title',
                    tick: { style: { fontSize: '.9rem' } },
                },
                yaxis: {
                    dataKey: 'count',
                    domain: [0, 100],
                },
            },
        }).graph
    }

    return (
        <div >

            {...ratingGraph()}

        </div>
    )
}

Here is my data from my api :

{
    "ratingBar": [
        {
            "title": "A",
            "count": 2
        },
        {
            "title": "B",
            "count": 48
        },
        {
            "title": "C",
            "count": 78
        }
    ]
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you have to use cells property :

  cells: data.map(m => ({fill: COLORS[m.title]}))
about 4 years ago · Juan Pablo Isaza Relatório

0

fill: data.map(m => COLORS[m.title])

Yours map is wrong. I guess the fill expects an array of colors. But you are accessing COLORS key with a mapped array which will be always undefined.

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