legend-symbol.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. import H from '../parts/Globals.js';
  11. import U from '../parts/Utilities.js';
  12. var merge = U.merge, pick = U.pick;
  13. /* eslint-disable valid-jsdoc */
  14. /**
  15. * Legend symbol mixin.
  16. *
  17. * @private
  18. * @mixin Highcharts.LegendSymbolMixin
  19. */
  20. H.LegendSymbolMixin = {
  21. /**
  22. * Get the series' symbol in the legend
  23. *
  24. * @private
  25. * @function Highcharts.LegendSymbolMixin.drawRectangle
  26. *
  27. * @param {Highcharts.Legend} legend
  28. * The legend object
  29. *
  30. * @param {Highcharts.Point|Highcharts.Series} item
  31. * The series (this) or point
  32. */
  33. drawRectangle: function (legend, item) {
  34. var options = legend.options, symbolHeight = legend.symbolHeight, square = options.squareSymbol, symbolWidth = square ? symbolHeight : legend.symbolWidth;
  35. item.legendSymbol = this.chart.renderer.rect(square ? (legend.symbolWidth - symbolHeight) / 2 : 0, legend.baseline - symbolHeight + 1, // #3988
  36. symbolWidth, symbolHeight, pick(legend.options.symbolRadius, symbolHeight / 2))
  37. .addClass('highcharts-point')
  38. .attr({
  39. zIndex: 3
  40. }).add(item.legendGroup);
  41. },
  42. /**
  43. * Get the series' symbol in the legend. This method should be overridable
  44. * to create custom symbols through
  45. * Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  46. *
  47. * @private
  48. * @function Highcharts.LegendSymbolMixin.drawLineMarker
  49. *
  50. * @param {Highcharts.Legend} legend
  51. * The legend object.
  52. */
  53. drawLineMarker: function (legend) {
  54. var options = this.options, markerOptions = options.marker, radius, legendSymbol, symbolWidth = legend.symbolWidth, symbolHeight = legend.symbolHeight, generalRadius = symbolHeight / 2, renderer = this.chart.renderer, legendItemGroup = this.legendGroup, verticalCenter = legend.baseline -
  55. Math.round(legend.fontMetrics.b * 0.3), attr = {};
  56. // Draw the line
  57. if (!this.chart.styledMode) {
  58. attr = {
  59. 'stroke-width': options.lineWidth || 0
  60. };
  61. if (options.dashStyle) {
  62. attr.dashstyle = options.dashStyle;
  63. }
  64. }
  65. this.legendLine = renderer
  66. .path([
  67. 'M',
  68. 0,
  69. verticalCenter,
  70. 'L',
  71. symbolWidth,
  72. verticalCenter
  73. ])
  74. .addClass('highcharts-graph')
  75. .attr(attr)
  76. .add(legendItemGroup);
  77. // Draw the marker
  78. if (markerOptions && markerOptions.enabled !== false && symbolWidth) {
  79. // Do not allow the marker to be larger than the symbolHeight
  80. radius = Math.min(pick(markerOptions.radius, generalRadius), generalRadius);
  81. // Restrict symbol markers size
  82. if (this.symbol.indexOf('url') === 0) {
  83. markerOptions = merge(markerOptions, {
  84. width: symbolHeight,
  85. height: symbolHeight
  86. });
  87. radius = 0;
  88. }
  89. this.legendSymbol = legendSymbol = renderer.symbol(this.symbol, (symbolWidth / 2) - radius, verticalCenter - radius, 2 * radius, 2 * radius, markerOptions)
  90. .addClass('highcharts-point')
  91. .add(legendItemGroup);
  92. legendSymbol.isMarker = true;
  93. }
  94. }
  95. };
  96. export default H.LegendSymbolMixin;