opensaucesystems/chartwire
Composer 安装命令:
composer require opensaucesystems/chartwire
包简介
Charts.js for Livewire
关键字:
README 文档
README
Chart.wire are simple charts for use with Laravel Livewire.
Requirements ^
This package requires the following packages/libraries:
Installation ^
You can install the package via composer:
composer require opensaucesystems/chartwire
Usage ^
Chart.wire supports out of the box the following types of charts
- Line Chart
- Bar Chart
- Doughnut Chart
- Pie Chart
- Area Chart
- Sparkline Chart
Each one comes with its own "model" class that allows you to define the chart's data and render behavior.
- Line Chart uses LineChartModel
- Bar Chart uses BarChartModel
- Doughnut Chart uses DoughnutChartModel
- Pie Chart uses PieChartModel
- Area Chart uses AreaChartModel
- Sparkline Chart uses SparklineChartModel
For example, to render a line chart, we create an instance of LineChartModel and pass it to the Livewire Line Chart component:
$colors = new \Opensaucesystems\Chartwire\Values\ColorValue([
'background' => 'rgba(54, 162, 235, 0.6)',
'border' => 'rgba(54, 162, 235, 0.6)',
'pointBackground' => 'rgba(54, 162, 235, 0.6)',
]);
$lineChartModel = (new \Opensaucesystems\Chartwire\Models\LineChartModel())
->addDataToDataset('Populations 2020', 1405544000, 'China', $colors)
->addDataToDataset('Populations 2020', 1370279686, 'India', $colors)
->addDataToDataset('Populations 2020', 330736378, 'United States', $colors)
->addDataToDataset('Populations 2020', 269603400, 'Indonesia', $colors)
->addDataToDataset('Populations 2020', 220892331, 'Pakistan', $colors)
->addDataToDataset('Populations 2020', 212404659, 'Brazil', $colors)
->addDataToDataset('Populations 2020', 206139587, 'Nigeria', $colors);
<livewire:line-chartwire :line-chart-model="$lineChartModel" />
Chart Model Methods ^
Common methods
These methods are common to all chart models
| Method | Arguments | Description |
|---|---|---|
| addDataToDataset | string $datasetName name of the dataset string|int|float $value data value string $label data label Opensaucesystems\Chartwire\Values\ColorValue $color color of data on chart null|string $type chart type array $extras extra chart options | Add data to chart dataset |
| reactiveKey | returns a string based on its data | |
| setOptions | array $options chart.js options | This will override any of the options set by the model methods as well as the default options. See Chart.js docs |
Legend methods
The following chart types support these methods:
- Line Chart
- Bar Chart
- Doughnut Chart
- Pie Chart
- Area Chart
| Method | Arguments | Description |
|---|---|---|
| withLegend | Show legend | |
| withoutLegend | Hide legend | |
| legendPositionTop | Set legend position to top | |
| legendPositionLeft | Set legend position to left | |
| legendPositionRight | Set legend position to right | |
| legendPositionBottom | Set legend position to bottom | |
| legendHorizontallyAlignedLeft | Align legend horizontal left | |
| legendHorizontallyAlignedCenter | Align legend horizontal center | |
| legendHorizontallyAlignedRight | Align legend horizontal right |
Axis methods
The following chart types support these methods:
- Line Chart
- Bar Chart
- Doughnut Chart
- Pie Chart
- Area Chart
| Method | Arguments | Description |
|---|---|---|
| withoutXAxis | Hide x-axis | |
| withXAxis | Show x-axis | |
| withoutYAxis | Hide y-axis | |
| withYAxis | Show y-axis |
Title methods
The following chart types support these methods:
- Line Chart
- Bar Chart
- Doughnut Chart
- Pie Chart
- Area Chart
| Method | Arguments | Description |
|---|---|---|
| withTitle | Show title | |
| withoutTitle | Hide title | |
| setTitleText | string $text | Set chart title |
| titlePositionTop | Set title position to top | |
| titlePositionLeft | Set title position to left | |
| titlePositionRight | Set title position to right | |
| titlePositionBottom | Set title position to bottom |
Event methods
The following chart types support these methods:
- Line Chart
- Bar Chart
- Doughnut Chart
- Pie Chart
- Area Chart
| Method | Arguments | Description |
|---|---|---|
| withOnClickEventName | string $onClickEventName | Livewire event name that will be fired when chart data is clicked |
When an event of type mouseup or click is triggered on the chart, then Livewire will emit an event with the name of the argument passed into this method. It will also pass the label and value that was clicked.
$lineChartModel = (new \Opensaucesystems\Chartwire\Models\LineChartModel())
->withOnClickEventName('onMyLineChartClicked');
Now you may register this event in another components $listeners property:
class ShowData extends Component
{
public $label;
public $value;
protected $listeners = ['onMyLineChartClicked' => 'handleMyLineChartClicked'];
public function handleMyLineChartClicked(array $data)
{
$this->label = $data['label'];
$this->value = $data['value'];
}
}
Stacked methods
The following chart types support these methods:
- Line Chart
- Bar Chart
- Area Chart
| Method | Arguments | Description |
|---|---|---|
| isStacked | bool $stacked = true | Stacked charts can be used to show how one data is made up of a number of smaller pieces. |
Sparkline charts ^
Sparkline charts are just a normal Line Chart with the default options hiding the legend, title, axes, tooltips etc.
To set the height and width of the Sparline Chart just use CSS.
This examples use TailwindCSS:
$colors = new \Opensaucesystems\Chartwire\Values\ColorValue([
'background' => 'rgba(54, 162, 235, 0.6)',
'border' => 'rgba(54, 162, 235, 0.6)',
])
$sparklineChartModel = (new \Opensaucesystems\Chartwire\Models\SparklineChartModel())
->addDataToDataset('Populations 2020', 1405544000, 'China', $colors)
->addDataToDataset('Populations 2020', 1370279686, 'India', $colors)
->addDataToDataset('Populations 2020', 330736378, 'United States', $colors)
->addDataToDataset('Populations 2020', 269603400, 'Indonesia', $colors)
->addDataToDataset('Populations 2020', 220892331, 'Pakistan', $colors)
->addDataToDataset('Populations 2020', 212404659, 'Brazil', $colors)
->addDataToDataset('Populations 2020', 206139587, 'Nigeria', $colors);
<div class="h-10 w-20">
<livewire:sparkline-chartwire :sparkline-chart-model="$sparklineChartModel" />
</div>
Colors ^
Each dataset requires a ColorValue object.
The ColorValue instance requires a border color. The background and pointBackground is optional.
The color can be simple a hexadecimal, RGB, or their color names.
$colors = new \Opensaucesystems\Chartwire\Values\ColorValue([
'border' => 'rgba(54, 162, 235, 0.6)',
])
Since Chart.js supports a CanvasGradient object, the ColorValue supports a LinearGradientValue object that is converted into a JS CanvasGradient object.
use Opensaucesystems\Chartwire\Values\ColorStopValue;
use Opensaucesystems\Chartwire\Values\LinearGradientValue;
$backgroundColorStops = [
new ColorStopValue(['color' => 'rgba(54, 162, 235, 0.6)', 'offset' => 0]),
new ColorStopValue(['color' => 'rgba(54, 162, 235, 0)', 'offset' => 1]),
];
$borderColorStops = [
new ColorStopValue(['color' => '#80b6f4', 'offset' => 0]),
new ColorStopValue(['color' => '#f49080', 'offset' => 1]),
];
$backgroundGradient = new LinearGradientValue([
'x1' => 0,
'y1' => 100,
'x2' => 0,
'y2' => 370,
'colorStops' => $backgroundColorStops,
])
$borderGradient = new LinearGradientValue([
'x1' => 500,
'y1' => 0,
'x2' => 100,
'y2' => 0,
'colorStops' => $borderColorStops,
]);
Multiple Datasets ^
You can also add multiple datasets to a chart that will allow you to group data.
Stacked
Stacked charts allows you to see that data is made up of a number of smaller pieces.
$colors2010 = new \Opensaucesystems\Chartwire\Values\ColorValue([
'background' => 'rgba(54, 162, 235, 0.6)',
'border' => 'rgba(54, 162, 235, 1)',
]);
$colors2020 = new \Opensaucesystems\Chartwire\Values\ColorValue([
'background' => 'rgba(37, 194, 160, 0.6)',
'border' => 'rgba(37, 194, 160, 1)',
]);
$barChartModel = (new \Opensaucesystems\Chartwire\Models\BarChartModel())
->addDataToDataset('Populations 2010', 1339724852, 'China', $colors2010)
->addDataToDataset('Populations 2010', 1182105564, 'India', $colors2010)
->addDataToDataset('Populations 2010', 309349689, 'United States', $colors2010)
->addDataToDataset('Populations 2010', 237641326, 'Indonesia', $colors2010)
->addDataToDataset('Populations 2010', 173510000, 'Pakistan', $colors2010)
->addDataToDataset('Populations 2010', 193252604, 'Brazil', $colors2010)
->addDataToDataset('Populations 2010', 158258917, 'Nigeria', $colors2010)
->addDataToDataset('Populations 2020', 1405544000, 'China', $colors2020)
->addDataToDataset('Populations 2020', 1370279686, 'India', $colors2020)
->addDataToDataset('Populations 2020', 330736378, 'United States', $colors2020)
->addDataToDataset('Populations 2020', 269603400, 'Indonesia', $colors2020)
->addDataToDataset('Populations 2020', 220892331, 'Pakistan', $colors2020)
->addDataToDataset('Populations 2020', 212404659, 'Brazil', $colors2020)
->addDataToDataset('Populations 2020', 206139587, 'Nigeria', $colors2020);
<livewire:bar-chartwire :bar-chart-model="$barChartModel" />
Troubleshooting ^
Chart components must be placed inside a container with fixed height. This is because the chart will use all the given vertical space. A fixed height is needed to render properly.
<div style="height: 32rem;">
<livewire:livewire-column-chart .../>
</div>
>Note: if a fixed height is not given, the chart will grow vertically infinitely. That's not what we want, right?
Testing ^
composer test
Changelog ^
Please see CHANGELOG for more information what has changed recently.
Contributing ^
Please see CONTRIBUTING for details.
Security ^
If you discover any security related issues, please email ashley@opensauce.systems instead of using the issue tracker.
Credits and Influences ^
License ^
The MIT License (MIT). Please see License File for more information.
opensaucesystems/chartwire 适用场景与选型建议
opensaucesystems/chartwire 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 71 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「charts」 「livewire」 「chartwire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 opensaucesystems/chartwire 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 opensaucesystems/chartwire 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 opensaucesystems/chartwire 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The "View Counter" bundle
A Versatile and Expandable jQuery Plotting Plugin
PHP library for c3js
Draw charts with a simple API on Laravel
Livewire starter kit for Lunar e-commerce.
Laravel Service Provider for morris.js
统计信息
- 总下载量: 71
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-08