Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Look into <a href="http://mbostock.github.com/d3/" rel="nofollow">d3.js</a>. There are several examples of how you can take an array in JavaScript and turn it into a graph like that.</p> <p>Here is one (more advance) example: <a href="http://mbostock.github.com/d3/ex/population.html" rel="nofollow">http://mbostock.github.com/d3/ex/population.html</a><br> And here is another example that is closer to what you want: <a href="http://mbostock.github.com/d3/tutorial/bar-2.html" rel="nofollow">http://mbostock.github.com/d3/tutorial/bar-2.html</a></p> <p><strong>EDIT</strong></p> <p>The actual code that Git Hub uses to create the graphs looks something like this:</p> <pre><code>GitHub.ParticipationGraph = (function(){ function b(target){ this.el = target; this.onSuccess = $.proxy(this.onSuccess, this); this.canvas = this.el.getContext("2d"); this.refresh(); } b.prototype.barWidth = 7; b.prototype.barMaxHeight = 20; b.prototype.getUrl = function(){ return $(this.el).data("source"); }; b.prototype.setData = function(data){ this.data = data; if (data == null || data.all == null || data.owner == null) { this.data = null; } this.scale = this.getScale(this.data); }; b.prototype.getScale = function(data){ var mx, i; if (data == null) return; mx = data.all[0]; for(i = 0; i &lt; data.all.length; i++) { if (data.all[i] &gt; mx) { mx = data.all[i]; } } return mx &gt;= this.barMaxHeight ? (this.barMaxHeight-.1)/mx : 1; }; b.prototype.refresh = function(){ $.ajax({ url: this.getUrl(), dataType: "json", success: this.onSuccess }); }; b.prototype.onSuccess = function(data){ this.setData(data); this.draw(); }; b.prototype.draw = function(){ if (this.data == null) return; this.drawCommits(this.data.all, "#cacaca"); this.drawCommits(this.data.owner, "#336699"); }; b.prototype.drawCommits = function(data, color){ var i, width, height, x, y; for (i = 0; i &lt; data.length; i++) { width = this.barWidth; height = data[i] * this.scale; x = i * (this.barWidth + 1); y = this.barMaxHeight - height; this.canvas.fillStyle = color; this.canvas.fillRect(x, y, width, height); } }; return b; })(); </code></pre> <p>Basically, they are calling the <code>data-source</code> tag located on the canvas, which returns some JSON that represents the amount of work/participation/watches (or whatever metric they are calculating) and then they go through each returned value and call <code>this.canvas.fillRect</code> with the predefined width (<code>(Screensize.width/52) - (paddingLeft + paddingRight)</code>) and the height from the JSON returned</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload