Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. var canvas;
  2. var context;
  3. var margin;
  4. var startX;
  5. var endX;
  6. var endY;
  7. var startY;
  8. var chartWidth;
  9. var chartHeight;
  10. var nodeSideLength;//Node square side length
  11. var colors = [];
  12. var foundNode = false; //Used for the reapinting procedure
  13. var nodes = [];
  14. var chartData;
  15.  
  16. //Settings variables
  17. var chartColor = "black";
  18. var backgroundColor = "#FFFFFF";
  19. var chartLinesColor = "lightgray";
  20. var textColor = "black";
  21.  
  22.  
  23. function chartify(canvas, chartData, settings) {
  24. this.canvas = canvas;
  25. context = canvas.getContext("2d");
  26. margin = 50;
  27. startX = margin;
  28. endX = canvas.width-margin-80;
  29. endY = canvas.height-margin;
  30. startY = margin;
  31. chartWidth = endX-startX;
  32. chartHeight = endY-startY;
  33. nodeSideLength = 8;
  34. this.chartData = chartData;
  35.  
  36. if (settings != undefined) {
  37. if (settings.chartColor != "" && settings.chartColor != undefined) {
  38. this.chartColor = settings.chartColor;
  39. }
  40. if (settings.backgroundColor != "" && settings.backgroundColor != undefined) {
  41. this.backgroundColor = settings.backgroundColor;
  42. }
  43. if (settings.chartLinesColor != "" && settings.chartLinesColor != undefined) {
  44. this.chartLinesColor = settings.chartLinesColor;
  45. }
  46. if (settings.textColor != "" && settings.textColor != undefined) {
  47. this.textColor = settings.textColor;
  48. }
  49. }
  50.  
  51. canvas.style.backgroundColor = backgroundColor;
  52.  
  53. //Generate colors for all the data sets
  54. for (var i = 0; i < chartData.data.length; i++) {
  55. colors[i] = generateHexColor();
  56. }
  57.  
  58. drawChart(chartData);
  59.  
  60. canvas.onmousemove = function(e) {
  61. checkCollision(e.clientX, e.clientY);
  62. }
  63. }
  64.  
  65. function drawChart() {
  66. drawAxis(chartData);
  67. drawChartLines(chartData);
  68. drawInfo(chartData);
  69. }
  70.  
  71. //Draws the line info and color
  72. function drawInfo(chartData) {
  73. var infoStartX = endX+20;
  74. var infoStartY = startY;
  75. var infoRectWidth = 100;
  76. var infoRectHeight = chartData.data.length*40;
  77.  
  78. context.fillStyle = backgroundColor;
  79. context.fillRect(infoStartX, infoStartY, infoRectWidth, infoRectHeight);
  80.  
  81. for (var i = 0; i < chartData.data.length; i++) {
  82. var textWidth = context.measureText(chartData.data[i].name).width;
  83. context.fillStyle = textColor;
  84. context.font = "Bold 8pt Arial";
  85. context.fillText(chartData.data[i].name, infoStartX+(infoRectWidth/2)-(textWidth/2), infoStartY+20+(i*25));
  86. context.strokeStyle = colors[i];
  87. context.lineWidth = 2.5;
  88. drawLine(infoStartX+infoRectWidth*0.1, infoStartY+25+(i*25), infoStartX+infoRectWidth*0.9, infoStartY+25+(i*25));
  89. }
  90.  
  91. }
  92.  
  93. function drawAxis(chartData) {
  94. var cols = chartData.cols;
  95. var YAxisMax = getYAxisMax(chartData.data);
  96.  
  97. context.strokeStyle = chartColor;
  98. context.fillStyle = chartColor;
  99.  
  100. //Draws chart lines
  101. drawLine(startX, startY, startX, endY);
  102. drawLine(startX, endY, endX, endY);
  103.  
  104. //Draws y-axis arrow and x-axis arrow
  105. drawTriangle(startX, startY-7, startX+5, startY, startX-5, startY);
  106. drawTriangle(endX+7, endY, endX, endY+5, endX, endY-5);
  107.  
  108. context.fillStyle = textColor;
  109. context.font = "8pt Arial";
  110. context.strokeStyle = chartLinesColor; //For background lines color
  111. context.lineWidth = 0.5; //For background lines width
  112.  
  113. //Draws column names
  114. for (var i = 0; i < cols.length; i++) {
  115. context.fillText(cols[i], i*(chartWidth/cols.length)+margin+15, chartHeight+margin+15);
  116. }
  117.  
  118. //Draws Y-axis values and background lines
  119. for (var i = 0; i < 11; i++) {
  120. var value = (YAxisMax/10)*i;
  121. context.fillText(value, startX-context.measureText(value).width-5,
  122. ((11-i)*(chartHeight/11)+(margin-11)));
  123. drawLine(startX+1, ((11-i)*(chartHeight/11)+(margin-11)), endX, ((11-i)*(chartHeight/11)+(margin-11)));
  124. }
  125. }
  126.  
  127. function drawChartLines(chartData) {
  128. var yMax = getYAxisMax(chartData.data);
  129. var yDiff = chartHeight/(yMax+(yMax/10));
  130. var xDiff = chartWidth/chartData.cols.length;
  131. var nodeX;
  132. var nodeY;
  133.  
  134. context.lineWidth = 0.7;
  135.  
  136. for (var i = 0; i < chartData.data.length; i++) {
  137. context.strokeStyle = colors[i];
  138. var values = chartData.data[i].values;
  139.  
  140. for (var j = 0; j < values.length-1; j++) {
  141. //Draws the line between two nodes
  142. drawLine(margin+25+xDiff*j, endY-11-values[j]*yDiff,
  143. margin+25+xDiff*(j+1), endY-11-values[j+1]*yDiff);
  144.  
  145. context.fillStyle = colors[i];
  146.  
  147. nodeX = margin-(nodeSideLength/2)+25+xDiff*j;
  148. nodeY = endY-(nodeSideLength/2)-11-values[j]*yDiff;
  149.  
  150. //Draws the node for the current line
  151. context.fillRect(nodeX, nodeY, nodeSideLength, nodeSideLength);
  152.  
  153. //Save the node info for generating detail popup on hover
  154. nodes[nodes.length] = {"name": chartData.data[i].name,
  155. "col": chartData.cols[j],
  156. "value": values[j],
  157. "xpos": nodeX,
  158. "ypos": nodeY,
  159. "color": colors[i]
  160. };
  161. }
  162. nodeX = margin-(nodeSideLength/2)+25+xDiff*(values.length-1);
  163. nodeY = endY-(nodeSideLength/2)-11-values[values.length-1]*yDiff;
  164.  
  165. //Draws the last node of the line
  166. context.fillRect(nodeX, nodeY, nodeSideLength, nodeSideLength);
  167.  
  168. //Save the node info for generating detail popup on hover
  169. nodes[nodes.length] = {"name": chartData.data[i].name,
  170. "col": chartData.cols[chartData.cols.length-1],
  171. "value": values[values.length-1],
  172. "xpos": nodeX,
  173. "ypos": nodeY,
  174. "color": colors[i]
  175. };
  176. }
  177. }
  178.  
  179. //Draws a line to canvas
  180. function drawLine(x1, y1, x2, y2) {
  181. context.beginPath();
  182. context.moveTo(x1, y1);
  183. context.lineTo(x2, y2);
  184. context.closePath();
  185. context.stroke();
  186. }
  187.  
  188. //Draws a traingle to canvas
  189. function drawTriangle(x1, y1, x2, y2, x3, y3) {
  190. context.beginPath();
  191. context.moveTo(x1, y1);
  192. context.lineTo(x2, y2);
  193. context.lineTo(x3, y3);
  194. context.lineTo(x1, y1);
  195. context.closePath();
  196. context.fill();
  197. }
  198.  
  199. //Returns the highest value found in data arrays
  200. function getYAxisMax(arrays) {
  201. var largestNumber = 0;
  202. for (var i = 0; i < arrays.length; i++) {
  203. var temp = Math.max.apply(Math, arrays[i].values);
  204. if (temp > largestNumber) {
  205. largestNumber = temp;
  206. }
  207. }
  208.  
  209. if (largestNumber <= 1) {
  210. return 1;
  211. }
  212. if (largestNumber <= 100) {
  213. return Math.ceil(largestNumber/10)*10;
  214. }
  215.  
  216. return Math.ceil(largestNumber/100)*100;
  217.  
  218. }
  219.  
  220. //Returns a hex color
  221. function generateHexColor() {
  222. var chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
  223. var color = "#";
  224. for (var i = 0; i < 6; i++) {
  225. color+=chars[Math.round(Math.random()*(chars.length-1))];
  226. }
  227. return color;
  228. }
  229.  
  230. //Checks if mouse pointer is over a node
  231. function checkCollision(x, y) {
  232. var canvas_element = document.getElementById("canvas");
  233. var offset = {
  234. x: canvas_element.offsetLeft,
  235. y: canvas_element.offsetTop
  236. };
  237.  
  238. for (var i = 0; i < nodes.length; i++) {
  239. var nodeX = nodes[i].xpos;
  240. var nodeY = nodes[i].ypos;
  241.  
  242. if (x > nodeX+offset.x && x < nodeX+nodeSideLength+offset.x && y > nodeY+offset.y && y < nodeY+nodeSideLength+offset.y) {
  243. foundNode = true;
  244. drawDetails(nodes[i]);
  245. return;
  246. }
  247. else {
  248. if (foundNode == true) {
  249. repaint();
  250. foundNode = false;
  251. }
  252. }
  253. }
  254. }
  255.  
  256. //Draws the detail information when mouse is over a line node
  257. function drawDetails(node) {
  258. var rectWidth = 170;
  259. var rectHeight = 60;
  260.  
  261. context.beginPath();
  262. context.fillStyle = backgroundColor;
  263. context.strokeStyle = node.color;
  264. context.lineWidth = 2.0;
  265. context.font = "Bold 10pt Arial";
  266. context.fillRect(node.xpos, node.ypos, rectWidth, rectHeight);
  267. context.strokeRect(node.xpos, node.ypos, rectWidth, rectHeight);
  268. context.fillStyle = textColor;
  269. context.fillText(node.name, node.xpos+rectWidth/2-context.measureText(node.name).width/2, node.ypos+18);
  270. context.font = "10pt Arial";
  271. context.fillText(chartData.xName + ": " + node.col, node.xpos+5, node.ypos+37);
  272. context.fillText(chartData.yName + ": " + node.value, node.xpos+5, node.ypos+53);
  273. }
  274.  
  275. //Reapaints the whole surface
  276. function repaint() {
  277. context.beginPath();
  278. context.clearRect(0, 0, canvas.width, canvas.height);
  279. drawChart();
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement