Home  >  Q&A  >  body text

React line chart with hours removed

<p>I want to remove every three hours from the bottom of the rope chart. I'm using react-chart-js2. I'm using the .map function to display these numbers. Is it possible to display only every three of them? Chart</p> <p>Here is the code that displays the hours: </p> <pre class="brush:php;toolbar:false;">const data = { labels: chart.prices?.map(x => moment(x[0]).format('h:mm a')), datasets: [{ data: chart.prices?.map(x => x[1]), backgroundColor: '#ffffff00', borderColor: 'rgb(79 70 229)', borderWidth: '2', pointBorderColor: '#ffffff00', tension: 0.4, fill: { target: "origin", // 3. Set the fill options above: "rgba(79, 70, 229, 0.0625)" } }] }</pre> <p>I want only every third number to be displayed</p>
P粉828463673P粉828463673400 days ago446

reply all(1)I'll reply

  • P粉068174996

    P粉0681749962023-08-19 11:48:31

    Replace every third item with an empty string.

    let hours = chart.prices?.map(x => moment(x[0]).format('h:mm a'));
    
    for (let i=0; i<=hours.length; i++) {
      if((i+1) % 3 == 0){
            hours[i] = "";
      }
    }

    Then in chart.js:

    labels: hours,

    reply
    0
  • Cancelreply