Statistical data cards

Dcat Admin has a variety of commonly used statistical cards built in, which can be easily interacted with the back-end API, the following is an introduction to the Usage.

Basic cards

The base card (Dcat\Admin\Widgets\Metrics\Card) is a card that does not display charts by default and is the simplest of the data cards.

Statistical data cards - 图1

Simple Example

The use of the basic cards can be found in the built-in App\Admin\Metrics\Examples\TotalUsers category.

  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use Dcat\Admin\Widgets\Metrics\Card;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Http\Request;
  6. class TotalUsers extends Card
  7. {
  8. /**
  9. * Card footer content.
  10. *
  11. * @var string|Renderable|\Closure
  12. */
  13. protected $footer;
  14. // Save custom parameters
  15. protected $data = [];
  16. // The method parameters must be set to default values
  17. public function __construct(array $data = [])
  18. {
  19. $this->data = [];
  20. parent::__construct();
  21. }
  22. protected function init()
  23. {
  24. parent::init();
  25. // Setting the TITLE
  26. $this->title('Total Users');
  27. // Setting the drop-down menu
  28. $this->dropdown([
  29. '7' => 'Last 7 Days',
  30. '28' => 'Last 28 Days',
  31. '30' => 'Last Month',
  32. '365' => 'Last Year',
  33. ]);
  34. }
  35. /**
  36. * Processing of requests.
  37. *
  38. * @param Request $request
  39. *
  40. * @return void
  41. */
  42. public function handle(Request $request)
  43. {
  44. // Get custom parameters passed externally
  45. $key1 = $request->get('key1');
  46. switch ($request->get('option')) {
  47. case '365':
  48. $this->content(mt_rand(600, 1500));
  49. $this->down(mt_rand(1, 30));
  50. break;
  51. case '30':
  52. $this->content(mt_rand(170, 250));
  53. $this->up(mt_rand(12, 50));
  54. break;
  55. case '28':
  56. $this->content(mt_rand(155, 200));
  57. $this->up(mt_rand(5, 50));
  58. break;
  59. case '7':
  60. default:
  61. $this->content(143);
  62. $this->up(15);
  63. }
  64. }
  65. // Passing a custom parameter to the handle method
  66. public function parameters() : array
  67. {
  68. return $this->data;
  69. }
  70. /**
  71. * @param int $percent
  72. *
  73. * @return $this
  74. */
  75. public function up($percent)
  76. {
  77. return $this->footer(
  78. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% Increase"
  79. );
  80. }
  81. /**
  82. * @param int $percent
  83. *
  84. * @return $this
  85. */
  86. public function down($percent)
  87. {
  88. return $this->footer(
  89. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% Decrease"
  90. );
  91. }
  92. /**
  93. * Set the content of the footer of the card
  94. *
  95. * @param string|Renderable|\Closure $footer
  96. *
  97. * @return $this
  98. */
  99. public function footer($footer)
  100. {
  101. $this->footer = $footer;
  102. return $this;
  103. }
  104. /**
  105. * Render the card content.
  106. *
  107. * @return string
  108. */
  109. public function renderContent()
  110. {
  111. $content = parent::renderContent();
  112. return <<<HTML
  113. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  114. <h2 class="ml-1 font-large-1">{$content}</h2>
  115. </div>
  116. <div class="ml-1 mt-1 font-weight-bold text-80">
  117. {$this->renderFooter()}
  118. </div>
  119. HTML;
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function renderFooter()
  125. {
  126. return $this->toString($this->footer);
  127. }
  128. }

Basic card method

initialization (init)

The init method is called in the card constructor method and can be used for card initialization operations.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. // Your initialization operation
  7. }
  8. }

set TITLE (title)

The title method allows you to set a TITLE in the top left corner of the card.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->title('active user');
  7. }
  8. }

Set dropdown menu (dropdown)

The dropdown method allows you to set a dropdown menu button in the upper right corner of the card, which needs to be used in combination with the handle method to have result.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. $this->dropdown([
  6. '7' => 'Last 7 Days',
  7. '28' => 'Last 28 Days',
  8. '30' => 'Last Month',
  9. '365' => 'Last Year',
  10. ]);
  11. }
  12. }

set subTitle (subTitle)

The subTitle method allows you to set a subTitle in the upper corner of the card.

{tip} This method will conflict with the dropdown method, so if the dropdown button is set, there is no need to set a secondary TITLE.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->subTitle('Last 30 days');
  7. }
  8. }

