score:6

Accepted answer

score:0

charts.js provides the responsive charts configuration options among which there is the onresize method. it gets passed two arguments: the chart instance and the new size. should you log both to the console you'll see the complete chart instance there that includes all the regular options to control the chart instance scales.

i added this to the options object on the bar chart instance so the x axis disappears on resize to the width of 768 px then appears on the resize back to the desktop screen size. the bar instance was the chart.js react wrapper provided by the react-chartjs-2 library.

<bar
  data={bardata}
  options={{
    // default value
    responsive: true,
    onresize: function(newchart, newsize) {
      console.log('newchart:', newchart);
      console.log('newsize:', newsize);

      if (newsize.width < 768) {
        newchart.options.scales.xaxes[0].display = false;
      } else {
        newchart.options.scales.xaxes[0].display = true;
      }
    }, ...

for a pie instance switch newchart.options.scales.xaxes[0].display for newchart.options.legend.display.

score:1

the simplest solution is:

scalefontsize: 0

see the chart.js document

score:1

you can extend the current barchart and remove the xlabels like this.

function registercustombarchart() {
    chart.types.bar.extend({
        name: "custombarchart",
        initialize: function (data) {
            chart.types.bar.prototype.initialize.apply(this, arguments);
            var xlabels = this.scale.xlabels
            xlabels.foreach(function (label, i) {
                    xlabels[i] = '';
            })
        }
    });
}

var mybarchart = new chart(context).custombarchart(data);

score:2

i got around this by defining labels as a list of empty strings. example:

var data = {
    labels: ["", "", "", "", ""],
    datasets: [{
        label: "test",
        fillcolor: "rgba(220,220,220,0.2)",
        strokecolor: "rgba(220,220,220,1)",
        pointcolor: "rgba(220,220,220,1)",
        pointstrokecolor: "#fff",
        pointhighlightfill: "#fff",
        pointhighlightstroke: "rgba(220,220,220,1)",
        data: [10, 20, 15, 8, 22]
    }]
};

for this you need the label to not be relevant in the tooltip box. i defined mine as follows:

tooltiptemplate: "latency: <%=value%>"

score:2

answer of @kapytanhook is correct for chart.js version 2.x.x

for those interested, following modification of his answer for ...

chart.js v3.x.x

(as v3.x.x is not backward compatible with v2.x.x)

const mylinechart = new chart(ctx, {
  type: 'line',
  data: data,
  options: {
    scales: {
      x: {  // <-- object '{' now, instead of array '[{' before in v2.x.x
        ticks: {
          display: false
        }
      }
    }
  }
})

programmatically:
also, as questions mentiones to show/hide the lables/ticks, i added a button to modify the chart programmatically:

mylinechart.options.scales['x'].ticks.display = true;
mylinechart.update();

following complete code with sample data to run snippet and see result with hidden x-axis ticks.

const labels = ["2021-08-02","2021-08-03","2021-08-04","2021-08-05","2021-08-06"];
const data_1 = [39,41,42,43,43];
const data_2 = [37,38,40,41,39];

const ctx = document.queryselector('canvas').getcontext('2d');

const btnshowhide = document.queryselector('#btnshowhide');

const data = {
  labels: labels,
  datasets: [{
    label: 'data 1',
    bordercolor: 'grey',
    data: data_1
  }, {
    label: 'data 2',
    bordercolor: 'grey',
    data: data_2
  }]
};

const mylinechart = new chart(ctx, {
  type: 'line',
  data: data,
  options: {
    scales: {
      x: { // <-- object '{' now, instead of array '[{' before in v2.x.x
        ticks: {
          display: false
        }
      }
    }
  }
});

btnshowhide.onclick = function() {
  if(btnshowhide.classlist.contains('hidden')) {
    mylinechart.options.scales['x'].ticks.display = true;
    mylinechart.update();
    
    btnshowhide.classlist.remove('hidden');
    btnshowhide.innerhtml = 'hide';
  } else {
    mylinechart.options.scales['x'].ticks.display = false;
    mylinechart.update();
    
    btnshowhide.classlist.add('hidden')
    btnshowhide.innerhtml = 'show';
  }
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- gets you the latest version of chart.js, now at v3.5.0 -->

<button id="btnshowhide" class="hidden">show</button>

<div style="width:320px">
  <canvas></canvas>
</div>

score:8

they added the option, 2.1.4 (and maybe a little earlier) has it

var mylinechart = new chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            xaxes: [{
                ticks: {
                    display: false
                }
            }]
        }
    }
}

Related Query

More Query from same tag