简体   繁体   中英

How to keep side bars in Chartjs bar graph from clipping?

I created a chartjs bar graph to represent a set of measurements over a 60 minute interval. I have managed to get the graph up and running but the end bars of it keep getting clipped. The code is here https://jsfiddle.net/bagelboy/yxs9tr5c/

//
var hourBarctx = document.getElementById("hourBarChart");
var hourBarChart = new Chart(hourBarctx, {
  // type: 'line',
  type: 'bar',
  data: {
    labels: [
      // min: moment.unix(new Date(2018, 1, 0, 0, 0, 0).getTime() / 1000),
      // max: moment.unix(new Date(2018, 1, 0, 0, 30, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 0, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 1, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 2, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 3, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 4, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 5, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 59, 0).getTime() / 1000),
      moment.unix(new Date(2018, 1, 0, 0, 60, 0).getTime() / 1000),


    ],
    datasets: [{
        label: 'All Units',
        data: [42, 43, 70, 89, 47, 42, 43, 70],
        // backgroundColor: 'rgba(0, 0, 200, 0.1)',
        backgroundColor: 'rgba(0, 99, 132, 0.6)',
        borderColor: 'rgba(0, 99, 132, 1)',
        // borderWidth: 0
        borderWidth: 1,

      },
      {
        label: 'TV',
        data: [],
        backgroundColor: 'rgba(76, 175, 80, 0.1)',
        borderWidth: 1,
      },
      {
        label: 'Light',
        data: [],
        backgroundColor: 'rgba(0, 175, 80, 0.1)',
        borderWidth: 1,
      },
      {
        label: 'AC',
        data: [],
        backgroundColor: 'rgba(76, 0, 80, 0.1)',
        borderWidth: 1,
      }
    ]
  },
  options: {
    tooltips: {
      enabled: true,
      callbacks: {
        label: function(tooltipItem, data) {
          var label = data.labels[tooltipItem.index];
          var val = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
          return label;
        }
      }

    },
    title: {
      display: true,
      text: 'Hour'
    },
    elements: {

    },
    scales: {
      xAxes: [{
        gridLines: {
                offsetGridLines: true
            },
        categoryPercentage: 1.0,
        barPercentage: 1.0,
        type: "time",
        stacked: true,

        time: {
          unit: 'minute',
          unitStepSize: 1,
          displayFormats: {
            // day: 'MM/DD/YYYY hh:mm a X Z ',
            minute: 'mm',
          },
          //new Date(year, month, day, hours, minutes, seconds, milliseconds)
          min: moment.unix(new Date(2018, 1, 0, 0, 0, 0).getTime() / 1000),
          max: moment.unix(new Date(2018, 1, 0, 0, 60, 0).getTime() / 1000),
        }
      }, ],
      yAxes: [{
        // barPercentage: 1,
        // categoryPercentage: 1,
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});

Anyone know how to keep the ends from being clipped like this? 在此处输入图片说明

I think it's the way you are trying to do the axis, its looking to show the values between 00-01, 01-02 etc, in the last data point your axis doesn't go to 1am, and the first data point is similar, i was able to fix it by changing:

min: moment.unix(new Date(2018, 1, 0, 0, 0, 0).getTime() / 1000),
max: moment.unix(new Date(2018, 1, 0, 0, 60, 0).getTime() / 1000),

to:

min: moment.unix(new Date(2018, 1, 0, 0, -1, 0).getTime() / 1000),
max: moment.unix(new Date(2018, 1, 0, 0, 61, 0).getTime() / 1000),

Might not be exactly what you want but in the fiddle (after adding moment.js as a dependency) it doesn't cut off your bars.

I haven't worked with time like this, but maybe the data supplied needs to be different to get it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM