定制 opensaucesystems/chartwire 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

MethodArgumentsDescription
addDataToDatasetstring $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
reactiveKeyreturns a string based on its data
setOptionsarray $options chart.js optionsThis 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
MethodArgumentsDescription
withLegendShow legend
withoutLegendHide legend
legendPositionTopSet legend position to top
legendPositionLeftSet legend position to left
legendPositionRightSet legend position to right
legendPositionBottomSet legend position to bottom
legendHorizontallyAlignedLeftAlign legend horizontal left
legendHorizontallyAlignedCenterAlign legend horizontal center
legendHorizontallyAlignedRightAlign legend horizontal right


Axis methods

The following chart types support these methods:

  • Line Chart
  • Bar Chart
  • Doughnut Chart
  • Pie Chart
  • Area Chart
MethodArgumentsDescription
withoutXAxisHide x-axis
withXAxisShow x-axis
withoutYAxisHide y-axis
withYAxisShow y-axis


Title methods

The following chart types support these methods:

  • Line Chart
  • Bar Chart
  • Doughnut Chart
  • Pie Chart
  • Area Chart
MethodArgumentsDescription
withTitleShow title
withoutTitleHide title
setTitleTextstring $textSet chart title
titlePositionTopSet title position to top
titlePositionLeftSet title position to left
titlePositionRightSet title position to right
titlePositionBottomSet title position to bottom


Event methods

The following chart types support these methods:

  • Line Chart
  • Bar Chart
  • Doughnut Chart
  • Pie Chart
  • Area Chart
MethodArgumentsDescription
withOnClickEventNamestring $onClickEventNameLivewire 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
MethodArgumentsDescription
isStackedbool $stacked = trueStacked 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 opensaucesystems/chartwire 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 71
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-08