1
0

element.point.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. module.exports = function(Chart) {
  3. var helpers = Chart.helpers,
  4. globalOpts = Chart.defaults.global,
  5. defaultColor = globalOpts.defaultColor;
  6. globalOpts.elements.point = {
  7. radius: 3,
  8. pointStyle: 'circle',
  9. backgroundColor: defaultColor,
  10. borderWidth: 1,
  11. borderColor: defaultColor,
  12. // Hover
  13. hitRadius: 1,
  14. hoverRadius: 4,
  15. hoverBorderWidth: 1
  16. };
  17. function xRange(mouseX) {
  18. var vm = this._view;
  19. return vm ? (Math.pow(mouseX - vm.x, 2) < Math.pow(vm.radius + vm.hitRadius, 2)) : false;
  20. }
  21. function yRange(mouseY) {
  22. var vm = this._view;
  23. return vm ? (Math.pow(mouseY - vm.y, 2) < Math.pow(vm.radius + vm.hitRadius, 2)) : false;
  24. }
  25. Chart.elements.Point = Chart.Element.extend({
  26. inRange: function(mouseX, mouseY) {
  27. var vm = this._view;
  28. return vm ? ((Math.pow(mouseX - vm.x, 2) + Math.pow(mouseY - vm.y, 2)) < Math.pow(vm.hitRadius + vm.radius, 2)) : false;
  29. },
  30. inLabelRange: xRange,
  31. inXRange: xRange,
  32. inYRange: yRange,
  33. getCenterPoint: function() {
  34. var vm = this._view;
  35. return {
  36. x: vm.x,
  37. y: vm.y
  38. };
  39. },
  40. getArea: function() {
  41. return Math.PI * Math.pow(this._view.radius, 2);
  42. },
  43. tooltipPosition: function() {
  44. var vm = this._view;
  45. return {
  46. x: vm.x,
  47. y: vm.y,
  48. padding: vm.radius + vm.borderWidth
  49. };
  50. },
  51. draw: function() {
  52. var vm = this._view;
  53. var ctx = this._chart.ctx;
  54. var pointStyle = vm.pointStyle;
  55. var radius = vm.radius;
  56. var x = vm.x;
  57. var y = vm.y;
  58. if (vm.skip) {
  59. return;
  60. }
  61. ctx.strokeStyle = vm.borderColor || defaultColor;
  62. ctx.lineWidth = helpers.getValueOrDefault(vm.borderWidth, globalOpts.elements.point.borderWidth);
  63. ctx.fillStyle = vm.backgroundColor || defaultColor;
  64. Chart.canvasHelpers.drawPoint(ctx, pointStyle, radius, x, y);
  65. }
  66. });
  67. };