jqh 5 years ago
parent
commit
8c7f0c808e

+ 6 - 0
resources/views/widgets/metrics/card.blade.php

@@ -16,6 +16,12 @@
             <div class="metric-header">{!! $header !!}</div>
         </div>
 
+        @if (! empty($options['subTitle']))
+            <span class="btn btn-sm bg-light shadow-0 p-0">
+                {{ $options['subTitle'] }}
+            </span>
+        @endif
+
         @if(! empty($options['dropdown']))
         <div class="dropdown chart-dropdown">
             <button class="btn btn-sm btn-light shadow-0 dropdown-toggle p-0 waves-effect" data-toggle="dropdown">

+ 1 - 14
src/Traits/InteractsWithApi.php

@@ -64,7 +64,7 @@ trait InteractsWithApi
      *
      * @return $this
      */
-    public function from(string $method, string $url, array $query = [])
+    public function request(string $method, string $url, array $query = [])
     {
         $this->method = $method;
         $this->url = admin_url(Helper::urlWithQuery($url, $query));
@@ -72,19 +72,6 @@ trait InteractsWithApi
         return $this;
     }
 
-    /**
-     * 请求当前地址.
-     *
-     * @param array $query
-     * @param string $method
-     *
-     * @return $this
-     */
-    public function fromCurrent(array $query = [], string $method = 'GET')
-    {
-        return $this->from($method, request()->fullUrlWithQuery($query));
-    }
-
     /**
      * 获取请求地址
      *

+ 15 - 0
src/Widgets/Metrics/Card.php

@@ -25,6 +25,7 @@ class Card extends Widget
     protected $options = [
         'icon'     => null,
         'title'    => null,
+        'subTitle' => null,
         'header'   => null,
         'content'  => null,
         'dropdown' => [],
@@ -124,6 +125,20 @@ class Card extends Widget
         return $this;
     }
 
+    /**
+     * 设置卡片子标题.
+     *
+     * @param string $title
+     *
+     * @return $this
+     */
+    public function subTitle(?string $title)
+    {
+        $this->options['subTitle'] = $title;
+
+        return $this;
+    }
+
     /**
      * 设置卡片头内容.
      *

+ 1 - 1
src/Widgets/Metrics/DonutChartCard.php

@@ -9,7 +9,7 @@ class DonutChartCard extends Card
     /**
      * @var int
      */
-    protected $chartHeight = 140;
+    protected $chartHeight = 135;
 
     /**
      * 内容宽度.

+ 2 - 2
src/Widgets/Metrics/SingleRoundChartCard.php

@@ -22,8 +22,8 @@ class SingleRoundChartCard extends RoundChartCard
         $color = Admin::color();
 
         $colors = [$color->success()];
-        $gradientToColors = ['#00b5b5'];
-        $strokColor = '#b9c3cd';
+        $gradientToColors = [$color->tear1()];
+        $strokColor = $color->gray();
 
         return [
             'chart' => [

+ 30 - 53
src/Widgets/Sparkline/Sparkline.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Widgets\Sparkline;
 
 use Dcat\Admin\Admin;
+use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\InteractsWithApi;
 use Dcat\Admin\Widgets\Widget;
 use Illuminate\Contracts\Support\Arrayable;
@@ -24,8 +25,12 @@ class Sparkline extends Widget
 {
     use InteractsWithApi;
 
-    public static $js = '@jquery.sparkline';
-    public static $css = '@jquery.sparkline';
+    public static $js = [
+        '@jquery.sparkline',
+    ];
+    public static $css = [
+        '@jquery.sparkline',
+    ];
 
     protected static $optionMethods = [
         'highlightSpotColor',
@@ -69,11 +74,10 @@ class Sparkline extends Widget
         'disableHiddenCheck',
     ];
 
+    protected $id;
+
     protected $type = 'line';
 
-    /**
-     * @var array
-     */
     protected $options = ['width' => '100%'];
 
     protected $values = [];
