Comparing version 1.0.0 to 1.1.0
@@ -10,3 +10,5 @@ window.Dataline = (function() { | ||
var values = canvas.getAttribute('data-values'); | ||
if (values) { | ||
if (values === '') { | ||
data = []; | ||
} else if (values) { | ||
data = values.split(',').map(function(value) { | ||
@@ -13,0 +15,0 @@ return parseFloat(value); |
@@ -1,1 +0,1 @@ | ||
window.Dataline=function(){function draw(id,data){var canvas=document.getElementById(id);if(!canvas)throw new Error("Element with id "+id+" does not exist.");if(!data){var values=canvas.getAttribute("data-values");values&&(data=values.split(",").map(function(value){return parseFloat(value)}))}if(!data)throw new Error("Chart data is not set.");drawInAnimationFrame(canvas,data),window.addEventListener("resize",function(){drawInAnimationFrame(canvas,data)},!1)}function drawInAnimationFrame(canvas,data){window.requestAnimationFrame(function(){canvas.width=Math.floor(canvas.offsetWidth),canvas.height=Math.floor(canvas.offsetHeight);var ctx=canvas.getContext("2d");ctx.clearRect(0,0,canvas.width,canvas.height),ctx.imageSmoothingEnabled=!0,drawData(ctx,30,data)})}function drawData(ctx,padding,values){function yLocation(value){return padding+value*innerHeight}var canvas=ctx.canvas,height=canvas.height,width=canvas.width,points=normalizeInvert(values),innerWidth=width-2*padding,innerHeight=height-2*padding,xDelta=innerWidth/points.length,minmax=arrayMinMax(values),first=points[0];if(ctx.save(),ctx.lineWidth=1,ctx.strokeStyle="#999",ctx.setLineDash([2]),minmax.min<0&&minmax.max>0){var yZero=Math.floor(yLocation(1-minmax.apply(0)))+.5;ctx.beginPath(),ctx.moveTo(padding,yZero),ctx.lineTo(width-padding,yZero),ctx.stroke()}var yMin=padding+innerHeight+.5;ctx.beginPath(),ctx.moveTo(padding,yMin),ctx.lineTo(width-padding,yMin),ctx.stroke();var yMax=padding+.5;ctx.beginPath(),ctx.moveTo(padding,yMax),ctx.lineTo(width-padding,yMax),ctx.stroke(),ctx.restore(),ctx.beginPath(),ctx.strokeStyle="#5cb85c",ctx.lineWidth=2,ctx.lineJoin="round",ctx.moveTo(padding,yLocation(first)),points.forEach(function(point,i){i>0&&ctx.lineTo(padding+i*xDelta,yLocation(point))}),ctx.stroke(),ctx.font="14px Arial",ctx.fillText(minmax.max.toFixed(2),padding,20),ctx.fillText(minmax.min.toFixed(2),padding,height-10)}function arrayMinMax(array){var min=Number.POSITIVE_INFINITY,max=Number.NEGATIVE_INFINITY;array.forEach(function(value){value<min&&(min=value),value>max&&(max=value)});var scale=Math.abs(max-min);return{min:min,max:max,scale:scale,apply:function(value){return(value-min)/scale}}}function normalizeInvert(array){var minmax=arrayMinMax(array);return array.map(function(value){return 1-minmax.apply(value)})}return{draw:draw}}(); | ||
window.Dataline=function(){function draw(id,data){var canvas=document.getElementById(id);if(!canvas)throw new Error("Element with id "+id+" does not exist.");if(!data){var values=canvas.getAttribute("data-values");""===values?data=[]:values&&(data=values.split(",").map(function(value){return parseFloat(value)}))}if(!data)throw new Error("Chart data is not set.");drawInAnimationFrame(canvas,data),window.addEventListener("resize",function(){drawInAnimationFrame(canvas,data)},!1)}function drawInAnimationFrame(canvas,data){window.requestAnimationFrame(function(){canvas.width=Math.floor(canvas.offsetWidth),canvas.height=Math.floor(canvas.offsetHeight);var ctx=canvas.getContext("2d");ctx.clearRect(0,0,canvas.width,canvas.height),ctx.imageSmoothingEnabled=!0,drawData(ctx,30,data)})}function drawData(ctx,padding,values){function yLocation(value){return padding+value*innerHeight}var canvas=ctx.canvas,height=canvas.height,width=canvas.width,points=normalizeInvert(values),innerWidth=width-2*padding,innerHeight=height-2*padding,xDelta=innerWidth/points.length,minmax=arrayMinMax(values),first=points[0];if(ctx.save(),ctx.lineWidth=1,ctx.strokeStyle="#999",ctx.setLineDash([2]),minmax.min<0&&minmax.max>0){var yZero=Math.floor(yLocation(1-minmax.apply(0)))+.5;ctx.beginPath(),ctx.moveTo(padding,yZero),ctx.lineTo(width-padding,yZero),ctx.stroke()}var yMin=padding+innerHeight+.5;ctx.beginPath(),ctx.moveTo(padding,yMin),ctx.lineTo(width-padding,yMin),ctx.stroke();var yMax=padding+.5;ctx.beginPath(),ctx.moveTo(padding,yMax),ctx.lineTo(width-padding,yMax),ctx.stroke(),ctx.restore(),ctx.beginPath(),ctx.strokeStyle="#5cb85c",ctx.lineWidth=2,ctx.lineJoin="round",ctx.moveTo(padding,yLocation(first)),points.forEach(function(point,i){i>0&&ctx.lineTo(padding+i*xDelta,yLocation(point))}),ctx.stroke(),ctx.font="14px Arial",ctx.fillText(minmax.max.toFixed(2),padding,20),ctx.fillText(minmax.min.toFixed(2),padding,height-10)}function arrayMinMax(array){var min=Number.POSITIVE_INFINITY,max=Number.NEGATIVE_INFINITY;array.forEach(function(value){value<min&&(min=value),value>max&&(max=value)});var scale=Math.abs(max-min);return{min:min,max:max,scale:scale,apply:function(value){return(value-min)/scale}}}function normalizeInvert(array){var minmax=arrayMinMax(array);return array.map(function(value){return 1-minmax.apply(value)})}return{draw:draw}}(); |
{ | ||
"name": "dataline", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "uglify-js": "^2.7.3" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9486
118