jtraulle/cake-charts

此包已被弃用且不再维护。未建议替代包。

CakePHP 的 CakeCharts 插件

安装: 610

依赖项: 0

建议者: 0

安全: 0

星星: 3

关注者: 2

分支: 2

开放问题: 0

类型:cakephp-plugin

v1.1.2 2017-10-23 17:45 UTC

This package is auto-updated.

Last update: 2022-03-01 00:10:06 UTC


README

Scrutinizer Code Quality Latest Stable Version Total Downloads License

目的

此插件是 plotly.js 图表库的 CakePHP 封装。它提供了一个简单的方式来在 CakePHP 应用程序中绘制图表,您可以使用提供的 DrawChart 助手函数来绘制图表,而无需编写 JavaScript。

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require jtraulle/cake-charts

1. 加载插件

确保在您的 config/bootstrap.php 中加载了 CakeCharts 插件

<?php
Plugin::load('CakeCharts', ['autoload' => true]);

2. 加载助手函数

将此行添加到您的 src/View/AppView.php 中的 initialize() 函数中

<?php
$this->loadHelper('CakeCharts.DrawChart');

3. 在默认布局中添加视图块

在您的 src/Template/Layout/default.ctp 中在关闭 </body> 标签之前添加这些行

<?= $this->fetch('cakeChartsLibrary');
    $this->fetch('cakeChartsDefinition'); ?>
  • cakeChartsLibrary 视图块将从 Plot.ly CDN 注入 plotly-latest.min.js JavaScript 文件
  • cakeChartsDefinition 视图块将包含生成的 plotly.js 图表

用法

从任何模板中

  • 单序列函数签名和 phpDoc
<?php
/**
 * Function structure for single series charts
 *
 * @param array $x Values to be placed on X axis
 * @param array $y Values to be placed on Y axis
 * @param string $mode For line charts only.
 *                     Can be either "markers", "lines" or "markers+line"
 * @param array $layout Any layout option accepted by Plotly.js
 * @see https://plot.ly/javascript/#layout-options for possible values
 * @param array $configuration Any configuration option accepted by Plotly.js
 * @see https://plot.ly/javascript/configuration-options for possible values and examples
 * @param string|null $id HTML identifier of div where chart will be drawed
 * @return string The generated chart
 */
$this->DrawChart->simpleBarChart(array $x, array $y, array $layout = [], array $configuration = [], string $id = null);
$this->DrawChart->pieChart(array $x, array $y, array $layout = [], array $configuration = [], string $id = null);
$this->DrawChart->singleLineChart(array $x, array $y, string $mode, array $layout = [], array $configuration = [], string $id = null);
  • 多序列函数签名和 phpDoc
<?php
/**
 * Function structure for multi series charts
 *
 * @param array $series Multi-dimensional array of series
 *    $series = [
 *       [
 *          (array) X values,
 *          (array) Y values,
 *          (opt. string) Name of the series,
 *          (opt. string) Line type ("markers", "lines" or "markers+line")
 *       ], [...], [...]
 *    ]
 * @param array $layout Any layout option accepted by Plotly.js
 * @see https://plot.ly/javascript/#layout-options for possible values
 * @param array $configuration Any configuration option accepted by Plotly.js
 * @see https://plot.ly/javascript/configuration-options for possible values and examples
 * @param string|null $id HTML identifier of div where chart will be drawed
 * @return string The generated chart
 */
$this->DrawChart->stackedBarChart(array $series, array $layout = [], array $configuration = [], string $id = null);
$this->DrawChart->groupedBarChart(array $series, array $layout = [], array $configuration = [], string $id = null);
$this->DrawChart->multilineChart(array $series, array $layout = [], array $configuration = [], string $id = null);

// $series multi-dimensional array example
$series = [
  [
    ['Apples', 'Tomatoes', 'Bananas', 'Cherries'],
    [38, 22, 55, 96],
    'Ripes',
    'markers'
  ],
  [
    ['Apples', 'Tomatoes', 'Bananas', 'Cherries'],
    [11, 15, 22, 10]
    'Unripes',
    'markers'
  ]
];

条形图示例

因为我们都喜欢示例!

简单的条形图

<?= $this->DrawChart->simpleBarChart(
    ['Apples', 'Tomatoes', 'Bananas', 'Cherries'],
    [38, 22, 55, 96]
); ?>

Simple bar chart

堆叠条形图

<?php

$fruits = ['Apples', 'Tomatoes', 'Bananas', 'Cherries'];
$ripes = [38, 22, 55, 96];
$unripes = [11, 15, 22, 10];
$series = [[$fruits, $ripes, 'Ripes'], [$fruits, $unripes, 'Unripes']];

echo $this->DrawChart->stackedBarChart($series);

Stacked bar chart

分组条形图

<?= $this->DrawChart->groupedBarChart($series); ?>

Grouped bar chart

饼图

<?= $this->DrawChart->pieChart(
    ['Apples', 'Tomatoes', 'Bananas', 'Cherries'],
    [38, 22, 55, 96]
); ?>

Pie chart

折线图

单线图

<?= $this->DrawChart->singleLineChart(
    ['January', 'February', 'March', 'April'],
    [1025, 1451, 1526, 2563],
    'lines'
); ?>

Single line chart

多线图

<?php

$months = ['January', 'February', 'March', 'April'];
$appleSold = [1025, 1451, 1526, 2563];
$tomatoesSold = [1542, 2325, 2515, 3609];
$bananasSold = [1242, 2695, 2875, 3219];
$cherriesSold = [1322, 1825, 2615, 4109];

echo $this->DrawChart->multilineChart([
    [$months, $appleSold, 'Apples sold', 'markers'],
    [$months, $tomatoesSold, 'Tomatoes sold', 'lines'],
    [$months, $bananasSold, 'Bananas sold'],
    [$months, $cherriesSold, 'Cherries sold']
]);

Multi lines chart

错误管理

如果您犯了错误,CakeCharts 会明确告诉您问题所在,这样您就可以快速修复它。

Error example