Set header content (header)

The header method is used to set the content of the card header. This method accepts one parameter, which can be string, Closure, or a template view (Illuminate\Contracts\Support\Renderable).

{tip} The content set in this way is in the same div container as title.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->header(
  7. <<<HTML
  8. <div>Header content</div>
  9. HTML
  10. );
  11. // You can also pass a closure
  12. $this->header(function () {
  13. return ...;
  14. });
  15. // You can also pass views
  16. $this->header(view('...'));
  17. }
  18. }

Set the main content (content)

The content method accepts one parameter, which can be string, Closure, or template view (Illuminate\Contracts\Support\Renderable).

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->withContent('Custom Content');
  7. }
  8. /**
  9. * Card Content
  10. *
  11. * @param string $content
  12. *
  13. * @return $this
  14. */
  15. public function withContent($content)
  16. {
  17. return $this->content(
  18. <<<HTML
  19. <div class="d-flex flex-column flex-wrap text-center">
  20. <h1 class="font-large-2 mt-2 mb-0">{$content}</h1>
  21. <small>Tickets</small>
  22. </div>
  23. HTML
  24. );
  25. }
  26. }

Setting height (height)

Through the height method, you can set the minimum height of the card, the default is 165.

  1. protected function init()
  2. {
  3. parent::init();
  4. $this->height(200);
  5. }

Passing custom parameters (parameters)

Parameters can be passed to the handle method via the parameters method.

  1. // Passing a custom parameter to the handle method
  2. public function parameters() : array
  3. {
  4. return [
  5. 'key1' => 'value1',
  6. ...
  7. ];
  8. }

Get custom parameters

  1. public function handle(Request $request)
  2. {
  3. // Get custom parameters
  4. $key1 = $request->get('key1');
  5. }

Rendering content (renderContent)

To ensure flexibility and scalability of the content, the system does not have any preset styles for the card content (i.e., just display whatever content is set, with no preset layouts or other styles). The renderContent method allows you to change the default rendering of a card.

The following example demonstrates the main functionality of the renderContent method.

  1. use Dcat\Admin\Support\Helper;
  2. class MyCard extend Card
  3. {
  4. protected $footer;
  5. protected function init()
  6. {
  7. parent::init();
  8. // Set the card content
  9. $this->content(...);
  10. // Set the content at the bottom of the card
  11. $this->footer(...);
  12. }
  13. /**
  14. * Add this method to set the content at the bottom of the card
  15. *
  16. * @return $this
  17. */
  18. public function footer($footer)
  19. {
  20. $this->footer = $footer;
  21. return $this;
  22. }
  23. /**
  24. * Rendering bottom content
  25. *
  26. * @return $this
  27. */
  28. public function renderFooter()
  29. {
  30. return Helper::render($this->footer);
  31. }
  32. /**
  33. * Render the card content
  34. * You can add the bottom of the card here
  35. *
  36. * @return string
  37. */
  38. public function renderContent()
  39. {
  40. $content = parent::renderContent();
  41. $footer = $this->renderFooter();
  42. return <<<HTML
  43. <div class="card-content">
  44. <div class="row">
  45. {$content}
  46. </div>
  47. <div class="metric-footer">
  48. {$footer}
  49. </div>
  50. </div>
  51. HTML;
  52. }
  53. }

Enable and render charts.

Basic cards have charting enabled by default, you can enable charting by using the useChart method, which instantiates a chart class and saves it in the chart property.

When the chart is enabled, you need to render the chart in your card content, otherwise the chart will be initialized, but still can not be displayed.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. // Enable Charts
  7. $this->useChart();
  8. }
  9. /**
  10. * Render the card content
  11. * You need to add the code to render the chart here
  12. *
  13. * @return string
  14. */
  15. public function renderContent()
  16. {
  17. // Content set by the content method
  18. $content = parent::renderContent();
  19. // Rendering Charts
  20. $chart = $this->renderChart();
  21. return <<<HTML
  22. <div class="my-chart">{$chart}</div>
  23. {$content}
  24. HTML
  25. }
  26. }

Chart Default Configuration (defaultChartOptions)

The default chart configuration can be set by the defaultChartOptions method, which is available only when the chart is enabled.

{tip} The chart configuration here also supports setting executable JS code, please refer to chart configuration setting executable JS code for detailed usage.

  1. class MyCard extend Card
  2. {
  3. protected function defaultChartOptions()
  4. {
  5. // Return the configuration of the chart
  6. return [
  7. ...
  8. ];
  9. }
  10. }

