[Chartjs]-Displaying mixed types of legends (bar and lines) with Chartjs

3๐Ÿ‘

โœ…

I think the only way is to customize the legend (see http://www.chartjs.org/docs/latest/configuration/legend.html#html-legends).

An example here below:

var ctx = document.getElementById('myChart');
var config = {
	type: 'bar',
  options: {
    legendCallback: function(chart) {
      var text = [];
      text.push('<ul class="' + chart.id + '-legend">');
      var data = chart.data;
      var datasets = data.datasets;
      if (datasets.length) {
        for (var i = 0; i < datasets.length; ++i) {
        	text.push('<li>');
        	if (datasets[i].type=='line') {
          	text.push('<span class="'+datasets[i].type+'" style="background-color:' + datasets[i].backgroundColor + '"></span>');
          } else {
          	text.push('<span class="'+datasets[i].type+'" style="background-color:' + datasets[i].backgroundColor + '"></span>');
          }
          text.push(datasets[i].label);
          text.push('</li>');
        }
      }
      text.push('</ul>');
      return text.join('');
    },
    legend: {
      display: false,
    },
    scales: {
      xAxes: [{
        type: "category",
        id: "axis-bar",
      }, {
        type: "time",
        id: "axis-time",
        display: false,
      }, ],
    },
  },
  data: {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [{
      label: "my dataset 1",
      type: "line",
      backgroundColor: "#0000FF",
      borderColor: "#0000FF",
      borderWidth: 1,
      fill: false,
      xAxisID: "axis-time",
      data: [{x:1*3600,y:2124},{x:1.5*3600,y:12124},{x:2*3600,y:1636},{x:2.5*3600,y:11636},{x:3*3600,y:1057},{x:3.5*3600,y:11057},{x:4*3600,y:9433},{x:4.5*3600,y:19433},{x:5*3600,y:4512},{x:5.5*3600,y:4512},{x:6*3600,y:3581},{x:6.5*3600,y:3581},{x:7*3600,y:53},{x:7.5*3600,y:53},]
    },{
      label: "my dataset 2",
      type: "bar",
      backgroundColor: "#ff0000",
      borderColor: "#ff0000",
      borderWidth: 1,
      fill: true,
      xAxisID: "axis-bar",
      data: [21242, 16360, 10577, 94337, 45312, 35581, 53]
    }]
  },

};

var myChart = new Chart(ctx, config);
var legend = myChart.generateLegend();
document.getElementById("legend").innerHTML = legend;
body {
  font-family: 'Arial';
}

.container {
  margin: 15px auto;
}

#chart {
  float: left;
  width: 70%;
}

#legend {
  float: right;
}

[class$="-legend"] {
  list-style: none;
  cursor: pointer;
  padding-left: 0;
}

[class$="-legend"] li {
  display: block;
  padding: 0 5px;
}

[class$="-legend"] li.hidden {
  text-decoration: line-through;
}

[class$="-legend"] li span {
  display: inline-block;
  margin-right: 10px;
  width: 20px;
}

[class$="-legend"] li span.bar {
  height: 10px;
}

[class$="-legend"] li span.line {
  height: 1px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<div id="legend"></div>
<canvas id="myChart" width="400" height="200"></canvas>

or a jsfiddle here: https://jsfiddle.net/beaver71/g7oz0noe/


P.S.: if you need also legend click handlers you can add them easily, see for an example this fiddle: https://jsfiddle.net/beaver71/b5hdn9gw/

2๐Ÿ‘

set pointStyle to whatever you want like line,rect etc inside your chart data object

 pointStyle: 'rect'

and add below line in options

  legend: {
           labels: {
                    usePointStyle: true
                   }
           }

Leave a comment