drawGrid.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var stepX = 77 / 14;
  2. function point(x, y) {
  3. x: 0;
  4. y: 0;
  5. }
  6. /* DRAW GRID */
  7. function drawGrid(graph) {
  8. $(graph).html("<defs>\n" +
  9. " <clipPath id=\"clip\" x=\"0\" y=\"0\" width=\"80\" height=\"40\" >\n" +
  10. " <rect id=\"clip-rect\" x=\"-80\" y=\"0\" width=\"77\" height=\"38.7\"/>\n" +
  11. " </clipPath>\n" +
  12. "\n" +
  13. " <linearGradient id=\"gradient-1\">\n" +
  14. " <stop offset=\"0\" stop-color=\"#00d5bd\" />\n" +
  15. " <stop offset=\"100\" stop-color=\"#24c1ed\" />\n" +
  16. " </linearGradient>\n" +
  17. "\n" +
  18. " <linearGradient id=\"gradient-2\">\n" +
  19. " <stop offset=\"0\" stop-color=\"#954ce9\" />\n" +
  20. " <stop offset=\"0.3\" stop-color=\"#954ce9\" />\n" +
  21. " <stop offset=\"0.6\" stop-color=\"#24c1ed\" />\n" +
  22. " <stop offset=\"1\" stop-color=\"#24c1ed\" />\n" +
  23. " </linearGradient>\n" +
  24. "\n" +
  25. " <linearGradient id=\"gradient-3\" x1=\"0%\" y1=\"0%\" x2=\"0%\" y2=\"100%\">>\n" +
  26. " <stop offset=\"0\" stop-color=\"rgba(0, 213, 189, 1)\" stop-opacity=\"0.07\"/>\n" +
  27. " <stop offset=\"0.5\" stop-color=\"rgba(0, 213, 189, 1)\" stop-opacity=\"0.13\"/>\n" +
  28. " <stop offset=\"1\" stop-color=\"rgba(0, 213, 189, 1)\" stop-opacity=\"0\"/>\n" +
  29. " </linearGradient>\n" +
  30. "\n" +
  31. " <linearGradient id=\"gradient-4\" x1=\"0%\" y1=\"0%\" x2=\"0%\" y2=\"100%\">>\n" +
  32. " <stop offset=\"0\" stop-color=\"rgba(149, 76, 233, 1)\" stop-opacity=\"0.07\"/>\n" +
  33. " <stop offset=\"0.5\" stop-color=\"rgba(149, 76, 233, 1)\" stop-opacity=\"0.13\"/>\n" +
  34. " <stop offset=\"1\" stop-color=\"rgba(149, 76, 233, 1)\" stop-opacity=\"0\"/>\n" +
  35. " </linearGradient>\n" +
  36. "\n" +
  37. " </defs>")
  38. var graph = Snap(graph);
  39. var g = graph.g();
  40. g.attr('id', 'grid');
  41. for (i = 0; i <= stepX + 2; i++) {
  42. var horizontalLine = graph.path(
  43. "M" + 0 + "," + stepX * i + " " +
  44. "L" + 77 + "," + stepX * i);
  45. horizontalLine.attr('class', 'horizontal');
  46. g.add(horizontalLine);
  47. };
  48. for (i = 0; i <= 14; i++) {
  49. var horizontalLine = graph.path(
  50. "M" + stepX * i + "," + 38.7 + " " +
  51. "L" + stepX * i + "," + 0)
  52. horizontalLine.attr('class', 'vertical');
  53. g.add(horizontalLine);
  54. };
  55. }
  56. function drawLineGraph(graph, points, id,max_v ,min_v ) {
  57. var graph = Snap(graph);
  58. //
  59. // graph.g().remove()
  60. /*END DRAW GRID*/
  61. /* PARSE POINTS */
  62. var myPoints = [];
  63. var shadowPoints = [];
  64. function parseData(points) {
  65. points_max = Math.max.apply(Math,points)
  66. points_min = Math.min.apply(Math,points)
  67. if(max_v != 0){
  68. points_max = max_v
  69. points_min = min_v
  70. }
  71. points_zong = points_max - points_min
  72. // if(points_zong >)
  73. // console.log("points_max:",points_max,"points_min:",points_min,"points_zong:",points_zong)
  74. // console.log("points:",points)
  75. for (i = 0; i < points.length; i++) {
  76. var p = new point();
  77. points_v = points[i] - points_min
  78. if(points_min < 0){
  79. points_v = points[i]+Math.abs(points_min);
  80. }
  81. var pv = (points_v) / points_zong * 100 ; // 放大倍
  82. pv = 100 - pv;
  83. // console.log( points_v , points_zong ,"pv:",pv)
  84. p.x = 77 / points.length * i + 2;
  85. p.y = 3.3 + pv / 3;
  86. // if (p.x > 78) {
  87. // p.x = 78;
  88. // }
  89. myPoints.push(p);
  90. }
  91. }
  92. var segments = [];
  93. function createSegments(p_array) {
  94. for (i = 0; i < p_array.length; i++) {
  95. var seg = "L" + p_array[i].x + "," + p_array[i].y;
  96. if (i === 0) {
  97. seg = "M" + p_array[i].x + "," + p_array[i].y;
  98. }
  99. segments.push(seg);
  100. }
  101. }
  102. function joinLine(segments_array, id) {
  103. var line = segments_array.join(" ");
  104. var line = graph.path(line);
  105. line.attr('id', 'graph-' + id);
  106. // var lineLength = line.getTotalLength();
  107. // line.attr({
  108. // 'stroke-dasharray': lineLength,
  109. // 'stroke-dashoffset': lineLength
  110. // });
  111. }
  112. function showValues() {
  113. var val1 = $(graph).find('.h-value');
  114. var val2 = $(graph).find('.percentage-value');
  115. val1.addClass('visible');
  116. val2.addClass('visible');
  117. }
  118. function drawPolygon(segments, id) {
  119. var lastel = segments[segments.length - 1];
  120. var polySeg = segments.slice();
  121. polySeg.push([78, 38.4], [1, 38.4]);
  122. var polyLine = polySeg.join(' ').toString();
  123. var replacedString = polyLine.replace(/L/g, '').replace(/M/g, "");
  124. var poly = graph.polygon(replacedString);
  125. var clip = graph.rect(-80, 0, 80, 40);
  126. poly.attr({
  127. 'id': 'poly-' + id,
  128. /*'clipPath':'url(#clip)'*/
  129. 'clipPath': clip
  130. });
  131. clip.animate({
  132. transform: 't80,0'
  133. }, 1300, mina.linear);
  134. }
  135. parseData(points);
  136. createSegments(myPoints);
  137. // console.log("segments:",segments)
  138. joinLine(segments,id);
  139. drawPolygon(segments, id); // 绘画 下面阴影
  140. /*$('#poly-'+id).attr('class','show');*/
  141. /* function drawPolygon(segments,id){
  142. var polySeg = segments;
  143. polySeg.push([80,40],[0,40]);
  144. var polyLine = segments.join(' ').toString();
  145. var replacedString = polyLine.replace(/L/g,'').replace(/M/g,"");
  146. var poly = graph.polygon(replacedString);
  147. poly.attr('id','poly-'+id)
  148. }
  149. drawPolygon(segments,id);*/
  150. }