miloschuman / yii2-highcharts

Highcharts widget for Yii 2 Framework
http://www.yiiframework.com/extension/yii2-highcharts-widget/
MIT License
166 stars 63 forks source link

Yii2 Highcharts Widget

Latest Stable Version Total Downloads Stars Scrutinizer Code Quality License

Easily add Highcharts, Highstock and Highmaps graphs to your Yii2 application.

Screen Shot

About

Highcharts

Create interactive charts easily for your web projects. Used by tens of thousands of developers and 59 out of the world's 100 largest companies, Highcharts is the simplest yet most flexible charting API on the market.

Highstock

Highstock lets you create stock or general timeline charts in pure JavaScript. Including sophisticated navigation options like a small navigator series, preset date ranges, date picker, scrolling and panning.

Highmaps

Build interactive maps to display sales, election results or any other information linked to geography. Perfect for standalone use or in dashboards in combination with Highcharts!

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require miloschuman/yii2-highcharts-widget

or add

"miloschuman/yii2-highcharts-widget": "^8.0"

to the require section of your composer.json file.

Usage

Preferred Method (using PHP arrays)

To use this widget, insert the following code into a view file:

use miloschuman\highcharts\Highcharts;

echo Highcharts::widget([
   'options' => [
      'title' => ['text' => 'Fruit Consumption'],
      'xAxis' => [
         'categories' => ['Apples', 'Bananas', 'Oranges']
      ],
      'yAxis' => [
         'title' => ['text' => 'Fruit eaten']
      ],
      'series' => [
         ['name' => 'Jane', 'data' => [1, 0, 4]],
         ['name' => 'John', 'data' => [5, 7, 3]]
      ]
   ]
]);

By configuring the options property, you can specify the options that need to be passed to the Highcharts JavaScript object. Please refer to the demo gallery and documentation on the Highcharts website for possible options.

See /doc/examples for more usage examples.

Alternative Method (using JSON string)

Alternatively, you can use a valid JSON string in place of an associative array to specify options:

use miloschuman\highcharts\Highcharts;

echo Highcharts::widget([
   'options'=>'{
      "title": { "text": "Fruit Consumption" },
      "xAxis": {
         "categories": ["Apples", "Bananas", "Oranges"]
      },
      "yAxis": {
         "title": { "text": "Fruit eaten" }
      },
      "series": [
         { "name": "Jane", "data": [1, 0, 4] },
         { "name": "John", "data": [5, 7,3] }
      ]
   }'
]);

Note: You must provide a valid JSON string (with double quotes) when using the second option. You can quickly validate your JSON string online using JSONLint.

Just the Assets

If you merely want to include the Highcharts/Highstock/Highmaps javascript libraries in your view, you can bypass the widget and access the asset bundle directly:

use miloschuman\highcharts\HighchartsAsset;

HighchartsAsset::register($this)->withScripts(['modules/stock', 'modules/exporting', 'modules/drilldown']);

In this scenario, you would need to write and include your own JavaScript to display the charts, just as illustrated in the Highcharts Demo, Highstock Demo and Highmaps Demo pages.

Tips