ScaleSetting.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Dcat\Admin\Widgets\Chart;
  3. trait ScaleSetting
  4. {
  5. /**
  6. * @see https://www.chartjs.org/docs/latest/axes/
  7. *
  8. * @param array $opts
  9. *
  10. * @return $this
  11. */
  12. public function scales(array $opts)
  13. {
  14. if (! isset($this->options['scales'])) {
  15. $this->options['scales'] = [];
  16. }
  17. $this->options['scales'] = array_merge($this->options['scales'], $opts);
  18. return $this;
  19. }
  20. /**
  21. * @param string|null $label
  22. *
  23. * @return $this
  24. */
  25. public function displayScaleLabelOnX(?string $label)
  26. {
  27. return $this->xAxes([
  28. [
  29. 'scaleLabel' => [
  30. 'display' => true,
  31. 'labelString' => $label,
  32. ],
  33. ],
  34. ]);
  35. }
  36. /**
  37. * @param string|null $label
  38. *
  39. * @return $this
  40. */
  41. public function displayScaleLabelOnY(?string $label)
  42. {
  43. return $this->yAxes([
  44. [
  45. 'scaleLabel' => [
  46. 'display' => true,
  47. 'labelString' => $label,
  48. ],
  49. ],
  50. ]);
  51. }
  52. /**
  53. * @param array $opts
  54. *
  55. * @return $this
  56. */
  57. public function yAxes(array $opts)
  58. {
  59. return $this->scales(['yAxes' => $opts]);
  60. }
  61. /**
  62. * @param array $opts
  63. *
  64. * @return $this
  65. */
  66. public function xAxes(array $opts)
  67. {
  68. return $this->scales(['xAxes' => $opts]);
  69. }
  70. /**
  71. * @see https://www.chartjs.org/docs/latest/axes/radial/linear.html
  72. *
  73. * @param array $opts
  74. *
  75. * @return $this
  76. */
  77. public function scale(array $opts)
  78. {
  79. if (! isset($this->options['scale'])) {
  80. $this->options['scale'] = [];
  81. }
  82. $this->options['scale'] = array_merge($this->options['scale'], $opts);
  83. return $this;
  84. }
  85. }