Set up a chart (chart)

The chart method allows you to set the chart configuration.

{tip} The chart configuration here also supports setting executable JS code, please refer to chart configuration setting executable JS code for detailed usage.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chart([...]);
  7. }
  8. }

Setting the chart configuration (chartOption)

Through the chartOption method, you can set the chart configuration, this method can only set one parameter at a time, supports setting multidimensional parameters.

{tip} The chart configuration here also supports setting executable JS code, please refer to chart configuration setting executable JS code for detailed usage.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chartOption('stroke.curve', 'smooth');
  7. $this->chartOption(
  8. 'plotOptions.radialBar.dataLabels.total.formatter',
  9. // The return code of this value will be executed as JS code
  10. JavaScript::make("function () { return {$number}; }")
  11. );
  12. }
  13. }

Set Chart Height (chartHeight)

Through the chartHeight method can set the height of the chart, this method is very important, often need to be used in conjunction with the height method to adjust the overall height of the card.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chartHeight(150);
  7. }
  8. }

Set the spacing on the chart (chartMarginTop)

The distance between the chart and the parent element can be set by the chartMarginTop method, which accepts a parameter of type int and can pass a negative number.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chartMarginTop(-10);
  7. }
  8. }

Set the bottom spacing of the chart (chartMarginBottom)

The chartMarginBottom method is used to set the distance between the chart and the lower elements, the method accepts an int type parameter and can pass a negative number.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chartMarginBottom(10);
  7. }
  8. }

Set chart labels (chartLabels)

The chartLabels method allows you to set the label (labels) configuration of the chart.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chartLabels('Label 1');
  7. // You can also pass arrays
  8. $this->chartLabels(['Label 1']);
  9. }
  10. }

Set Chart Color (chartColors)

The chartColors method allows you to set the color (colors) configuration of the chart.

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. $this->chartColors('#4f41a1');
  7. // You can also pass arrays
  8. $this->chartColors(['#4f41a1']);
  9. }
  10. }

Rendering Charts (renderChart)

The renderChart method is used to render the chart

  1. class MyCard extend Card
  2. {
  3. protected function init()
  4. {
  5. parent::init();
  6. // Enable Charts
  7. $this->useChart();
  8. }
  9. /**
  10. * Render the card content
  11. * You need to add the code to render the chart here
  12. *
  13. * @return string
  14. */
  15. public function renderContent()
  16. {
  17. // Content set by the content method
  18. $content = parent::renderContent();
  19. // Rendering Charts
  20. $chart = $this->renderChart();
  21. return <<<HTML
  22. <div class="my-chart">{$chart}</div>
  23. {$content}
  24. HTML
  25. }
  26. }

Linear trend chart cards (Line)

The Linear Trend (Dcat\Admin/Widgets\Metrics\Line) is a statistical card with a polyline/line graph, inherited from the base card Dcat\Admin/Widgets\Metrics\Card.

Statistical data cards - 图2

Example

