bullet.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* *
  2. * (c) 2010-2019 Kacper Madej
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. import '../parts/Utilities.js';
  9. var pick = H.pick,
  10. isNumber = H.isNumber,
  11. relativeLength = H.relativeLength,
  12. seriesType = H.seriesType,
  13. columnProto = H.seriesTypes.column.prototype;
  14. /**
  15. * The bullet series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.bullet
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. seriesType('bullet', 'column'
  24. /**
  25. * A bullet graph is a variation of a bar graph. The bullet graph features a
  26. * single measure, compares it to a target, and displays it in the context of
  27. * qualitative ranges of performance that could be set using
  28. * [plotBands](#yAxis.plotBands) on [yAxis](#yAxis).
  29. *
  30. * @sample {highcharts} highcharts/demo/bullet-graph/
  31. * Bullet graph
  32. *
  33. * @extends plotOptions.column
  34. * @since 6.0.0
  35. * @product highcharts
  36. * @excluding allAreas, boostThreshold, colorAxis, compare, compareBase
  37. * @optionparent plotOptions.bullet
  38. */
  39. , {
  40. /**
  41. * All options related with look and positiong of targets.
  42. *
  43. * @since 6.0.0
  44. */
  45. targetOptions: {
  46. /**
  47. * The width of the rectangle representing the target. Could be set
  48. * as a pixel value or as a percentage of a column width.
  49. *
  50. * @type {number|string}
  51. * @since 6.0.0
  52. */
  53. width: '140%',
  54. /**
  55. * The height of the rectangle representing the target.
  56. *
  57. * @since 6.0.0
  58. */
  59. height: 3,
  60. /**
  61. * The border color of the rectangle representing the target. When
  62. * not set, the point's border color is used.
  63. *
  64. * In styled mode, use class `highcharts-bullet-target` instead.
  65. *
  66. * @type {Highcharts.ColorString}
  67. * @since 6.0.0
  68. * @product highcharts
  69. * @apioption plotOptions.bullet.targetOptions.borderColor
  70. */
  71. /**
  72. * The color of the rectangle representing the target. When not set,
  73. * point's color (if set in point's options -
  74. * [`color`](#series.bullet.data.color)) or zone of the target value
  75. * (if [`zones`](#plotOptions.bullet.zones) or
  76. * [`negativeColor`](#plotOptions.bullet.negativeColor) are set)
  77. * or the same color as the point has is used.
  78. *
  79. * In styled mode, use class `highcharts-bullet-target` instead.
  80. *
  81. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  82. * @since 6.0.0
  83. * @product highcharts
  84. * @apioption plotOptions.bullet.targetOptions.color
  85. */
  86. /**
  87. * The border width of the rectangle representing the target.
  88. *
  89. * In styled mode, use class `highcharts-bullet-target` instead.
  90. *
  91. * @since 6.0.0
  92. */
  93. borderWidth: 0
  94. },
  95. tooltip: {
  96. pointFormat: '<span style="color:{series.color}">\u25CF</span>' +
  97. ' {series.name}: <b>{point.y}</b>. Target: <b>{point.target}' +
  98. '</b><br/>'
  99. }
  100. }, {
  101. pointArrayMap: ['y', 'target'],
  102. parallelArrays: ['x', 'y', 'target'],
  103. /**
  104. * Draws the targets. For inverted chart, the `series.group` is rotated, so
  105. * the same coordinates apply. This method is based on column series
  106. * drawPoints function.
  107. *
  108. * @ignore
  109. * @function Highcharts.Series#drawPoints
  110. */
  111. drawPoints: function () {
  112. var series = this,
  113. chart = series.chart,
  114. options = series.options,
  115. animationLimit = options.animationLimit || 250;
  116. columnProto.drawPoints.apply(this);
  117. series.points.forEach(function (point) {
  118. var pointOptions = point.options,
  119. shapeArgs,
  120. targetGraphic = point.targetGraphic,
  121. targetShapeArgs,
  122. targetVal = point.target,
  123. pointVal = point.y,
  124. width,
  125. height,
  126. targetOptions,
  127. y;
  128. if (isNumber(targetVal) && targetVal !== null) {
  129. targetOptions = H.merge(
  130. options.targetOptions,
  131. pointOptions.targetOptions
  132. );
  133. height = targetOptions.height;
  134. shapeArgs = point.shapeArgs;
  135. width = relativeLength(
  136. targetOptions.width,
  137. shapeArgs.width
  138. );
  139. y = series.yAxis.translate(
  140. targetVal,
  141. false,
  142. true,
  143. false,
  144. true
  145. ) - targetOptions.height / 2 - 0.5;
  146. targetShapeArgs = series.crispCol.apply({
  147. // Use fake series object to set borderWidth of target
  148. chart: chart,
  149. borderWidth: targetOptions.borderWidth,
  150. options: {
  151. crisp: options.crisp
  152. }
  153. }, [
  154. shapeArgs.x + shapeArgs.width / 2 - width / 2,
  155. y,
  156. width,
  157. height
  158. ]);
  159. if (targetGraphic) {
  160. // Update
  161. targetGraphic[
  162. chart.pointCount < animationLimit ?
  163. 'animate' :
  164. 'attr'
  165. ](targetShapeArgs);
  166. // Add or remove tooltip reference
  167. if (isNumber(pointVal) && pointVal !== null) {
  168. targetGraphic.element.point = point;
  169. } else {
  170. targetGraphic.element.point = undefined;
  171. }
  172. } else {
  173. point.targetGraphic = targetGraphic = chart.renderer
  174. .rect()
  175. .attr(targetShapeArgs)
  176. .add(series.group);
  177. }
  178. // Presentational
  179. if (!chart.styledMode) {
  180. targetGraphic.attr({
  181. fill: pick(
  182. targetOptions.color,
  183. pointOptions.color,
  184. (series.zones.length && (point.getZone.call({
  185. series: series,
  186. x: point.x,
  187. y: targetVal,
  188. options: {}
  189. }).color || series.color)) || undefined,
  190. point.color,
  191. series.color
  192. ),
  193. stroke: pick(
  194. targetOptions.borderColor,
  195. point.borderColor,
  196. series.options.borderColor
  197. ),
  198. 'stroke-width': targetOptions.borderWidth
  199. });
  200. }
  201. // Add tooltip reference
  202. if (isNumber(pointVal) && pointVal !== null) {
  203. targetGraphic.element.point = point;
  204. }
  205. targetGraphic.addClass(point.getClassName() +
  206. ' highcharts-bullet-target', true);
  207. } else if (targetGraphic) {
  208. point.targetGraphic = targetGraphic.destroy(); // #1269
  209. }
  210. });
  211. },
  212. /**
  213. * Includes target values to extend extremes from y values.
  214. *
  215. * @ignore
  216. * @function Highcharts.Series#getExtremes
  217. */
  218. getExtremes: function (yData) {
  219. var series = this,
  220. targetData = series.targetData,
  221. yMax,
  222. yMin;
  223. columnProto.getExtremes.call(this, yData);
  224. if (targetData && targetData.length) {
  225. yMax = series.dataMax;
  226. yMin = series.dataMin;
  227. columnProto.getExtremes.call(this, targetData);
  228. series.dataMax = Math.max(series.dataMax, yMax);
  229. series.dataMin = Math.min(series.dataMin, yMin);
  230. }
  231. }
  232. },
  233. /** @lends Highcharts.seriesTypes.ohlc.prototype.pointClass.prototype */
  234. {
  235. /**
  236. * Destroys target graphic.
  237. *
  238. * @private
  239. * @function
  240. */
  241. destroy: function () {
  242. if (this.targetGraphic) {
  243. this.targetGraphic = this.targetGraphic.destroy();
  244. }
  245. columnProto.pointClass.prototype.destroy.apply(this, arguments);
  246. }
  247. });
  248. /**
  249. * A `bullet` series. If the [type](#series.bullet.type) option is not
  250. * specified, it is inherited from [chart.type](#chart.type).
  251. *
  252. * @extends series,plotOptions.bullet
  253. * @since 6.0.0
  254. * @product highcharts
  255. * @excluding dataParser, dataURL, marker
  256. * @apioption series.bullet
  257. */
  258. /**
  259. * An array of data points for the series. For the `bullet` series type,
  260. * points can be given in the following ways:
  261. *
  262. * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
  263. * to `x,y,target`. If the first value is a string, it is applied as the name
  264. * of the point, and the `x` value is inferred. The `x` value can also be
  265. * omitted, in which case the inner arrays should be of length 2\. Then the
  266. * `x` value is automatically calculated, either starting at 0 and
  267. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  268. * series options.
  269. * ```js
  270. * data: [
  271. * [0, 40, 75],
  272. * [1, 50, 50],
  273. * [2, 60, 40]
  274. * ]
  275. * ```
  276. *
  277. * 2. An array of objects with named values. The following snippet shows only a
  278. * few settings, see the complete options set below. If the total number of
  279. * data points exceeds the series'
  280. * [turboThreshold](#series.bullet.turboThreshold), this option is not
  281. * available.
  282. * ```js
  283. * data: [{
  284. * x: 0,
  285. * y: 40,
  286. * target: 75,
  287. * name: "Point1",
  288. * color: "#00FF00"
  289. * }, {
  290. * x: 1,
  291. * y: 60,
  292. * target: 40,
  293. * name: "Point2",
  294. * color: "#FF00FF"
  295. * }]
  296. * ```
  297. *
  298. * @type {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
  299. * @extends series.column.data
  300. * @since 6.0.0
  301. * @product highcharts
  302. * @apioption series.bullet.data
  303. */
  304. /**
  305. * The target value of a point.
  306. *
  307. * @type {number}
  308. * @since 6.0.0
  309. * @product highcharts
  310. * @apioption series.bullet.data.target
  311. */
  312. /**
  313. * Individual target options for each point.
  314. *
  315. * @extends plotOptions.bullet.targetOptions
  316. * @product highcharts
  317. * @apioption series.bullet.data.targetOptions
  318. */
  319. /**
  320. * @product highcharts
  321. * @excluding halo, lineWidth, lineWidthPlus, marker
  322. * @apioption series.bullet.states.hover
  323. */
  324. /**
  325. * @product highcharts
  326. * @excluding halo, lineWidth, lineWidthPlus, marker
  327. * @apioption series.bullet.states.select
  328. */