Skip to content

Commit

Permalink
Added QR Code support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanrolds committed Jan 3, 2012
1 parent add3a3f commit 34d93d0
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 15 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@
chart.setAutoScaling();
chart.setTransparentBackground();

var imageUrl = chart.getUrl(true); // First param controls http vs. https
var imageUrl = chart.getUrl(true); // First param controls http vs. https

## QR Code

![QR Code](https://chart.googleapis.com/chart?cht=qr&chs=100x100&chl=https://github.com/ryanrolds/quiche&chld=L|0)

var quiche = require('quiche');

var qr = quiche('qr');
qr.setLabel('https://github.com/ryanrolds/quiche');
qr.setWidth(100);
qr.setHeight(100);

var url = qr.getUrl(true); // First param controls http vs. https

# Documentation

Expand All @@ -74,6 +87,7 @@
* line
* pie
* bar
* qr

### All charts

Expand Down Expand Up @@ -118,6 +132,17 @@
* line.setSparklines();
* line.setXY();

### QR Code

* qr.setLabel(data [string]);
* qr.setEncoding(encoding [UTF-8 | Shift_JS | ISO-8859-1]);
* qr.setErrorCorrectionLevel(level [string]) // http://code.google.com/apis/chart/infographics/docs/qr_codes.html#overview
* qr.setMargin(margin [number]); // Margin around graphic
* qr.setWidth(width [number]);
* qr.setHeight(height [number]);
* qr.getUrl(https [boolean]); // true = https, false = http


# Features

* Pie charts
Expand Down
75 changes: 75 additions & 0 deletions lib/qr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

// http://code.google.com/apis/chart/infographics/docs/overview.html

var Chart = require('./chart');

module.exports = (function() {
var QR = function() {
this.initialize();
};

QR.prototype.initialize = function() {
Chart.prototype.initialize.call(this);

this.type = 'qrcode';
this.encoding = 'UTF-8';
this.errorCorrection = 'L';
this.margins = 0;
this.width = 200;
this.height = 200;
};

QR.prototype.setLabel = function(data) {
this.labels = [data];
};

QR.prototype.getLabel = function() {
return this.labels;
};

QR.prototype.setEncoding = function(encoding) {
if(['UTF-8', 'Shift_JIS', 'ISO-8859-1'].indexOf(encoding) === -1) {
throw new Error('Invalid encoding');
}

this.encoding = encoding;
};

QR.prototype.getEncoding = function() {
this.encoding;
};

QR.prototype.setErrorCorrectionLevel = function(level) {
if([].indexOf(level) === -1) {
throw new Error('Invalid error correction level');
}

this.errorCorrection = level;
};

QR.prototype.getErrorCorrectionLevel = function() {
return this.errorCorrection;
};

QR.prototype.setMargin = function(rows) {
this.margins = rows;
};

QR.prototype.getMargin = function() {
return this.margins;
};

QR.prototype.setWidth = Chart.prototype.setWidth;
QR.prototype.setHeight = Chart.prototype.setHeight;
QR.prototype.getType = Chart.prototype.getType;

QR.prototype.getData = function() {
return [];
}

QR.prototype.getUrl = function() {
return Chart.prototype.getUrl.call(this, arguments[0]);
};

return QR;
}());
70 changes: 63 additions & 7 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module.exports = {
params = _merge(params, processLegend(chart));
params = _merge(params, processSize(chart));
params = _merge(params, processBackground(chart));
params = _merge(params, processEncoding(chart));
params = _merge(params, processLabel(chart));
params = _merge(params, processLabelData(chart));

var options = [];
for(var param in params) {
Expand Down Expand Up @@ -58,6 +61,8 @@ function processType(chart) {
} else {
cht += 'c';
}
} else if(type === 'qrcode') {
cht = 'qr';
}

return {
Expand Down Expand Up @@ -137,11 +142,13 @@ function processData(chart) {
}

function processBarWidthSpacing(chart) {
var params = {};

params.chbh = ['a', chart.barSpacing, chart.barWidth].join(',');
if(chart.getType() === 'bar') {
return {
'chbh': ['a', chart.barSpacing, chart.barWidth].join(',')
};
}

return params;
return {};
}

function processAxisLabels(chart) {
Expand Down Expand Up @@ -170,13 +177,38 @@ function processAxisLabels(chart) {

function processLegend(chart) {
var params = {};

params.chdlp = [chart.legend.position, chart.legend.order].join('|');
params.chdls = [chart.legend.color, chart.legend.size].join(',');
var legend = chart.legend;
if(legend.position || legend.order) {

params.chdlp = [legend.position || 's', legend.order || 's'].join('|');
}

if(legend.color || legend.size) {
if(legend.size && !legend.color) {
throw new Error('Legend color must also be set');
}

params.chdls = [legend.color, legend.size].join(',');
}

return params;
}

function processLabelData(chart) {
if(chart.getType() === 'qrcode') {
var ecl = chart.getErrorCorrectionLevel();
var margin = chart.getMargin();
if(ecl !== 'L' || margin !== 4) {
console.log(ecl, margin, [ecl, margin].join('|'));
return {
'chld': [ecl, margin].join('|')
};
}
}

return {};
}

function processLineStyle(chart) {
if(['line', 'radar'].indexOf(chart.getType()) === -1) {
return {};
Expand Down Expand Up @@ -217,6 +249,30 @@ function processLineStyle(chart) {
};
}

function processLabel(chart) {
var type = chart.getType();
if(type === 'qrcode') {
return {
'chl': chart.getLabel()
};
}

return {};
}

function processEncoding(chart) {
if(chart.getType() === 'qrcode') {
var encoding = chart.getEncoding();
if(encoding && encoding !== 'L') {
return {
'choe': encoding
};
}
}

return {};
}

function processBackground(chart) {
if(!chart.background) {
return {};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "quiche",
"version": "0.0.5",
"version": "0.0.6",
"author": "Ryan Olds <ryanrolds@gmail.com>",
"description": "Google Chart Tools: Image Chart wrapper for Node.js",
"description": "Google Chart Tools: Image Chart & Infographics wrapper for Node.js",
"keywords": [
"google image charts",
"charts",
"infographics",
"charting",
"google charts",
"url"
Expand Down
2 changes: 1 addition & 1 deletion quiche.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var lookup = {};
var charts = ['bar', 'pie', 'line'];
var charts = ['bar', 'pie', 'line', 'qr'];
charts.forEach(function(item) {
lookup[item.toLowerCase()] = require('./lib/' + item);
});
Expand Down
5 changes: 1 addition & 4 deletions test/pie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ describe('Pie Chart', function() {
it('should be a pie chart', function() {
assert.ok(url.indexOf('cht=p') > 0);
});
});



});
22 changes: 22 additions & 0 deletions test/qr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var assert = require('assert');

var quiche = require('../quiche');

describe('QR Infograph', function() {
var qr = quiche('qr');
qr.setLabel('https://github.com/ryanrolds/quiche');
qr.setWidth(100);
qr.setHeight(100);

var url = qr.getUrl();

console.log(url);

it('should return a url', function() {
assert.equal(typeof url, 'string');
});

it('should be a qr', function() {
assert.ok(url.indexOf('cht=qr') > 0);
});
});

0 comments on commit 34d93d0

Please sign in to comment.