Reference may be made to the built-in App\Admin\Metrics\Examples\NewUsers category.

  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use Dcat\Admin\Widgets\Metrics\Line;
  4. use Illuminate\Http\Request;
  5. class NewUsers extends Line
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $label = 'New Users';
  11. /**
  12. * Initialize card content
  13. *
  14. * @return void
  15. */
  16. protected function init()
  17. {
  18. parent::init();
  19. $this->title($this->label);
  20. $this->dropdown([
  21. '7' => 'Last 7 Days',
  22. '28' => 'Last 28 Days',
  23. '30' => 'Last Month',
  24. '365' => 'Last Year',
  25. ]);
  26. }
  27. /**
  28. * Processing of requests
  29. *
  30. * @param Request $request
  31. *
  32. * @return mixed|void
  33. */
  34. public function handle(Request $request)
  35. {
  36. $generator = function ($len, $min = 10, $max = 300) {
  37. for ($i = 0; $i <= $len; $i++) {
  38. yield mt_rand($min, $max);
  39. }
  40. };
  41. switch ($request->get('option')) {
  42. case '365':
  43. // Card Content
  44. $this->withContent(mt_rand(1000, 5000).'k');
  45. // Chart data
  46. $this->withChart(collect($generator(30))->toArray());
  47. // 直线
  48. break;
  49. case '30':
  50. // Card Content
  51. $this->withContent(mt_rand(400, 1000).'k');
  52. // Chart data
  53. $this->withChart(collect($generator(30))->toArray());
  54. // 直线
  55. break;
  56. case '28':
  57. // Card Content
  58. $this->withContent(mt_rand(400, 1000).'k');
  59. // Chart data
  60. $this->withChart(collect($generator(28))->toArray());
  61. // 直线
  62. break;
  63. case '7':
  64. default:
  65. // Card Content
  66. $this->withContent('89.2k');
  67. // Chart data
  68. $this->withChart([28, 40, 36, 52, 38, 60, 55,]);
  69. }
  70. }
  71. /**
  72. * Setting up chart data.
  73. *
  74. * @param array $data
  75. *
  76. * @return $this
  77. */
  78. public function withChart(array $data)
  79. {
  80. return $this->chart([
  81. 'series' => [
  82. [
  83. 'name' => $this->label,
  84. 'data' => $data,
  85. ],
  86. ],
  87. ]);
  88. }
  89. /**
  90. * Set the card content.
  91. *
  92. * @param string $content
  93. *
  94. * @return $this
  95. */
  96. public function withContent($content)
  97. {
  98. return $this->content(
  99. <<<HTML
  100. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  101. <h2 class="ml-1 font-large-1">{$content}</h2>
  102. <span class="mb-0 mr-1 text-80">{$this->label}</span>
  103. </div>
  104. HTML
  105. );
  106. }
  107. }

Methods

Set lines to straight (chartStraight)

  1. use Dcat\Admin\Widgets\Metrics\Line;
  2. class MyCard extend Line
  3. {
  4. protected function init()
  5. {
  6. parent::init();
  7. $this->chartStraight();
  8. }
  9. }

Setting Lines to Curves (chartSmooth)

The default display is the curve

  1. use Dcat\Admin\Widgets\Metrics\Line;
  2. class MyCard extend Line
  3. {
  4. protected function init()
  5. {
  6. parent::init();
  7. $this->chartSmooth();
  8. }
  9. }

doughnut card (Donut)

The Doughnut Card (Dcat\AdminWidgets\Metrics\Donut) is a statistical card with a doughnut chart, inherited from the base card Dcat\AdminWidgets\Metrics\Card.

Statistical data cards - 图3

Example

Reference may be made to the built-in App\Admin\Metrics\Examples\NewDevices category.

  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\Metrics\Donut;
  5. class NewDevices extends Donut
  6. {
  7. protected $labels = ['Desktop', 'Mobile'];
  8. /**
  9. * Initialize card content
  10. */
  11. protected function init()
  12. {
  13. parent::init();
  14. $color = Admin::color();
  15. $colors = [$color->primary(), $color->alpha('blue2', 0.5)];
  16. $this->title('New Devices');
  17. $this->subTitle('Last 30 days');
  18. $this->chartLabels($this->labels);
  19. // Set Chart Color
  20. $this->chartColors($colors);
  21. }
  22. /**
  23. * Rendering template
  24. *
  25. * @return string
  26. */
  27. public function render()
  28. {
  29. $this->fill();
  30. return parent::render();
  31. }
  32. /**
  33. * Writing data
  34. *
  35. * @return void
  36. */
  37. public function fill()
  38. {
  39. $this->withContent(44.9, 28.6);
  40. // Chart data
  41. $this->withChart([44.9, 28.6]);
  42. }
  43. /**
  44. * Setting chart data
  45. *
  46. * @param array $data
  47. *
  48. * @return $this
  49. */
  50. public function withChart(array $data)
  51. {
  52. return $this->chart([
  53. 'series' => $data
  54. ]);
  55. }
  56. /**
  57. * Set the card header content
  58. *
  59. * @param mixed $desktop
  60. * @param mixed $mobile
  61. *
  62. * @return $this
  63. */
  64. protected function withContent($desktop, $mobile)
  65. {
  66. $blue = Admin::color()->alpha('blue2', 0.5);
  67. $style = 'margin-bottom: 8px';
  68. $labelWidth = 120;
  69. return $this->content(
  70. <<<HTML
  71. <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
  72. <div style="width: {$labelWidth}px">
  73. <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
  74. </div>
  75. <div>{$desktop}</div>
  76. </div>
  77. <div class="d-flex pl-1 pr-1" style="{$style}">
  78. <div style="width: {$labelWidth}px">
  79. <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
  80. </div>
  81. <div>{$mobile}</div>
  82. </div>
  83. HTML
  84. );
  85. }
  86. }

