2amigos / yii2-chartjs-widget

ChartJs Widget For Yii2
https://2amigos.us
Other
109 stars 67 forks source link

chartjs-plugin-annotation #34

Open mkormout opened 6 years ago

mkormout commented 6 years ago

Any chance to get something like chartjs-plugin-annotation bundled with yii2-chartjs-widget? Thank you!

EDIT: For those who needs this, I've already achieved this by:

tonydspaniard commented 6 years ago

@kormizz any help highly appreciated. PRs welcome.

sasha-x commented 4 years ago

+1 @kormizz

use yii\web\AssetBundle;
class ChartJsAnnotationAsset extends AssetBundle
{
    public $sourcePath = '@bower/chartjs-plugin-annotation';
    public $js = [
        'chartjs-plugin-annotation.js',
    ];
    public $depends = [
        'yii\web\JqueryAsset',
        'dosamigos\chartjs\ChartJsAsset'
    ];
}
class ChartJs extends \dosamigos\chartjs\ChartJs
{
    //add annotation plugin support
    protected function registerClientScript()
    {
         //other way options block in js config is empty and annotations don't work
        $this->clientOptions = $this->options;  
        $view = $this->getView();
        ChartJsAnnotationAsset::register($view);
        parent::registerClientScript();
    }
}

Should I make PR(s)?

tonydspaniard commented 4 years ago

@sasha-x that would be great. Please, make sure test work when you do. Thanks.

bbushong commented 3 years ago

Can I get more detail as to where the above code goes? I'm trying to get this plugin to work as well, and would love more details...

mkormout commented 3 years ago

Can I get more detail as to where the above code goes? I'm trying to get this plugin to work as well, and would love more details...

It's a long time but AFAIK:

PREPARATION:

OPTION 1 - local usage:

OPTION 2 - global usage:

bbushong commented 3 years ago

@mkormout a H-U-G-E thank you! I was close, but you got me the rest of the way!! For anyone else that needs a little more info (when going with option 1): At top of view:

use dosamigos\chartjs\ChartJs;
use app\assets\ChartJsAnnotationAsset;
ChartJsAnnotationAsset::register($this)

and then the client options part in that view:

'clientOptions' => [
        'legend' => [
            'labels' => [
                'fontFamily' => "'Roboto', sans-serif;'",
            ]
        ],
        **'annotation' => [
            'annotations' => [
                [
                    'drawTime' => 'afterDatasetsDraw',
                    'display' => true,
                    'type' => 'line',
                    'mode' => 'horizontal',
                    'scaleID' => 'yAxes', // links to below...
                    'value' => '100',
                    'borderColor' => 'red',
                    'borderWidth' => 2,
                ],
            ],
        ],
        'scales' => [
            'yAxes'=> [
                [
                    'id' => 'yAxes',   //needed for the scaleID above...
                    'ticks' => [
                        'beginAtZero' => true,
                        'fontFamily' => "'Roboto', sans-serif;'",
                        'suggestedMax' => 100,
                    ],
                ]
            ],
            'xAxes'=> [
                [
                    'ticks' => [
                        //'fontFamily' => "'Roboto', sans-serif;'",
                        'stepSize' => 1,
                    ],
                ]
            ],
        ],
    ],

Again, thank you so much!