var line = function (id, attr, data) {
var el = document.getElementById(id) || document.body
var canvas = document.createElement('canvas')
el.appendChild(canvas)
canvas.width = (attr && attr.hasOwnProperty('width')) ? attr.width || 250 : 250
canvas.height = (attr && attr.hasOwnProperty('width')) ? attr.height || 250 : 250
canvas.innerText = 'no support canvas'
var ctx = canvas.getContext('2d')
if (!ctx)
return
var vals = [100, 150, 89, 200]
var yvals = ['A', 'B', 'C', 'D']
if (data && data.constructor === Object) {
yvals = Object.keys(data)
vals = yvals.map(function(v) { return Number(data[v]) })
}
var padding = 30
var origin = {x: padding, y: canvas.height - padding},
width = canvas.width - padding,
height = canvas.height - padding
ctx.moveTo(origin.x, origin.y)
ctx.lineTo(width, origin.y)
var startPadding = 20
var space = (width - startPadding) / yvals.length
for (var i = 0; i < yvals.length; i++) {
ctx.moveTo(startPadding + padding + i * space, height)
ctx.lineTo(startPadding + padding + i * space, height - 5)
ctx.fillText(yvals[i], startPadding + padding + i * space, height + 20)
}
ctx.moveTo(origin.x, origin.y)
ctx.lineTo(origin.x, padding)
var max = Math.max.apply({}, vals)
var min = 0
var count = 5
var space = (height) / count
var step = parseInt((max-min)/(count-1))
for (var i = 0; i < count; i++) {
if (i !== 0) {
ctx.moveTo(padding, height - i * space)
ctx.lineTo(padding + 5, height - i * space)
ctx.fillText(min + step * i, padding - 20, height - i * space)
}
}
var scale = space/step
var space = (width - startPadding) / vals.length
for (var i = 0; i < vals.length; i++) {
if (i !== 0) {
ctx.lineTo(space * i + padding + startPadding, height - vals[i] * scale)
ctx.fillText(vals[i], padding + startPadding + space * i, height - vals[i] * scale)
} else {
ctx.moveTo(padding + startPadding, height - vals[i] * scale )
ctx.fillText(vals[i], 30, height - vals[i] * scale)
}
}
ctx.stroke()
ctx.closePath()
}