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

191
Visualizações
Generate graph with dynamic dataKey in Rechart

Base on following example code, I have created a Line chart.

const {LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend} = Recharts;
const data1 = [
      {time: 10,  pv: 2400}, 
      {time: 10, amt: 1300},
      {time: 20,  pv: 500}, 
      {time: 20, amt: 1350},
      {time: 30, pv: 3200}, 
      {time: 30, amt: 4400},
];

  
const SimpleLineChart = React.createClass({
    render () {
    return (
        <LineChart width={600} height={300}
            margin={{top: 5, right: 30, left: 20, bottom: 5}}>
       <XAxis type="number" dataKey="name" domain={['auto', 'auto']} />
       <YAxis/>
       <CartesianGrid strokeDasharray="3 3"/>
       <Tooltip/>
       <Legend />
       <Line type="monotone" data={data1} dataKey="pv" stroke="#8884d8" activeDot={{r: 8}}/>
      </LineChart>
    );
  }
})

ReactDOM.render(
  <SimpleLineChart />,
  document.getElementById('container')
);

But my actual requirement is to pass 'dataKey' ( In here 'pv', 'amt' so on) dynamically without hard coding like 'pv' or 'amt'.

response_arr.forEach((item, index) => {

 var category = response_arr[index].category;

 newArray.push({ time: response_arr[index].dateTime, {this.category}: response_arr[index].value });
})

However, the above code snippet (this.category) is not allowed in javascript. Can anybody help me to achieve this problem.

about 4 years ago · Juan Pablo Isaza
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