controller.radar.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. module.exports = function(Chart) {
  3. var helpers = Chart.helpers;
  4. Chart.defaults.radar = {
  5. aspectRatio: 1,
  6. scale: {
  7. type: 'radialLinear'
  8. },
  9. elements: {
  10. line: {
  11. tension: 0 // no bezier in radar
  12. }
  13. }
  14. };
  15. Chart.controllers.radar = Chart.DatasetController.extend({
  16. datasetElementType: Chart.elements.Line,
  17. dataElementType: Chart.elements.Point,
  18. linkScales: helpers.noop,
  19. update: function(reset) {
  20. var me = this;
  21. var meta = me.getMeta();
  22. var line = meta.dataset;
  23. var points = meta.data;
  24. var custom = line.custom || {};
  25. var dataset = me.getDataset();
  26. var lineElementOptions = me.chart.options.elements.line;
  27. var scale = me.chart.scale;
  28. // Compatibility: If the properties are defined with only the old name, use those values
  29. if ((dataset.tension !== undefined) && (dataset.lineTension === undefined)) {
  30. dataset.lineTension = dataset.tension;
  31. }
  32. helpers.extend(meta.dataset, {
  33. // Utility
  34. _datasetIndex: me.index,
  35. // Data
  36. _children: points,
  37. _loop: true,
  38. // Model
  39. _model: {
  40. // Appearance
  41. tension: custom.tension ? custom.tension : helpers.getValueOrDefault(dataset.lineTension, lineElementOptions.tension),
  42. backgroundColor: custom.backgroundColor ? custom.backgroundColor : (dataset.backgroundColor || lineElementOptions.backgroundColor),
  43. borderWidth: custom.borderWidth ? custom.borderWidth : (dataset.borderWidth || lineElementOptions.borderWidth),
  44. borderColor: custom.borderColor ? custom.borderColor : (dataset.borderColor || lineElementOptions.borderColor),
  45. fill: custom.fill ? custom.fill : (dataset.fill !== undefined ? dataset.fill : lineElementOptions.fill),
  46. borderCapStyle: custom.borderCapStyle ? custom.borderCapStyle : (dataset.borderCapStyle || lineElementOptions.borderCapStyle),
  47. borderDash: custom.borderDash ? custom.borderDash : (dataset.borderDash || lineElementOptions.borderDash),
  48. borderDashOffset: custom.borderDashOffset ? custom.borderDashOffset : (dataset.borderDashOffset || lineElementOptions.borderDashOffset),
  49. borderJoinStyle: custom.borderJoinStyle ? custom.borderJoinStyle : (dataset.borderJoinStyle || lineElementOptions.borderJoinStyle),
  50. // Scale
  51. scaleTop: scale.top,
  52. scaleBottom: scale.bottom,
  53. scaleZero: scale.getBasePosition()
  54. }
  55. });
  56. meta.dataset.pivot();
  57. // Update Points
  58. helpers.each(points, function(point, index) {
  59. me.updateElement(point, index, reset);
  60. }, me);
  61. // Update bezier control points
  62. me.updateBezierControlPoints();
  63. },
  64. updateElement: function(point, index, reset) {
  65. var me = this;
  66. var custom = point.custom || {};
  67. var dataset = me.getDataset();
  68. var scale = me.chart.scale;
  69. var pointElementOptions = me.chart.options.elements.point;
  70. var pointPosition = scale.getPointPositionForValue(index, dataset.data[index]);
  71. helpers.extend(point, {
  72. // Utility
  73. _datasetIndex: me.index,
  74. _index: index,
  75. _scale: scale,
  76. // Desired view properties
  77. _model: {
  78. x: reset ? scale.xCenter : pointPosition.x, // value not used in dataset scale, but we want a consistent API between scales
  79. y: reset ? scale.yCenter : pointPosition.y,
  80. // Appearance
  81. tension: custom.tension ? custom.tension : helpers.getValueOrDefault(dataset.lineTension, me.chart.options.elements.line.tension),
  82. radius: custom.radius ? custom.radius : helpers.getValueAtIndexOrDefault(dataset.pointRadius, index, pointElementOptions.radius),
  83. backgroundColor: custom.backgroundColor ? custom.backgroundColor : helpers.getValueAtIndexOrDefault(dataset.pointBackgroundColor, index, pointElementOptions.backgroundColor),
  84. borderColor: custom.borderColor ? custom.borderColor : helpers.getValueAtIndexOrDefault(dataset.pointBorderColor, index, pointElementOptions.borderColor),
  85. borderWidth: custom.borderWidth ? custom.borderWidth : helpers.getValueAtIndexOrDefault(dataset.pointBorderWidth, index, pointElementOptions.borderWidth),
  86. pointStyle: custom.pointStyle ? custom.pointStyle : helpers.getValueAtIndexOrDefault(dataset.pointStyle, index, pointElementOptions.pointStyle),
  87. // Tooltip
  88. hitRadius: custom.hitRadius ? custom.hitRadius : helpers.getValueAtIndexOrDefault(dataset.hitRadius, index, pointElementOptions.hitRadius)
  89. }
  90. });
  91. point._model.skip = custom.skip ? custom.skip : (isNaN(point._model.x) || isNaN(point._model.y));
  92. },
  93. updateBezierControlPoints: function() {
  94. var chartArea = this.chart.chartArea;
  95. var meta = this.getMeta();
  96. helpers.each(meta.data, function(point, index) {
  97. var model = point._model;
  98. var controlPoints = helpers.splineCurve(
  99. helpers.previousItem(meta.data, index, true)._model,
  100. model,
  101. helpers.nextItem(meta.data, index, true)._model,
  102. model.tension
  103. );
  104. // Prevent the bezier going outside of the bounds of the graph
  105. model.controlPointPreviousX = Math.max(Math.min(controlPoints.previous.x, chartArea.right), chartArea.left);
  106. model.controlPointPreviousY = Math.max(Math.min(controlPoints.previous.y, chartArea.bottom), chartArea.top);
  107. model.controlPointNextX = Math.max(Math.min(controlPoints.next.x, chartArea.right), chartArea.left);
  108. model.controlPointNextY = Math.max(Math.min(controlPoints.next.y, chartArea.bottom), chartArea.top);
  109. // Now pivot the point for animation
  110. point.pivot();
  111. });
  112. },
  113. draw: function(ease) {
  114. var meta = this.getMeta();
  115. var easingDecimal = ease || 1;
  116. // Transition Point Locations
  117. helpers.each(meta.data, function(point) {
  118. point.transition(easingDecimal);
  119. });
  120. // Transition and Draw the line
  121. meta.dataset.transition(easingDecimal).draw();
  122. // Draw the points
  123. helpers.each(meta.data, function(point) {
  124. point.draw();
  125. });
  126. },
  127. setHoverStyle: function(point) {
  128. // Point
  129. var dataset = this.chart.data.datasets[point._datasetIndex];
  130. var custom = point.custom || {};
  131. var index = point._index;
  132. var model = point._model;
  133. model.radius = custom.hoverRadius ? custom.hoverRadius : helpers.getValueAtIndexOrDefault(dataset.pointHoverRadius, index, this.chart.options.elements.point.hoverRadius);
  134. model.backgroundColor = custom.hoverBackgroundColor ? custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(dataset.pointHoverBackgroundColor, index, helpers.getHoverColor(model.backgroundColor));
  135. model.borderColor = custom.hoverBorderColor ? custom.hoverBorderColor : helpers.getValueAtIndexOrDefault(dataset.pointHoverBorderColor, index, helpers.getHoverColor(model.borderColor));
  136. model.borderWidth = custom.hoverBorderWidth ? custom.hoverBorderWidth : helpers.getValueAtIndexOrDefault(dataset.pointHoverBorderWidth, index, model.borderWidth);
  137. },
  138. removeHoverStyle: function(point) {
  139. var dataset = this.chart.data.datasets[point._datasetIndex];
  140. var custom = point.custom || {};
  141. var index = point._index;
  142. var model = point._model;
  143. var pointElementOptions = this.chart.options.elements.point;
  144. model.radius = custom.radius ? custom.radius : helpers.getValueAtIndexOrDefault(dataset.radius, index, pointElementOptions.radius);
  145. model.backgroundColor = custom.backgroundColor ? custom.backgroundColor : helpers.getValueAtIndexOrDefault(dataset.pointBackgroundColor, index, pointElementOptions.backgroundColor);
  146. model.borderColor = custom.borderColor ? custom.borderColor : helpers.getValueAtIndexOrDefault(dataset.pointBorderColor, index, pointElementOptions.borderColor);
  147. model.borderWidth = custom.borderWidth ? custom.borderWidth : helpers.getValueAtIndexOrDefault(dataset.pointBorderWidth, index, pointElementOptions.borderWidth);
  148. }
  149. });
  150. };