zballina / yii2-chartjs-widget
为 Yii2 的 ChartJs 组件
2.1.2
2017-07-26 09:24 UTC
Requires
- bower-asset/chartjs: ~2.6
- yiisoft/yii2: ~2.0.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ~4.4
This package is auto-updated.
Last update: 2024-09-05 18:44:01 UTC
README
渲染 ChartJs 插件 组件
安装
安装此扩展的首选方式是通过 composer。这需要 composer-asset-plugin,它也是 yii2 的依赖之一——所以如果你已经安装了 yii2,那么你很可能已经设置好了。
运行以下命令之一:
composer require 2amigos/yii2-chartjs-widget:~2.0
或
"2amigos/yii2-chartjs-widget" : "~2.0"
将以下内容添加到应用程序的 composer.json
文件的 require 部分。
使用方法
支持以下类型
- 折线图
- 条形图
- 雷达图
- 极坐标图
- 饼图
- 环形图
- 气泡图
- 散点图
- 面积图
- 混合图
以下示例使用 Line
类型的图表。请参阅 ChartJs 插件 文档了解插件支持的不同类型。
use dosamigos\chartjs\ChartJs;
<?= ChartJs::widget([
'type' => 'line',
'options' => [
'height' => 400,
'width' => 400
],
'data' => [
'labels' => ["January", "February", "March", "April", "May", "June", "July"],
'datasets' => [
[
'label' => "My First dataset",
'backgroundColor' => "rgba(179,181,198,0.2)",
'borderColor' => "rgba(179,181,198,1)",
'pointBackgroundColor' => "rgba(179,181,198,1)",
'pointBorderColor' => "#fff",
'pointHoverBackgroundColor' => "#fff",
'pointHoverBorderColor' => "rgba(179,181,198,1)",
'data' => [65, 59, 90, 81, 56, 55, 40]
],
[
'label' => "My Second dataset",
'backgroundColor' => "rgba(255,99,132,0.2)",
'borderColor' => "rgba(255,99,132,1)",
'pointBackgroundColor' => "rgba(255,99,132,1)",
'pointBorderColor' => "#fff",
'pointHoverBackgroundColor' => "#fff",
'pointHoverBorderColor' => "rgba(255,99,132,1)",
'data' => [28, 48, 40, 19, 96, 27, 100]
]
]
]
]);
?>
插件使用示例(在饼图中显示百分比)
echo ChartJs::widget([
'type' => 'pie',
'id' => 'structurePie',
'options' => [
'height' => 200,
'width' => 400,
],
'data' => [
'radius' => "90%",
'labels' => ['Label 1', 'Label 2', 'Label 3'], // Your labels
'datasets' => [
[
'data' => ['35.6', '17.5', '46.9'], // Your dataset
'label' => '',
'backgroundColor' => [
'#ADC3FF',
'#FF9A9A',
'rgba(190, 124, 145, 0.8)'
],
'borderColor' => [
'#fff',
'#fff',
'#fff'
],
'borderWidth' => 1,
'hoverBorderColor'=>["#999","#999","#999"],
]
]
],
'clientOptions' => [
'legend' => [
'display' => false,
'position' => 'bottom',
'labels' => [
'fontSize' => 14,
'fontColor' => "#425062",
]
],
'tooltips' => [
'enabled' => true,
'intersect' => true
],
'hover' => [
'mode' => false
],
'maintainAspectRatio' => false,
],
'plugins' =>
new \yii\web\JsExpression('
[{
afterDatasetsDraw: function(chart, easing) {
var ctx = chart.ctx;
chart.data.datasets.forEach(function (dataset, i) {
var meta = chart.getDatasetMeta(i);
if (!meta.hidden) {
meta.data.forEach(function(element, index) {
// Draw the text in black, with the specified font
ctx.fillStyle = 'rgb(0, 0, 0)';
var fontSize = 16;
var fontStyle = 'normal';
var fontFamily = 'Helvetica';
ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
// Just naively convert to string for now
var dataString = dataset.data[index].toString()+'%';
// Make sure alignment settings are correct
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var padding = 5;
var position = element.tooltipPosition();
ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
});
}
});
}
}]')
])
更多信息
ChartJs 有许多配置选项。有关更多信息,请访问 ChartJs 插件 网站。
贡献
请参阅 CONTRIBUTING 以获取详细信息。
致谢
许可证
BSD 许可证(BSD)。有关更多信息,请参阅 许可证文件。
定制软件 | 网络与移动软件开发
www.2amigos.us