Chartjs-Chart.js 2 line charts with separate dataset labels

1๐Ÿ‘

Ok, that could help you, Now I have made different line chart with four different data with different counts.
Here i have made three label array and attached to tick callback function randomly.
You can choose whichever apropriate, but make sure bigger atrray lenth should come
in to data: { labels: label1}.
Check link enter link description here

var ctx = document.getElementById("myChart");

var label1 = ["aa", "bb", "cc", "dd", "ee", "ff", "gg"];
var label2 = ["1900", "1950", "1999", "2050"];
var label3 = ["AA", "BB", "CC", "DD", "EE"];

var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: label1,
    datasets: [{
      label: "Europe",
      type: "line",
      borderColor: "#8e5ea2",
      data: [408, 547, 675, 934],
      fill: false
    }, {
      label: "Africa",
      type: "line",
      borderColor: "magenta",
      data: [133, 221, 783, 2478, 1900, 1200],
      fill: false
    }, {
      label: "Europe",
      type: "line",
      borderColor: "green",
      data: [408, 547, 675, 734, 200],
      fill: false
    }, {
      label: "Africa",
      type: "line",
      borderColor: "red",
      //backgroundColorHover: "#3e95cd",
      data: [133, 221, 783, 2478],
      fill: false
    }]
  },
  options: {
    title: {
      display: true,
      text: 'Population growth (millions): Europe & Africa'
    },
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
        ticks: {
          // Include a dollar sign in the ticks
          callback: function(value, index, values) {
            if (label3[index] === undefined) {
              label3[index] = "no data ";
            }
            if (label2[index] === undefined) {
              label2[index] = "no data ";
            }
            return "l3 " + label3[index] + "l2 " + label2[index] + ' $' + value;
          }
        }
      }]
    }
  }
});

0๐Ÿ‘

//use following code that might helpful
 var ctx = $("#myChart");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: [1500,1600,1700,1750,1800,1850,1900,1950,1999,2050],
    datasets: [{ 
        data: [86,114,106,106,107,111,133,221,783,2478],
        label: "Africa",
        borderColor: "#3e95cd",
        fill: false
      }, { 
        data: [282,350,411,502,635,809,947,1402,3700,5267],
        label: "Asia",
        borderColor: "#8e5ea2",
        fill: false
      }, { 
        data: [168,170,178,190,203,276,408,547,675,734],
        label: "Europe",
        borderColor: "#3cba9f",
        fill: false
      }, { 
        data: [40,20,10,16,24,38,74,167,508,784],
        label: "Latin America",
        borderColor: "#e8c3b9",
        fill: false
      }, { 
        data: [6,3,2,2,7,26,82,172,312,433],
        label: "North America",
        borderColor: "#c45850",
        fill: false
      }
    ]
  },
  options: {
    title: {
      display: true,
      text: 'World population per region (in millions)'
    }
  }
});

0๐Ÿ‘

I think this one as you mention have different label and differnt data and count

// here is link https://codepen.io/hiren65/pen/ydMZaJ 
var ctx = $("#myChart");
  var myChart = new Chart(ctx, {
    type: 'line',
    data: {      
      datasets: [{
        data: [1, 2, 6, 11, 5], 
        type: "line",
        borderColor: "red",
        labels: [
          'green',
          'yellow',
          'red',
          'purple',
          'blue',
        ]
      }, {
        data: [6, 7, 8],
        type: "line",
        borderColor: "#123ea2",
        labels: [
          'a',
          'b',
          'c'
        ],
      },  {
        data: [6, 3, 8,10,11,4],
        type: "line",
        borderColor: "#6cc",
        labels: [
          'a1',
          'b1',
          'c1',
          'e1'
        ],
      },]
    },
    options: {
      responsive: true,
      legend: {
        display: true,
      },
    }
  });

Leave a comment