Methods

set content width (contentWidth)

Through the contentWidth method, you can set the width of the text content and the chart content, the default is [6, 6].

{tip} The width here is a value between 1-12.

  1. use Dcat\Admin\Widgets\Metrics\Line;
  2. class MyCard extend Line
  3. {
  4. protected function init()
  5. {
  6. parent::init();
  7. $this->contentWidth(4, 8);
  8. }
  9. }

Bar Chart Cards (Bar)

The Bar Chart Card (Dcat\Admin\Widgets\MetricsBar) is a statistical card with a bar chart, inherited from the base card Dcat\Admin\Widgets\MetricsCard.

Statistical data cards - 图4

Example

Reference may be made to the built-in App\Admin\Metrics\Examples\NewDevices category.

  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\Metrics\Bar;
  5. use Illuminate\Http\Request;
  6. class Sessions extends Bar
  7. {
  8. /**
  9. * Initialize card content
  10. */
  11. public function init()
  12. {
  13. parent::init();
  14. $color = Admin::color();
  15. $dark35 = $color->dark35();
  16. // Card content width
  17. $this->contentWidth(5, 7);
  18. // TITLE
  19. $this->title('Avg Sessions');
  20. // Set drop-down options
  21. $this->dropdown([
  22. '7' => 'Last 7 Days',
  23. '28' => 'Last 28 Days',
  24. '30' => 'Last Month',
  25. '365' => 'Last Year',
  26. ]);
  27. // Set Chart Color
  28. $this->chartColors([
  29. $dark35,
  30. $dark35,
  31. $color->primary(),
  32. $dark35,
  33. $dark35,
  34. $dark35
  35. ]);
  36. }
  37. /**
  38. * Processing of requests
  39. *
  40. * @param Request $request
  41. *
  42. * @return mixed|void
  43. */
  44. public function handle(Request $request)
  45. {
  46. switch ($request->get('option')) {
  47. case '7':
  48. default:
  49. // Card Content
  50. $this->withContent('2.7k', '+5.2%');
  51. // Chart data
  52. $this->withChart([
  53. [
  54. 'name' => 'Sessions',
  55. 'data' => [75, 125, 225, 175, 125, 75, 25],
  56. ],
  57. ]);
  58. }
  59. }
  60. /**
  61. * Setting up chart data.
  62. *
  63. * @param array $data
  64. *
  65. * @return $this
  66. */
  67. public function withChart(array $data)
  68. {
  69. return $this->chart([
  70. 'series' => $data,
  71. ]);
  72. }
  73. /**
  74. * Set the card content.
  75. *
  76. * @param string $title
  77. * @param string $value
  78. * @param string $style
  79. *
  80. * @return $this
  81. */
  82. public function withContent($title, $value, $style = 'success')
  83. {
  84. // Display according to options
  85. $label = strtolower(
  86. $this->dropdown[request()->option] ?? 'last 7 days'
  87. );
  88. $minHeight = '183px';
  89. return $this->content(
  90. <<<HTML
  91. <div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
  92. <div class="text-left">
  93. <h1 class="font-large-2 mt-2 mb-0">{$title}</h1>
  94. <h5 class="font-medium-2" style="margin-top: 10px;">
  95. <span class="text-{$style}">{$value} </span>
  96. <span>vs {$label}</span>
  97. </h5>
  98. </div>
  99. <a href="#" class="btn btn-primary shadow waves-effect waves-light">View Details <i class="feather icon-chevrons-right"></i></a>
  100. </div>
  101. HTML
  102. );
  103. }
  104. }

Circle Charts (Round)

The Round Chart Card (Dcat\Admin/Widgets\Metrics\Round) is a statistical card with multiple rings, inherited from the base card Dcat\Admin/Widgets\Metrics\Card.

Statistical data cards - 图5

Example

The specific Example is similar to that of the above card, which can be referred to in the built-in App\Admin\Metrics\Examples\ProductOrders category, which will not be posted here.

More built-in cards

The system also has built-in cards such as Dcat\Admin\Widgets\Metrics\RadialBar, Dcat\Admin\Widgets\Metrics\SingleRound, etc. Since the Usages are similar to the above-mentioned cards, the code will not be repeated here.

[Click me to see an online demo of all the built-in cards] (http://103.39.211.179:8080/admin/components/metric-cards)

Custom chart cards

For custom cards, refer to the code for the built-in chart above.