Displaying line chart for multiple months using chart.js

Viewed 3110

I have to display a line chart with two different datasets in one single chart. After execution of the query I got the plan_plan and actual_plan of the following form.

plan_plan = 0: {label: "Feb-20", plan_hrs: "20"} 
            1: {label: "Oct-20", plan_hrs: "94"} 
actual_plan = 0: {label: "Mar-20", actual_hrs: "1"}

javacript code:

   function show_Graph()
        {
            {
                var plandata = <?php echo json_encode($plan_plan); ?>;
                var actualdata = <?php echo json_encode($actual_plan); ?>;

                 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.plan_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.actual_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 want to get the month in the format (Jan-20) in x-axis and then plan_hrs and actual_hrs data in y-axis which can be shown as a line chart. And also should show a single point entry even when there are no data points to be connected. I have used the script tags:

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

But it is not showing graph when I am including the following script for bootstrap:

<script src="build/js/custom.min.js">
1 Answers

I can help for parsing and displaying the dates on the xAxis but unfortunately I don't know Bootstrap.

Define your xAxis as follows:

xAxes: [{
  type: 'time',
  time: {
    parser: 'MMM-YY',
    unit: 'month',
    displayFormats: {
      month: 'MMM-YY'
    }
  }
}]

Chart.js internally uses Moment.js, hence you can use the following date/time formats for parsing and displaying the date. In your case, this is 'MMM-YYD'.

const plan_plan = [{label: "Feb-20", plan_hrs: "20"}, {label: "Oct-20", plan_hrs: "94"}]; 
const actual_plan = [{label: "Mar-20", actual_hrs: "1"}];

new Chart("scurve_chart", {
  type: 'line',
  data: {
    datasets: [{
        label: "Planned Hours",
        fill: false,
        borderColor: "rgba(255, 0, 0, 1)",
        data: plan_plan.map(o => ({ x: o.label, y: Number(o.plan_hrs)}))
      },
      {
        label: "Actual Hours",
        fill: false,
        borderColor: "rgba(0, 255, 0, 1)",
        data: actual_plan.map(o => ({x: o.label, y: Number(o.actual_hrs)}))
      }
    ]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }],
      xAxes: [{
        type: 'time',
        time: {
          parser: 'MMM-YY',
          unit: 'month',
          displayFormats: {
             month: 'MMM-YY'
          }
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
<canvas id="scurve_chart" height="90"></canvas>

Related