PHPackages                             sibilino/yii2-dygraphswidget - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. sibilino/yii2-dygraphswidget

ActiveLibrary[Templating &amp; Views](/categories/templating)

sibilino/yii2-dygraphswidget
============================

Graphing and charting widget for Yii 2, based on Dygraphs.js

v1.1.2(10y ago)416.4k↓54.2%4MITPHP

Since Feb 18Pushed 9y ago4 watchersCompare

[ Source](https://github.com/Sibilino/yii2-dygraphswidget)[ Packagist](https://packagist.org/packages/sibilino/yii2-dygraphswidget)[ Docs](https://github.com/Sibilino/yii2-dygraphswidget)[ RSS](/packages/sibilino-yii2-dygraphswidget/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (7)Dependencies (1)Versions (8)Used By (0)

Dygraphs Widget for Yii 2
=========================

[](#dygraphs-widget-for-yii-2)

---

[![Total Downloads](https://camo.githubusercontent.com/57ea5ba85b9fd7cc4ef6bdf90e5134a1a92a8cde15f4f7fb412076ca32b65979/68747470733a2f2f706f7365722e707567782e6f72672f736962696c696e6f2f796969322d64796772617068737769646765742f646f776e6c6f616473)](https://packagist.org/packages/sibilino/yii2-dygraphswidget)

A simple graph widget for Yii 2, based on \[Dygraphs\] ().

Changelog
---------

[](#changelog)

---

```
1.1.0 - Support for DateTime data and Data Providers.
1.0.0 - Added visibility checkboxes feature and completed tests.

```

Installation
------------

[](#installation)

---

### Composer

[](#composer)

Add *sibilino/yii2-dygraphswidget* to your *composer.json* file and perform a Composer Update as usual.

```
"require": {
	"sibilino/yii2-dygraphswidget": "*"
}
```

### Manually

[](#manually)

If for some reason you cannot or do not want to use [Composer](https://getcomposer.org/ "Composer"), then you must create the widget folder manually, and then configure your Yii application to autoload the widget classes.

First, create the folder structure *sibilino/yii2-dygraphswidget/widget* inside the *vendor* subfolder of your Yii application.

Then, download the widget .zip file and extract the **contents** of its *widget* subfolder into the folder you created in the previous step.

Next, edit *config/web.php* and add the following entry:

```
[
	//...
	'aliases' => [
		'@sibilino/y2dygraphs' => '@vendor/sibilino/yii2-dygraphswidget/widget',
	],
	//...
]
```

Finally, remember to use the namespace *sibilino\\y2dygraphs* when you call the widget.

Usage
-----

[](#usage)

---

In your view, create the widget with your data matrix as its *data* option.

```
use sibilino\y2dygraphs\DygraphsWidget;

echo DygraphsWidget::widget([
	'data' => $your_data,
]);
```

Dygraphs options
----------------

[](#dygraphs-options)

---

You can set the *options* property to pass additional options to the Dygraphs object:

```
echo DygraphsWidget::widget([
	'data' => $your_data,
	'options' => [
		'labels' => ['X', 'Sin', 'Rand', 'Pow'],
		'title'=> 'Main Graph',
		//...
	],
]);
```

Data formats
------------

[](#data-formats)

---

The data to display in the widget can be specified in several ways. Consider the following examples, and make sure to read \[the official documentation\] () for more details:

- **Matrix**

```
$data = [
	[1, 25, 100],
	[2, 50, 90],
	[3, 100, 80],
	//...
];
```

- **URL**URL to a text file with the data.

```
$data = 'http://dygraphs.com/dow.txt';
```

- **Model attribute**Specify the `model` and `attribute` configuration parameters to take the data from an attribute of a `yii\base\Model` object:

```
$myModel = UserStats::findOne($id);
// Assume $myModel->loginAttempts contains a matrix of login attempts per day
echo DygraphsWidget::widget([
	'model' => $myModel,
	'attribute' => 'loginAttempts',
	'options' => [
		//...
	],
]);
```

- **Data Provider**The `data` property can contain a Data Provider (implementing `yii\data\DataProviderInterface`). In this case, the data matrix will be generated from the models provided by the Data Provider. Each data row will contain the values of the attributes of one model. By default, all attributes of every model will be used, but you can configure the `attributes`property to specify the list of attributes to appear in a row (the specified order will be taken into account).

```
$provider = new ActiveDataProvider([
    'query' => User::find(),
]);
// Let's assume User contains the attributes 'id', 'joinDate', 'powerLevel'
echo DygraphsWidget::widget([
	'data' => $provider,
	'attributes' => ['joinDate', 'powerLevel'], // Display the graph of powerLevel by joinDate
	'options' => [
		//...
	],
]);
```

- **JavaScript**JS code that returns a data object usable by Dygraphs. The code must be wrapped inside a JsExpression object:

```
$your_data = new JsExpression('function () {
	var data = [];
      for (var i = 0; i < 1000; i++) {
        var base = 10 * Math.sin(i / 90.0);
        data.push([i, base, base + Math.sin(i / 2.0)]);
      }
      var highlight_start = 450;
      var highlight_end = 500;
      for (var i = highlight_start; i
