|
@@ -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(),
|
|
|
+ ];
|
|
|
}
|
|
|
}
|