'use strict';

function getLayout(count, rect) {
  var entryHeight = 20;
  var left = rect.left;
  var result = [];
  var totalPerEntry = rect.height / count;
  for (var i = 0; i < count; i += 1) {
    result.push({
      top: (totalPerEntry - entryHeight) / 2 + rect.top + totalPerEntry * i,
      left: left
    });
  }
  return result;
}

function drawLegendEntry(ctx, data, layout) {
  ctx.fillStyle = '#444';
  ctx.font = '13px Verdana';
  ctx.textAlign = 'left';
  ctx.fillText(data.label, layout.left + 25, layout.top + 10);

  ctx.fillStyle = data.strokeColor;
  ctx.fillRect(layout.left, layout.top, 20, 20);
}

function drawLegend(ctx, rect, data) {
  var layout = getLayout(data.length, rect);
  data.forEach(function (d, i) {
    drawLegendEntry(ctx, d, layout[i]);
  });
}

function generateChart(labels, dataset) {
  var Canvas = require('canvas');
  var canvas = new Canvas(800, 500);
  var ctx = canvas.getContext('2d');
  var Chart = require('nchart');
  var data = {
    labels: labels,
    datasets: dataset
  };
  new Chart(ctx).Line(data);
  var legendSize = {
    left: 40,
    top: 12,
    width: 190,
    height: 70
  };
  drawLegend(ctx, legendSize, dataset);
  return canvas;
}

var fs = require('fs');
var ChartRenderer = {
  generate: function () {
  },
  save: function (filename, chart) {
    chart.toBuffer(function (err, buf) {
      if (err) {
        throw err;
      }
      if (!fs.existsSync(filename)) {
        fs.mkdirSync(filename);
      }
      fs.writeFileSync(filename, buf);
    });
  }
};

module.exports = ChartRenderer;

@@ -1206,75 +1206,886 @@
 var 
-fs = require('fs');%0Avar ChartRenderer = %7B%0A  generate: function () %7B
+colors = %5B%7B%0A  fillColor: 'rgba(220,220,220,0.3)',%0A  strokeColor: 'rgba(220,220,220,1)',%0A  pointColor: 'rgba(220,220,220,1)',%0A  pointStrokeColor: '#fff',%0A  pointHighlightFill: '#fff',%0A  pointHighlightStroke: 'rgba(220,220,220,1)',%0A%7D, %7B%0A  fillColor: 'rgba(151,187,205,0.3)',%0A  strokeColor: 'rgba(151,187,205,1)',%0A  pointColor: 'rgba(151,187,205,1)',%0A  pointStrokeColor: '#fff',%0A  pointHighlightFill: '#fff',%0A  pointHighlightStroke: 'rgba(151,187,205,1)',%0A%7D%5D;%0A%0Avar fs = require('fs');%0Avar ChartRenderer = %7B%0A  generate: function (labels, datasets) %7B%0A    var color = 0;%0A    datasets = datasets.map(function (s) %7B%0A      var c = colors%5Bcolor%5D;%0A      var result = %7B%7D;%0A      color += 1;%0A      for (var prop in c) %7B%0A        result%5Bprop%5D = c%5Bprop%5D;%0A      %7D%0A      result.data = s.values;%0A      result.label = s.label;%0A      return result;%0A    %7D);%0A    return generateChart(labels, datasets);
 %0A  %7D

Update the rendering functionality