ghunti/highcharts-php
Composer 安装命令:
composer require ghunti/highcharts-php
包简介
A php wrapper for highcharts and highstock javascript libraries
README 文档
README
HighchartsPHP is a PHP library that works as a wrapper for the Highchart js library (http://www.highcharts.com) and it was built with flexibility and maintainability in mind. It isn't a simple port of the JavaScript library to PHP, it was designed in a way that mimics the JavaScript counterpart API, so that the developer only needs to learn one API.
The companion webpage can be found at https://goncaloqueiros.net/highcharts.php
Setup
The recommended way to install HighchartsPHP is through Composer. Just create a composer.json file and run the php composer.phar install command to install it:
{
"require": {
"ghunti/highcharts-php": "^4"
}
}
Current package version supports PHP >= 8.0. For compatibility with older PHP versions, use the 3.x tag.
Usage
Simple
You can create a highchart or highstock chart using one of the three js engine available (jQuery, mootools, and prototype), using the Highchart constructor.
//This will create a highchart chart with the jquery js engine $chart = new Highchart();
//To create a highstock chart with the jquery js engine $stockChart = new Highchart(Highchart::HIGHSTOCK);
//Create a highchart chart with the mootools js engine $chartWithMootools = new Highchart(null, Highchart::ENGINE_MOOTOOLS);
Now that there's a valid $chart object the developer only needs to add elements to it as if it was writing them in JavaScript.
$chart->title = array('text' => 'Monthly Average Temperature', 'x' => -20); or $chart->title->text = 'Monthly Average Temperature'; $chart->title->x = -20;
You can also create simple arrays
$chart->series[] = array('name' => 'Tokyo', 'data' => array(7.0, 6.9, 9.5)); or $chart->series[0] = array('name' => 'Tokyo', 'data' => array(7.0, 6.9, 9.5)); or $chart->series[0]->name = 'Tokyo'; $chart->series[0]->data = array(7.0, 6.9, 9.5);
Render
To get all the script necessary to render your chart you can use the printScripts() method:
$chart->printScripts();
Or if you don't want to directly echo the scripts and rather the function to return the script string:
$chart->printScripts(true);
And finally to render the chart object use the render() method:
echo $chart->render("chart");
The first (optional) argument passed to render method is the var name to be used by JavaScript and the second (optional) argument is the callback to be passed to the Highcharts.Chart method. The third and last (optional) argument flags that you want your script already wrapped around HTML script tags.
Its also possible to render the chart options only by calling the renderOptions() method. Useful for times where the chart is used inside a $.getJson call for example
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=range.json&callback=?', function(data) { $('#container').highcharts(<?php echo $chart->renderOptions(); ?>)});
Javascript expressions
If one of the chart options must be a JavaScript expression, you can't assign a simple string to it, otherwise it will be printed as a simple JavaScript string also. For that you must use the special HighchartJsExpr object:
$chart->tooltip->formatter = new HighchartJsExpr("function() { return '' + this.series.name + this.x + ': ' + this.y + '°C'; }" );
Empty javascript object {}
If you wish to render an empty javascript object {}, just assign the variable you want with new stdClass()
Extra scripts
To manually include a script for render use the addExtraScript function:
$chart->addExtraScript('export', 'http://code.highcharts.com/modules/', 'exporting.js');
To include an extra script use the key that's on the config file or that was given manually via addExtraScript
$chart->includeExtraScripts(array('export'));
To include more than one script just add it to the array
$chart->includeExtraScripts(array('export', 'highcharts-more'));
If no arguments are passed, it will include all the extra scripts
$chart->includeExtraScripts();
If you want to add any extra script to the default config file, feel free to open a PR. Here is the list of the current extra scripts available:
Use new Highcharts 3.0 charts
Highcharts 3.0 introduced a new set of charts that require an additional javascript file highcharts-more.js.
To include this extra script you need to call the includeExtraScripts method with the highcharts-more key.
$chart = new Highchart(); $chart->includeExtraScripts(array('highcharts-more'));
Render only some options
If you need to render a small portion of options, you can use the HighchartOptionRenderer::render($options) method.
A good example of this can be found at clock demo
$backgroundOptions = new HighchartOption(); $backgroundOptions->radialGradient = array( 'cx' => 0.5, 'cy' => -0.4, 'r' => 1.9 ); ... $chart->pane->background[] = array( new stdClass(), array('backgroundColor' => new HighchartJsExpr('Highcharts.svg ? ' . HighchartOptionRenderer::render($backgroundOptions) . ' : null') ) );
This way it is possible to include option rendering inside a javascript expression
Set up a general configuration
There are cases where a configuration is not created only for a chart, but for all the charts on the page (lang and global) are examples of this.
To set a general option, just create a new HighchartOption (not chart) and send it to Highchart::setOptions() method.
The Highchart::setOptions() must be placed before the chart render
$option = new HighchartOption(); $option->global->useUTC = false; echo Highchart::setOptions($option);
Themes
Theme creation follows the same process for a general option.
You create a new HighchartOption object, use it has if it was a chart and then call Highchart::setOptions() method.
$theme = new HighchartOption(); //Code your theme as if this was a chart $theme->colors = array('#058DC7', '#50B432', '#ED561B'); ... echo Highchart::setOptions($theme);
Configuration
By default HighchartsPHP library comes with configurations to work out of the box. If you wish to change the path of any js library loaded, have a look at src/config.php.
In case you need to change some of this values you should use the setConfigurations method:
$chart = new Highchart(); $chart->setConfigurations( array( 'jQuery' => array( 'name' => 'anotherName' ) ) );
Demos
All the Highcharts and Highstocks live demos present on http://www.highcharts.com under the demo gallery were reproduced using this library and you can find them on the demos folder or see a live example on https://goncaloqueiros.net/highcharts/demos.php
Tests
You can run the unit tests with the following command:
$ cd path/to/HighchartsPHP/
$ composer install
$ vendor/bin/phpunit
License
The HighchartsPHP package is open-sourced software licensed under the MIT license.
ghunti/highcharts-php 适用场景与选型建议
ghunti/highcharts-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.33M 次下载、GitHub Stars 达 363, 最近一次更新时间为 2014 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「javascript」 「charts」 「highcharts」 「highstock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ghunti/highcharts-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ghunti/highcharts-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ghunti/highcharts-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
The "View Counter" bundle
A Versatile and Expandable jQuery Plotting Plugin
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
PHP library for c3js
统计信息
- 总下载量: 2.33M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 376
- 点击次数: 23
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2014-02-16