Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Chart.extension.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Chart extension for making the bars rounded
  3. // Code from: https://codepen.io/jedtrow/full/ygRYgo
  4. //
  5. Chart.elements.Rectangle.prototype.draw = function() {
  6. var ctx = this._chart.ctx;
  7. var vm = this._view;
  8. var left, right, top, bottom, signX, signY, borderSkipped, radius;
  9. var borderWidth = vm.borderWidth;
  10. // Set Radius Here
  11. // If radius is large enough to cause drawing errors a max radius is imposed
  12. var cornerRadius = 6;
  13. if (!vm.horizontal) {
  14. // bar
  15. left = vm.x - vm.width / 2;
  16. right = vm.x + vm.width / 2;
  17. top = vm.y;
  18. bottom = vm.base;
  19. signX = 1;
  20. signY = bottom > top ? 1 : -1;
  21. borderSkipped = vm.borderSkipped || 'bottom';
  22. } else {
  23. // horizontal bar
  24. left = vm.base;
  25. right = vm.x;
  26. top = vm.y - vm.height / 2;
  27. bottom = vm.y + vm.height / 2;
  28. signX = right > left ? 1 : -1;
  29. signY = 1;
  30. borderSkipped = vm.borderSkipped || 'left';
  31. }
  32. // Canvas doesn't allow us to stroke inside the width so we can
  33. // adjust the sizes to fit if we're setting a stroke on the line
  34. if (borderWidth) {
  35. // borderWidth shold be less than bar width and bar height.
  36. var barSize = Math.min(Math.abs(left - right), Math.abs(top - bottom));
  37. borderWidth = borderWidth > barSize ? barSize : borderWidth;
  38. var halfStroke = borderWidth / 2;
  39. // Adjust borderWidth when bar top position is near vm.base(zero).
  40. var borderLeft = left + (borderSkipped !== 'left' ? halfStroke * signX : 0);
  41. var borderRight = right + (borderSkipped !== 'right' ? -halfStroke * signX : 0);
  42. var borderTop = top + (borderSkipped !== 'top' ? halfStroke * signY : 0);
  43. var borderBottom = bottom + (borderSkipped !== 'bottom' ? -halfStroke * signY : 0);
  44. // not become a vertical line?
  45. if (borderLeft !== borderRight) {
  46. top = borderTop;
  47. bottom = borderBottom;
  48. }
  49. // not become a horizontal line?
  50. if (borderTop !== borderBottom) {
  51. left = borderLeft;
  52. right = borderRight;
  53. }
  54. }
  55. ctx.beginPath();
  56. ctx.fillStyle = vm.backgroundColor;
  57. ctx.strokeStyle = vm.borderColor;
  58. ctx.lineWidth = borderWidth;
  59. // Corner points, from bottom-left to bottom-right clockwise
  60. // | 1 2 |
  61. // | 0 3 |
  62. var corners = [
  63. [left, bottom],
  64. [left, top],
  65. [right, top],
  66. [right, bottom]
  67. ];
  68. // Find first (starting) corner with fallback to 'bottom'
  69. var borders = ['bottom', 'left', 'top', 'right'];
  70. var startCorner = borders.indexOf(borderSkipped, 0);
  71. if (startCorner === -1) {
  72. startCorner = 0;
  73. }
  74. function cornerAt(index) {
  75. return corners[(startCorner + index) % 4];
  76. }
  77. // Draw rectangle from 'startCorner'
  78. var corner = cornerAt(0);
  79. ctx.moveTo(corner[0], corner[1]);
  80. for (var i = 1; i < 4; i++) {
  81. corner = cornerAt(i);
  82. nextCornerId = i + 1;
  83. if (nextCornerId == 4) {
  84. nextCornerId = 0
  85. }
  86. nextCorner = cornerAt(nextCornerId);
  87. width = corners[2][0] - corners[1][0];
  88. height = corners[0][1] - corners[1][1];
  89. x = corners[1][0];
  90. y = corners[1][1];
  91. var radius = cornerRadius;
  92. // Fix radius being too large
  93. if (radius > height / 2) {
  94. radius = height / 2;
  95. }
  96. if (radius > width / 2) {
  97. radius = width / 2;
  98. }
  99. ctx.moveTo(x + radius, y);
  100. ctx.lineTo(x + width - radius, y);
  101. ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  102. ctx.lineTo(x + width, y + height - radius);
  103. ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  104. ctx.lineTo(x + radius, y + height);
  105. ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  106. ctx.lineTo(x, y + radius);
  107. ctx.quadraticCurveTo(x, y, x + radius, y);
  108. }
  109. ctx.fill();
  110. if (borderWidth) {
  111. ctx.stroke();
  112. }
  113. };