Customize border style on Chart.js for bar chart - Javascript Chart.js

Javascript examples for Chart.js:Bar Chart

Description

Customize border style on Chart.js for bar chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> 
      <style id="compiled-css" type="text/css">
.myChartDiv {/*from   ww w.  j  av  a  2s  . co  m*/
   max-width: 600px;
   max-height: 400px;
}
      </style> 
      <script type="text/javascript">
    window.onload=function(){
var ctx = document.getElementById("myChart");
var borderColors = ['red','blue','black']
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Black"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3],
            borderColor:borderColors,
            borderWidth:3,
            borderStyle:'dash'//has no effect
        }]
    }
});
function dashedBorder(chart, dataset, data, dash) {
   chart.config.data.datasets[dataset]._meta[0].data[data].draw = function() {
        chart.chart.ctx.setLineDash(dash);
        Chart.elements.Rectangle.prototype.draw.apply(this, arguments);
        chart.chart.ctx.setLineDash([1,0]);
    }
}
// dashedBprder(chart, datasetIndex, barIndex, dash values)
dashedBorder(myChart, 0, 0, [15,10]);
dashedBorder(myChart, 0, 2, [5,15]);
console.log(myChart);
    }

      </script> 
   </head> 
   <body> 
      <div class="myChartDiv"> 
         <canvas id="myChart" width="600" height="400"></canvas> 
      </div>  
   </body>
</html>

Related Tutorials