-1

I have a chartjs line chart requirement where the client is asking to show labels on x axis only if it has a data point. please find his mockup below.

enter image description here

the x-axis is time and here is what I am getting.

enter image description here

how can I achieve this?

here is my config.

    options={{
                scales: {
                    xAxes: [
                        {
                            distribution: 'linear',
                            type: "time",
                            time: {
                                min: range_min.toDateString(),
                                max: range_max.toDateString(),
                                unit: "day",
                                stepSize: "1",
                            },
                            id: 'xAxis',
                            ticks: {
                                autoSkip: true,
                                callback: function (value, index, values) {
                                    return formatDate(new Date(value))
                                },
                            }
                        },
                    ],
                },
                pan: {
                    enabled: true,
                    mode: "x",
                    speed: 1,
                    threshold: 1,
                },
                zoom: {
                    enabled: true,
                    drag: true,
                    sensitivity: 0.5,
                    mode: "x",
                },
                annotation: {
                    annotations: [{
                        type: 'line',
                        mode: 'vertical',
                        scaleID: 'xAxis',
                        value: 1582229218219,
                        endValue: 1582229218219,
                        borderColor: 'rgb(75, 0, 0)',
                        borderWidth: 4
                    }]

                },
                onClick: (event, item) => {
                    console.log(item)
                }

            }}

1 Answers1

0

yes this is possible, you can achieve this by using the tick callback like so:

const data = [{
  x: 'Red',
  y: 10
}, {
  x: 'Yellow',
  y: 5
}, {
  x: 'Orange',
  y: 3
}]

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data,
      borderWidth: 1,
      backgroundColor: 'red',
      borderColor: 'red',
      fill: false
    }]
  },
  options: {
    scales: {
      xAxes: [{
        ticks: {
          callback: (val, y, z, t) => (
            data.map(el => el.x).indexOf(val) >= 0 ? val : null
          )
        }
      }]
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js" integrity="sha512-hZf9Qhp3rlDJBvAKvmiG+goaaKRZA6LKUO35oK6EsM0/kjPK32Yw7URqrq3Q+Nvbbt8Usss+IekL7CRn83dYmw==" crossorigin="anonymous"></script>
</body>
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69