javascriptphpchart.jsdayofmonth

Line chart using Chart.js


I have two tables, which I got after the execution of mysql query (table1 is pl_pl and table2 is act_act) in the form:

table1:

label   act_hrs
Jan-19  7
Feb-20  8
Mar-20  9

table2:
label   pl_hrs
Mar-20  45
Apr-20  53

I have to label all the points in act_hrs and pl_hrs using a line chart with a common x axis of label . I have tried following javascript code for this:

javascript code:

function show_scurve_Graph()
        {
            {
                var plandata = <?php echo json_encode($pl_pl); ?>;
                var actualdata = <?php echo json_encode($act_act); ?>;

                var labels = [];
                  for (var i in plandata) {
                        labels.push(plandata[i].label);
                  }
                  for (var j in actualdata) {
                        labels.push(actualdata[j].label);
                  }

                new Chart("scurve_chart", {
  type: 'line',
  data: {
    labels: Array.from(labels),
    datasets: [{
        label: "Planned Hours",
        fill: false,
        borderColor: "rgba(255, 0, 0, 1)",
        pointHoverBackgroundColor: "rgba(255, 0, 0, 1)",
        data: plandata.map(o => ({ x: Number(o.label), y: Number(o.pl_hrs)}))
      },
      {
        label: "Actual Hours",
        fill: false,
        backgroundColor: "rgba(0, 255, 0, 0.75)",
        borderColor: "rgba(0, 255, 0, 1)",
        pointHoverBackgroundColor: "rgba(0, 255, 0, 1)",
        pointHoverBorderColor: "rgba(0, 255, 0, 1)",
        data: actualdata.map(o => ({x: Number(o.label), y: Number(o.act_hrs)}))
      }
    ]
  },
  options: {    
    tooltips: {
      callbacks: {
        title: (tooltipItem, data) => "Month " + data.datasets[tooltipItem[0].datasetIndex].data[tooltipItem[0].index].x
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        },
       scaleLabel: {
        display: true,
        labelString: 'Hours'
      }
      }],
      xAxes: [{
        ticks: {          
          min: 0,
          max: 53,
          stepSize: 1
        },
        scaleLabel: {
        display: true,
        labelString: 'Month'
      }
      }]
    }
  }
});
 }}

I have used following library for this:

<script src="vendors/jquery/dist/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 

I got something like this as output : enter image description here


Solution

  • The following lines are the culprit:

    data: plandata.map(o => ({ x: Number(o.label), y: Number(o.pl_hrs)}))
    

    and

    data: actualdata.map(o => ({x: Number(o.label), y: Number(o.act_hrs)}))
    

    The map callback should only return the hour figures, like this:

    // Planned
    data: plandata.map(o => Number(o.pl_hrs))
    
    // Actual
    data: actualdata.map(o => Number(o.act_hrs))
    

    The data property only needs an array of figures you want to plot on the graph. Here is a jsfiddle

    Note: you should normalize your data on all data sources based on the labels you use on labels config of the chart. What I mean is, the order of the figures in your data array should correspond to the label order.