Sibilino / yii2-dygraphswidget

A simple charting widget for Yii 2, based on Dygraphs.
MIT License
4 stars 4 forks source link
charting charts data-series dygraphs php widget yii2 yii2-extension yii2-widgets

Dygraphs Widget for Yii 2


Total Downloads

A simple graph widget for Yii 2, based on [Dygraphs] (http://dygraphs.com/).

Changelog


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

Installation


Composer

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

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

Manually

If for some reason you cannot or do not want to use 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


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


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


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] (http://dygraphs.com/data.html) for more details:

Additional options


The following widget properties can also be specified:

JavaScript code in options


Anytime you need to pass JavaScript code (for example, passing a function to an option), just pass a new JsExpression($your_js_code). For example:

$options = [
    'underlayCallback' => new JsExpression('function(canvas, area, g)
            {
                var bottom_left = g.toDomCoords(highlight_start, -20);
                var top_right = g.toDomCoords(highlight_end, +20);

                var left = bottom_left[0];
                var right = top_right[0];

                canvas.fillStyle = "rgba(255, 255, 102, 1.0)";
                canvas.fillRect(left, area.y, right - left, area.h);
            }'),
];

Visibility checkboxes


It is often useful to hide and show some of the dataseries in a chart. The widget features helper scripts to easily control series visibily with checkboxes.

To use this feature, make sure your page has one checkbox per series in the chart, and give each checkbox an id attribute with the index of the series controlled by it. Then, configure the widget with a checkBoxSelector that matches the group of checkboxes. For example, for a chart with 2 data series:

<input class="visibility" id="0" type="checkbox">
<input class="visibility" id="1" type="checkbox">
<?= DygraphsWidget::widget([
    'checkBoxSelector' => '.visibility',
    'data' => [
        // [x, series0, series1]
        [1, 25, 100],
        [2, 50, 90],
        [3, 100, 80],
        //...
    ],
    'options' => [
        // Starting visibility
        'visibility' => [
            false,
            true,
        ],
        //...
    ],
    // ...
]);?>

The attribute that associates a checkbox with a data series (id in the example) can be changed by configuring checkBoxReferenceAttr.