Sessions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace UCore\DcatAdmin\Metrics\Examples;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\Metrics\Bar;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 图标卡片:大号数值 + 趋势 + 柱状图
  8. *
  9. */
  10. class Sessions extends Bar
  11. {
  12. /**
  13. * 初始化卡片内容
  14. */
  15. protected function init()
  16. {
  17. parent::init();
  18. $color = Admin::color();
  19. $dark35 = $color->dark35();
  20. // 卡片内容宽度
  21. $this->contentWidth(5, 7);
  22. // 标题
  23. $this->title('平均在线');
  24. // 设置下拉选项
  25. $this->dropdown([
  26. '7' => 'Last 7 Days',
  27. '28' => 'Last 28 Days',
  28. '30' => 'Last Month',
  29. '365' => 'Last Year',
  30. ]);
  31. // 设置图表颜色
  32. $this->chartColors([
  33. $dark35,
  34. $dark35,
  35. $color->primary(),
  36. $dark35,
  37. $dark35,
  38. $dark35
  39. ]);
  40. }
  41. /**
  42. * 处理请求
  43. *
  44. * @param Request $request
  45. *
  46. * @return mixed|void
  47. */
  48. public function handle(Request $request)
  49. {
  50. switch ($request->get('option')) {
  51. case '7':
  52. default:
  53. // 卡片内容,数值,目前是伪数据,实际应读取数据
  54. $this->withContent('2000', '+5.2%');
  55. // 图表数据
  56. $this->withChart([
  57. [
  58. 'name' => 'Sessions',
  59. 'data' => [75, 125, 225, 175, 125, 75, 25],
  60. ],
  61. ]);
  62. }
  63. }
  64. /**
  65. * 设置图表数据.
  66. *
  67. * @param array $data
  68. *
  69. * @return $this
  70. */
  71. public function withChart(array $data)
  72. {
  73. return $this->chart([
  74. 'series' => $data,
  75. ]);
  76. }
  77. /**
  78. * 设置卡片内容.
  79. *
  80. * @param string $title 主统计
  81. * @param string $value 次级统计
  82. * @param string $style 次级统计样式
  83. *
  84. * @return $this
  85. */
  86. public function withContent($title, $value, $style = 'success')
  87. {
  88. // 根据选项显示
  89. $label = strtolower(
  90. $this->dropdown[request()->option] ?? 'last 7 days'
  91. );
  92. $minHeight = '183px';
  93. return $this->content(
  94. <<<HTML
  95. <div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
  96. <div class="text-left">
  97. <h1 class="font-lg-2 mt-2 mb-0">{$title}</h1>
  98. <h5 class="font-medium-2" style="margin-top: 10px;">
  99. <span class="text-{$style}">{$value} </span>
  100. <span>vs {$label}</span>
  101. </h5>
  102. </div>
  103. <a href="#" class="btn btn-primary shadow waves-effect waves-light">View Details <i class="feather icon-chevrons-right"></i></a>
  104. </div>
  105. HTML
  106. );
  107. }
  108. }