4

I am a newbie when it comes to using libraries for drawing charts in JavaScript/ I just started experimenting with Chartjs and I have been unable to how to use getJson or anything else to load my json object and replace the labels and data. I have used HighCharts before and its quite simple compared to this. Also, how would I go about putting this into a directive in Angular and displaying it.

https://jsfiddle.net/0u9Lpttx/1/

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div style="width: 100%; height: 100%;">
    <canvas id="myChart" style="width: 100%; height: auto;"></canvas>
</div>
<div id="js-legend" class="chart-legend"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.min.js"></script>
<script src="js/chartJsControl.js"></script>
</body>
</html>

data.json

[
  {
    "timestamp": "Monday",
    "original_tweet": "756",
    "retweets": "345",
    "shared": "34",
    "quoted": "14"
  },
  {
    "timestamp": "Tuesday",
    "original_tweet": "756",
    "retweets": "345",
    "shared": "34",
    "quoted": "14"
  },
  {
    "timestamp": "Wednesday",
    "original_tweet": "756",
    "retweets": "345",
    "shared": "34",
    "quoted": "14"
  }
]

chartJsControl.js

var test = [];
$.getJSON("data.json", function (json) {
    test.push(json[i].timestamp);

});
var data = {
    labels: test,
    datasets: [
        {
            label: "My First dataset",
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            label: "My Second dataset",
            fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};


var ctx = document.getElementById("myChart").getContext("2d");
ctx.canvas.width = 1000;
ctx.canvas.height = 800;

var myChart = new Chart(ctx).Bar(data);
3
  • the creation of the chart must be inside the getJSON callback function. Only then you can use the returning json Commented Oct 6, 2015 at 18:49
  • 3
    please give example to show Commented Oct 6, 2015 at 18:51
  • @user2402107 you could think of angular version of chart.js take a look at this answer stackoverflow.com/a/28278459/2435473 Commented Oct 6, 2015 at 18:57

1 Answer 1

6

If you want to use the returned JSON from data.json you need to do this in the callback function like this:

$.getJSON("data.json", function (json) {
  // will generate array with ['Monday', 'Tuesday', 'Wednesday']
  var labels = json.map(function(item) {
    return item.timestamp;
  });

  var data = {
    labels: labels,
    datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.5)",
      strokeColor: "rgba(220,220,220,0.8)",
      highlightFill: "rgba(220,220,220,0.75)",
      highlightStroke: "rgba(220,220,220,1)",
      data: [65, 59, 80, 81, 56, 55, 40]
    },
    {
      label: "My Second dataset",
      fillColor: "rgba(151,187,205,0.5)",
      strokeColor: "rgba(151,187,205,0.8)",
      highlightFill: "rgba(151,187,205,0.75)",
      highlightStroke: "rgba(151,187,205,1)",
      data: [28, 48, 40, 19, 86, 27, 90]
    }
    ]
  };

  var ctx = document.getElementById("myChart").getContext("2d");
  ctx.canvas.width = 1000;
  ctx.canvas.height = 800;

  var myChart = new Chart(ctx).Bar(data);
});

If you are using it with Angular I would recommend using the angular chart.js version, see: http://jtblin.github.io/angular-chart.js/

Then you already have an angular directive, which you can use!

Sign up to request clarification or add additional context in comments.

5 Comments

A while ago, I wrote an blog article where I compared different angular chart libraries: lingohub.com/blog/2014/06/…
Actually here is a tougher question. Its working perfectly now but I want to break each json object per day so that Monday it is a grouped chart but right now its just giving me the same data set back
I don't understand ? Maybe you just need to iterate over your json and fill the datasets array.
Like I want each json for each day to be its own grouped bar chart on the canvas. Right now however its grouping the two data sets together.
I don't know how you can do grouped bar charts with Chart.js, sorry!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.