Line.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Dcat\Admin\Widgets\Chart;
  3. /**
  4. * @see https://www.chartjs.org/docs/latest/charts/line.html
  5. */
  6. class Line extends Chart
  7. {
  8. use ScaleSetting;
  9. public $colors = [
  10. 'rgba(64,153,222, 1)', // primary
  11. 'rgba(38,166,154, 1)', // tear
  12. 'rgba(121,134,203, 1)', // purple
  13. 'rgba(33,185,120, 1)', // success
  14. 'rgba(249,144,55, 1)', // orange
  15. 'rgba(41,126,192, 1)', // primary darker
  16. 'rgba(245,87,59, 1)', // red
  17. 'rgba(24,103,192, 1)', // blue
  18. 'rgba(242,203,34, 1)', // yellow
  19. 'rgba(143,193,93, 1)', // green
  20. 'rgba(89,169,248, 1)', // custom
  21. 'rgba(129,199,132, 1)', // silver
  22. 'rgba(228,113,222, 1)', // lime
  23. ];
  24. public $backgroundColors = [
  25. 'rgba(64,153,222, 0.1)', // primary
  26. 'rgba(38,166,154, 0.1)', // tear
  27. 'rgba(121,134,203, 0.2)', // purple
  28. 'rgba(33,185,120, 0.12)', // success
  29. 'rgba(249,144,55, 0.1)', // orange
  30. 'rgba(41,126,192, 0.1)', // primary darker
  31. 'rgba(245,87,59, 0.1)', // red
  32. 'rgba(24,103,192, 0.1)', // blue
  33. 'rgba(242,203,34, 0.1)', // yellow
  34. 'rgba(143,193,93, 0.1)', // green
  35. 'rgba(89,169,248, 0.1)', // custom
  36. 'rgba(129,199,132, 0.1)', // silver
  37. 'rgba(228,113,222, 0.1)', // lime
  38. ];
  39. protected $fillBackground = false;
  40. protected $opaque = false;
  41. public function fillBackground(bool $opaque = false)
  42. {
  43. $this->fillBackground = true;
  44. $this->opaque = $opaque;
  45. return $this;
  46. }
  47. protected function fillColor(array $colors = [])
  48. {
  49. $colors = $colors ?: $this->colors;
  50. $bgColors = $this->opaque ? $colors : $this->backgroundColors;
  51. foreach ($this->data['datasets'] as &$item) {
  52. if (empty($item['strokeColor'])) {
  53. $color = array_shift($colors);
  54. $bgColor = array_shift($bgColors);
  55. $item['borderColor'] = $color;
  56. $item['backgroundColor'] = $this->fillBackground ? $bgColor : 'rgba(255, 255, 255, 0.1)';
  57. }
  58. }
  59. }
  60. public function showLines(bool $val)
  61. {
  62. return $this->options(['showLines' => $val]);
  63. }
  64. public function spanGaps(bool $val)
  65. {
  66. return $this->options(['spanGaps' => $val]);
  67. }
  68. }