Drupal and Chartjs

Submitted by admin on Sun, 03/29/2015 - 16:39

Here is the snippet I used to describe how to load a Chartjs-based chart within a Drupal installation

(function ($) {
  Drupal.behaviors.mymodule = {
    attach: function (context, settings) {
      $(document, context).ready(function () {
        var url = "<a href="http://localhost/test7/myweb/get_data">http://localhost/test7/myweb/get_data</a>";
        $.getJSON( url, function(data) {
          var ctx = $("#myChart").get(0).getContext("2d");
          var myLineChart = new Chart(ctx).Bar(data, {scaleFontSize:13,scaleFontColor:"#ffa45e"});

http://localhost/test7/myweb/get_data is the URL where Json data comes.
"#myChart" is the ID of canvas element where chartjs will display the bar chart.

<canvas id="myChart" width="800" height="800" style="width: 400px; height: 400px;"></canvas>