public
Last active

convert table.chart to chart image by google (in prototype)

  • Download Gist
table2gchart.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
window.onload = function(){
 
function Chart(table_elem, size, type) {
this.dom = table_elem;
this.size = size;
this.type = type
this.calc();
}
 
Chart.prototype.rangechars = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
 
Chart.prototype.colors = ["FF0000", "00FF00", "0000FF", "FFFF00", "FF00FF", "00FFFF",
"800000", "008000", "000080", "808000", "800080", "008080"];
 
Chart.prototype.trs = function() {
return this.dom.getElementsByTagName('tr');
}
 
Chart.prototype.head = function() {
return this.rows(0);
}
 
Chart.prototype.rows = function(index) {
var tr = this.trs()[index];
var tds = tr.children;
var arr = [];
for (var i = 0, len = tds.length; i < len; i++) {
arr.push(tds[i]);
}
return arr;
}
 
Chart.prototype.row_length = function() {
return this.trs().length;
}
 
Chart.prototype.col_length = function() {
return this.head().length;
}
 
Chart.prototype.lines_length = function() {
return this.col_length() - 1;
}
 
 
Chart.prototype.calc = function() {
if (this.data && this.data[0]) return this.data;
this.data = [];
for (var i = 1, len = this.row_length(); i < len; i++) {
var r = this.rows(i);
for (var j = 0, len2 = r.length; j < len2; j++) {
if (!this.data[j]) this.data[j] = [];
this.data[j].push(r[j].innerHTML.trim());
}
}
return this.data;
}
 
Chart.prototype.max = function(line) {
// TODO: cache
return Math.max.apply(null, this.data[line+1]);
}
 
Chart.prototype.min = function(line) {
// TODO: cache
return Math.min.apply(null, this.data[line+1]);
}
 
Chart.prototype.yrange = function(line) {
var margin = 0.1;
var min = this.min(line);
var max = this.max(line);
var sub = max - min;
return [line+1, min - sub*margin, max + sub*margin];
}
 
Chart.prototype.encode_unit = function(line) {
var maxes = this.yrange(line);
return (maxes[2] - maxes[1]) / 62;
}
 
Chart.prototype.encode_value = function(value, unit, min) {
return Math.round((value - min)/unit);
}
 
Chart.prototype.encode = function(line) {
var unit = this.encode_unit(line);
var min = this.yrange(line)[1];
var d = this.data[line+1];
var result = this.data[line+1].map(function(x){return this.rangechars[this.encode_value(x, unit, min)]}, this);
return result.join("");
}
 
Chart.prototype.chtt = function() {
return encodeURIComponent(this.head()[0].innerHTML);
}
 
Chart.prototype.chdl = function() {
return this.head().slice(1).map(function(x){return x.innerHTML}).join("|")
}
 
Chart.prototype.chco = function() {
var num = this.lines_length();
return this.colors.slice(0, num).join(",")
}
 
Chart.prototype.chxs = function() {
var num = this.lines_length();
var tmp = [];
for (var i = 0; i < num; i++) {
tmp.push([i+1, this.colors[i]].join(","))
}
return tmp.join("|");
}
 
Chart.prototype.chxr = function() {
var tmp = [];
for (var i = 0, l = this.lines_length(); i < l; i++) {
tmp.push(this.yrange(i).join(","));
}
return tmp.join("|");
}
 
Chart.prototype.chxl = function() {
return "0:|" + this.data[0].join("|");
}
 
Chart.prototype.chxt = function() {
var num = this.lines_length();
var tmp = [];
for (var i = 0; i < num; i++) tmp.push("y");
return "x," + tmp.join(",")
}
 
Chart.prototype.chd = function() {
var tmp = [];
for (var i = 0, l = this.lines_length(); i < l; i++) {
tmp.push(this.encode(i));
}
return "s:" + tmp.join(",");
}
 
Chart.prototype.query = function() {
return [
"chs=" +(this.size||"960x240"),
"cht=" +(this.type||"lc"),
"chtt="+this.chtt(),
"chco="+this.chco(),
"chdl="+this.chdl(),
"chxr="+this.chxr(),
"chxt="+this.chxt(),
"chxs="+this.chxs(),
"chxl="+this.chxl(),
"chd=" +this.chd()
].join("&");
}
 
Chart.prototype.url = function() {
this.calc();
return "http://chart.apis.google.com/chart?" + this.query();
}
 
// exec
var charts = document.getElementsByClassName('chart');
for (var i = 0, len = charts.length; i < len; i++) {
var c = charts[i];
var chart = new Chart(c);
var img = document.createElement('img');
img.src = chart.url();
 
c.parentNode.insertBefore(img, c.nextSibling);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.