Guest User

Untitled

a guest
Jun 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. const data = [
  4. [10, 100],
  5. [80, 800],
  6. [20, 200],
  7. [95, 400],
  8. [50, 500],
  9. ];
  10.  
  11. const full = 100 + 200 + 500 + 800 + 400;
  12. const height = 100;
  13. const width = 1000;
  14.  
  15. function prepareSVG() {
  16. const points = data.map(
  17. ([likes, duration]) => {
  18. const h = height * (likes / 100);
  19. const w = width * (duration / full)
  20.  
  21. return { w, h };
  22. }
  23. )
  24. .reduce(
  25. (res, cur) => res.concat(calculateNext(res, cur)),
  26. ` 0,${height}`
  27. )
  28. .concat(` ${width},${height}`)
  29. .trim();
  30.  
  31. return `
  32. <svg width="1000" height="1000">
  33. <polyline points="${points}" style="fill:rgba(95, 107, 144, 0.424);stroke:blue;stroke-width:1"/>
  34. </svg>
  35. `;
  36. }
  37.  
  38. function parsePrevPosition(path) {
  39. const [ x, y ] = path.slice(path.lastIndexOf(' ')).split(',');
  40.  
  41. return { x: +x, y: +y };
  42. }
  43.  
  44. function calculateNext(path, { w, h }) {
  45. const { x, y } = parsePrevPosition(path);
  46.  
  47. const nextX = x + w;
  48. const nextY = height - h;
  49.  
  50. return ` ${nextX},${nextY}`;
  51. };
  52.  
  53.  
  54. fs.createWriteStream('./index.svg')
  55. .end(prepareSVG());
Add Comment
Please, Sign In to add comment