Show Label near Line in combined Chart - Javascript Chart.js

Javascript examples for Chart.js:Line Chart

Description

Show Label near Line in combined Chart

Demo Code

ResultView the demo in separate window

<html lang="en">
   <head> 
      <title>Chart.js - Text Annotations</title> 
   </head> 
   <body translate="no"> 
      <div id="canvas-holder" style="width:40%"> 
         <canvas id="myChart"></canvas> 
      </div> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script> 
      <script src="https://rawgit.com/chartjs/chartjs-plugin-annotation/master/chartjs-plugin-annotation.js"></script> 
      <script>
      var chartColors = {/*  w ww.j ava  2s.  com*/
  red: 'rgb(255, 99, 132)',
  orange: 'rgb(255, 159, 64)',
  yellow: 'rgb(255, 205, 86)',
  green: 'rgb(75, 192, 192)',
  blue: 'rgb(54, 162, 235)',
  purple: 'rgb(153, 102, 255)',
  grey: 'rgb(231,233,237)'
};
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['Jan 21st', 'Feb 21st'],
    datasets: [{
      type: 'line',
      label: 'B',
      data: [10, 25],
      fill: false,
      borderWidth: 3,
      borderColor: chartColors.orange,
      lineTension: 0,
    }, {
      type: 'bar',
      label: 'A',
      backgroundColor: chartColors.blue,
      data: [10, 25],
    }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }],
      xAxes: [{
        ticks: {
          min: 'Jan 21st',
          max: 'Apr 21st',
        }
      }],
    },
    annotation: {
      annotations: [{
        type: 'line',
        mode: 'horizontal',
        scaleID: 'y-axis-0',
        value: 18,
        borderColor: 'white',
        borderWidth: 0,
        label: {
          xAdjust: -50,
          fontSize: 16,
          fontColor: 'black',
          backgroundColor: 'white',
          content: "+20%",
          enabled: true
        }
      }],
      drawTime: 'beforeDatasetsDraw'
    }
  }
});
      //# sourceURL=pen.js
    
      </script>  
   </body>
</html>

Related Tutorials