16 var LineChart =
function() {
21 var margins = {
top: 50, bottom: 50, left: 75, right: 50};
22 var chartHeight, chartWidth;
24 var useDataLabels =
false;
25 var useAxisLabels =
true;
26 var usePersistanceDisplay =
false;
27 var useIncrLabels =
true;
28 var fadeGblAlpha = 0.05;
37 var gridStrokeStyle =
"lightgray";
38 var dataStrokeStyle =
"black";
39 var dataLineWidth = 2;
42 var render =
function(dataId,gridId,inData){
44 if(data.margins != null){margins=data.margins};
45 if(data.useGrid != null){useGrid=data.useGrid};
46 if(data.useDataLabels != null){useDataLabels=data.useDataLabels};
47 if(data.useAxisLabels != null){useAxisLabels=data.useAxisLabels};
48 if(data.usePersistanceDisplay != null){useAxisLabels=data.useAxisLabels};
49 if(data.fadeGblAlpha != null){fadeGblAlpha=data.fadeGblAlpha};
50 if(data.timeoutVal != null){timeoutVal=data.timeoutVal};
51 if(data.gridStrokeStyle != null){useAxisLabels=data.gridStrokeStyle};
52 if(data.dataStrokeStyle != null){dataStrokeStyle=data.dataStrokeStyle};
53 if(data.dataLineWidth != null){dataLineWidth=data.dataLineWidth};
54 if(data.HorizontalDivs != null){hDivs=data.HorizontalDivs}
55 else{hDivs = data.dataPoints.length-1};
56 if(data.VerticalDivs != null){vDivs=data.VerticalDivs}
57 else{vDivs = data.dataPoints.length-1};
61 gridCanvas = document.getElementById(gridId);
62 gridCtx = gridCanvas.getContext(
"2d");
63 dataCanvas = document.getElementById(dataId);
64 dataCtx = dataCanvas.getContext(
"2d");
66 xMaxPx = data.chartWidth;
67 chartWidth = xMaxPx + (margins.left + margins.right);
68 yMaxPx = data.chartHeight;
69 chartHeight = yMaxPx + (margins.top + margins.bottom);
71 maxXVal = getMaxXVal()
72 maxYVal = Math.round(getMaxYVal() / 10) * 10;
73 minXVal = getMinXVal()
74 minYVal = Math.round(getMinYVal() / 10) * 10;
79 yRatio = yMaxPx/(maxYVal-minYVal);
82 yRatio = yMaxPx/(maxYVal);
86 xRatio = xMaxPx/(maxXVal-minXVal);
91 dataCanvas.style.height = dataCanvas.height = chartHeight*window.devicePixelRatio;
92 dataCanvas.style.width = dataCanvas.width = chartWidth*window.devicePixelRatio;
93 dataCtx.scale(window.devicePixelRatio,window.devicePixelRatio);
96 gridCanvas.style.height = gridCanvas.height = chartHeight*window.devicePixelRatio;
97 gridCanvas.style.width = gridCanvas.width = chartWidth*window.devicePixelRatio;
98 gridCtx.scale(window.devicePixelRatio,window.devicePixelRatio);
107 var renderChart =
function () {
109 if (useGrid ==
true){renderGrid()};
110 if (useDataLabels ==
true){renderDataLabels()};
111 if (useAxisLabels ==
true){renderAxisLabels()};
112 if (useIncrLabels ==
true){renderIncrLabels()};
114 renderData(data.dataPoints);
117 var renderGrid =
function () {
120 var vDivSpacing = (xMaxPx/vDivs);
121 var hDivSpacing = (yMaxPx/hDivs);
126 for (var x =margins.left; x <= xMaxPx + margins.left; x += vDivSpacing){
127 gridCtx.moveTo(0.5 + x, margins.top);
128 gridCtx.lineTo(0.5 + x, yMaxPx+margins.top);
131 for (var x=margins.top; x <= yMaxPx+margins.top; x += hDivSpacing){
132 gridCtx.moveTo(margins.left, 0.5 + x);
133 gridCtx.lineTo(xMaxPx+margins.left, 0.5 + x);
135 gridCtx.strokeStyle = gridStrokeStyle;
142 var renderDataLabels =
function () {
147 var renderAxisLabels =
function () {
148 var labelFont = (data.labelFont != null)?data.labelFont:
'20pt Arial';
149 gridCtx.font = labelFont;
150 gridCtx.textAlign =
"center";
153 if(data.title !=
"" && data.title != null){
154 var size = gridCtx.measureText(data.title);
155 gridCtx.fillText(data.title, (margins.left+(xMaxPx/2)), (margins.top/1.5));
160 if(data.xLabel !=
"" && data.xLabel != null){
162 size = gridCtx.measureText(data.xLabel);
163 gridCtx.fillText(data.xLabel, (margins.left+(xMaxPx/2)), (yMaxPx+margins.top+margins.bottom/1.5))
167 if(data.yLabel !=
"" && data.yLabel != null){
169 gridCtx.rotate(-Math.PI / 2);
170 gridCtx.font = labelFont;
171 gridCtx.fillText(data.yLabel, (margins.top+(yMaxPx/ 2)) * -1, margins.left / 4);
177 var renderIncrLabels =
function() {
179 gridCtx.font = (data.dataPointFont != null) ? data.dataPointFont :
'10pt Calibri';
182 startYLabel = minYVal;
189 var startXLabel = minXVal;
191 console.log(
"First X Label: " + startXLabel);
192 console.log(
"First Y Label: " + startYLabel);
196 var vDivSpacing = (xMaxPx/vDivs);
197 var hDivSpacing = (yMaxPx/hDivs);
205 for (var i = 0; i < hDivs+1; i++){
206 yPos += (i == 0) ? margins.top : hDivSpacing;
207 var txt = maxYVal-((i == 0) ? Math.abs(startYLabel) : i*((maxYVal-minYVal)/hDivs));
208 txt = +txt.toFixed(1);
209 console.log(i +
" " + txt);
210 var txtSize = gridCtx.measureText(txt);
211 gridCtx.fillText(txt, margins.left - ((txtSize.width >= 14) ? txtSize.width : 10) -7, yPos + 4);
215 for (var i = 0; i < vDivs+1; i++){
216 xPos += (i == 0) ? margins.left : vDivSpacing;
217 var txt =(i*((maxXVal-minXVal)/vDivs))+startXLabel;
218 txt = +txt.toFixed(1);
219 var txtSize = gridCtx.measureText(txt);
220 gridCtx.fillText(txt, xPos, margins.top + yMaxPx + (margins.bottom / 3));
224 gridCtx.strokeStyle = gridStrokeStyle;
232 var renderData =
function (dataPts) {
233 var lastPt = {x: 0, y: 0};
234 var thisPt = {x: 0, y: 0};
238 for(var i = 0; i < data.dataPoints.length; i++){
239 var pt = data.dataPoints[i];
240 thisPt.x = ((pt.x-data.dataPoints[0].x) * xRatio) + margins.left;
241 thisPt.y = ((maxYVal - pt.y) * yRatio) + margins.top;
242 console.log(
"Current Point coords - x: " + thisPt.x +
" y: " + thisPt.y)
243 if (!(lastPt.x == 0) && !(lastPt.y == 0)){
244 dataCtx.moveTo(lastPt.x+0.5,lastPt.y+0.5);
245 dataCtx.lineTo(thisPt.x+0.5,thisPt.y+0.5);
247 ptArray.push(thisPt);
252 dataCtx.lineJoin =
"round";
253 dataCtx.lineCap =
"round";
254 dataCtx.strokeStyle = dataStrokeStyle;
255 dataCtx.lineWidth = dataLineWidth;
261 var updateData =
function (dataPts){
262 if(!usePersistanceDisplay){
263 dataCtx.clearRect(0, 0, chartWidth, chartHeight);
268 var enableFadeOut =
function () {
269 usePersistanceDisplay =
true;
273 var disableFadeOut =
function () {
274 usePersistanceDisplay =
false;
277 var fadeOut =
function () {
279 if(usePersistanceDisplay ==
true){
281 dataCtx.globalAlpha = fadeGblAlpha;
282 dataCtx.globalCompositeOperation=
'destination-out';
283 dataCtx.fillStyle=
'#FFF';
284 dataCtx.fillRect(0,0,dataCanvas.width, dataCanvas.height);
287 return setTimeout(
function() {
296 var getMaxXVal =
function () {
298 for (let pt of data.dataPoints){
306 var getMaxYVal =
function () {
308 for (let pt of data.dataPoints){
317 var getMinXVal =
function () {
318 var minVal = data.dataPoints[0].x;
319 for (let pt of data.dataPoints){
321 console.log(
"X: " + pt.x +
" < " + minVal +
", setting minVal to that.")
328 var getMinYVal =
function () {
329 var minVal = data.dataPoints[0].y;
330 for (let pt of data.dataPoints){
342 enableFadeOut: enableFadeOut,
343 disableFadeOut: enableFadeOut,
344 updateData: updateData