Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                )
<div class="container">
  <div class="row"><div class="col-md-6">
    <h4 class="mt-3">
      Scrollable Columns in Column-Chart
    </h4>
    <p>Using Chart.js</p>
    <p>To show lots of columns (data) in small area</p>
    <div class="chart-container">
      <canvas id="myChart" class="chart"></canvas>
    </div>
  </p></div>
  
  <div class="nav-buttons">
    <button id="leftBtn" class="btn btn-outline-primary"><<</button>
    <button id="rightBtn" class="btn btn-outline-primary">>></button>
  </div>

  
</div>
    


              
            
!

CSS

              
                .chart-container {
  width: 100%;
  overflow-x: auto;
  height: 300px;
}


              
            
!

JS

              
                const ctx = document.getElementById('myChart').getContext('2d');
const labels = [
  'Jan 23', 'Feb 23', 'Mar 23', 'Apr 23', 'May 23', 'Jun 23', 'Jul 23', 'Aug 23', 'Sep 23', 'Oct 23', 'Nov 23', 'Dec 23',
  'Jan 24', 'Feb 24', 'Mar 24', 'Apr 24', 'May 24', 'Jun 24', 'Jul 24', 'Aug 24', 'Sep 24', 'Oct 24', 'Nov 24', 'Dec 24'
];
const data = [
  12, 19, 3, 5, 12, 13, 10, 15, 8, 12, 14, 6, 12, 19, 3, 5, 12, 3, 10, 15, 18, 12, 14, 16
];
let startIndex = 0;
const sliceSize = 6;

const updateChart = () => {
  const slicedLabels = labels.slice(startIndex, startIndex + sliceSize);
  const slicedData = data.slice(startIndex, startIndex + sliceSize);
  myChart.data.labels = slicedLabels;
  myChart.data.datasets[0].data = slicedData;
  myChart.update();
};

const myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: labels.slice(startIndex, startIndex + sliceSize),
    datasets: [{
      label: 'Sales',
      data: data.slice(startIndex, startIndex + sliceSize),
      backgroundColor: 'rgba(75, 212, 242, 0.4)',
      borderColor: 'rgba(75, 192, 192, 1)',
      /*
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)'
      ],
      borderColor: [
        'rgba(255, 99, 132, 1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)'
      ],
      */
      borderWidth: 1,
      hoverBackgroundColor: 'rgba(255, 159, 64, 0.2)',
      hoverBorderColor: 'rgba(255, 159, 64, 1)'
    }]
  },
  options: {
    responsive: true,
    animation: false, 
    maintainAspectRatio: false,
    scales: {
      x: {
        beginAtZero: true,
        grid: {
          color: 'rgba(100, 100, 100, 0.4)'
        },
        ticks: {
          color: 'rgba(100, 100, 100, 1)'
        }
      },
      y: {
        beginAtZero: true,
        max: 20,
        grid: {
          color: 'rgba(200, 200, 200, 0.4)'
        },
        ticks: {
          color: 'rgba(100, 100, 100, 1)'
        }
      }
    }
  },
  plugins: {
    legend: {
      labels: {
        color: 'rgba(100, 100, 100, 1)'
      }
    },
    tooltip: {
      backgroundColor: 'rgba(60, 60, 60, 0.3)',
      titleColor: 'rgba(255, 255, 255, 1)',
      bodyColor: 'rgba(255, 255, 255, 1)',
      borderColor: 'rgba(255, 255, 255, 1)',
      borderWidth: 1
    }
  }
});

document.getElementById('leftBtn').addEventListener('click', () => {
  if (startIndex > 0) {
    startIndex--;
    updateChart();
  }
});

document.getElementById('rightBtn').addEventListener('click', () => {
  if (startIndex < labels.length - sliceSize) {
    startIndex++;
    updateChart();
}
});





              
            
!
999px

Console