@@ -88,7 +92,7 @@ class Sparkline extends Widget
     }
 
     /**
-     * Get or set the sparkline values.
+     * 设置图表值.
      *
      * @param mixed|null $values
      *
@@ -100,19 +104,13 @@ class Sparkline extends Widget
             return $this->values;
         }
 
-        if (is_string($values)) {
-            $values = explode(',', $values);
-        } elseif ($values instanceof Arrayable) {
-            $values = $values->toArray();
-        }
-
-        $this->values = $values;
+        $this->values = Helper::array($values);
 
         return $this;
     }
 
     /**
-     * Set width of sparkline.
+     * 设置图表宽度.
      *
      * @param int $width
      *
@@ -126,7 +124,7 @@ class Sparkline extends Widget
     }
 
     /**
-     * Set height of sparkline.
+     * 设置图表高度.
      *
      * @param int $width
      *
@@ -142,7 +140,7 @@ class Sparkline extends Widget
     }
 
     /**
-     * Composite the given sparkline.
+     * 组合图表.
      *
      * @param int $width
      *
@@ -156,10 +154,6 @@ class Sparkline extends Widget
     }
 
     /**
-     * Setup scripts.
-     *
-     * @param int $width
-     *
      * @return string
      */
     protected function script()
@@ -169,7 +163,7 @@ class Sparkline extends Widget
 
         if (! $this->allowBuildRequest()) {
             return <<<JS
-$('#{$this->id}').sparkline($values, $options);
+$('#{$this->getId()}').sparkline($values, $options);
 {$this->buildCombinationScript()};
 JS;
         }
@@ -179,7 +173,7 @@ JS;
 if (!response.status) {
     return Dcat.error(response.message || 'Server internal error.');
 }        
-var id = '{$this->id}', opt = $options;
+var id = '{$this->getId()}', opt = $options;
 opt = $.extend(opt, response.options || {});
 $('#'+id).sparkline(response.values || $values, opt);
 JS
@@ -200,7 +194,7 @@ JS
             $options = json_encode($value[1]);
 
             $script .= <<<JS
-$('#{$this->id}').sparkline($value, $options);
+$('#{$this->getId()}').sparkline($value, $options);
 JS;
         }
 
@@ -212,12 +206,10 @@ JS;
      */
     public function render()
     {
-        $this->makeId();
-
         Admin::script($this->script());
 
         $this->setHtmlAttribute([
-            'id' => $this->id,
+            'id' => $this->getId(),
         ]);
 
         $this->collectAssets();
@@ -228,15 +220,13 @@ HTML;
     }
 
     /**
-     * Get element id.
+     * 获取容器元素ID.
      *
      * @return string
      */
     public function getId()
     {
-        $this->makeId();
-
-        return $this->id;
+        return $this->id ?: ($this->id = $this->generateId());
     }
 
     /**
@@ -255,35 +245,22 @@ HTML;
     }
 
     /**
-     * Make element id.
-     *
-     * @return void
+     * @return string
      */
-    protected function makeId()
+    protected function generateId()
     {
-        if ($this->id) {
-            return;
-        }
-        $this->id = 'sparkline_'.$this->type.Str::random(8);
+        return 'sparkline-'.$this->type.Str::random(8);
     }
 
     /**
-     * Return JsonResponse instance.
-     *
-     * @param bool  $returnOptions
-     * @param array $data
-     *
-     * @return \Illuminate\Http\JsonResponse
+     * @return array
      */
-    public function toJsonResponse(bool $returnOptions = true, array $data = [])
+    public function valueResult()
     {
-        return response()->json(array_merge(
-            [
-                'status'  => 1,
-                'values'  => $this->values(),
-                'options' => $returnOptions ? $this->getOptions() : [],
-            ],
-            $data
-        ));
+        return [
+            'status'  => 1,
+            'values'  => $this->values(),
+            'options' => $this->getOptions(),
+        ];
     }
 }