Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

chartjs-plugin-annotation #34

Open
mkormout opened this issue Dec 9, 2017 · 6 comments
Open

chartjs-plugin-annotation #34

mkormout opened this issue Dec 9, 2017 · 6 comments

Comments

@mkormout
Copy link

mkormout commented Dec 9, 2017

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:

  • requiring "bower-asset/chartjs-plugin-annotation": "*", in composer.json
  • inheriting AssetBundle to ChartJsAnnotationAsset (see ChartJsAsset.php)
  • calling ChartJsAnnotationAsset::register($this); in the target view
@tonydspaniard
Copy link
Member

@kormizz any help highly appreciated. PRs welcome.

@sasha-x
Copy link

sasha-x commented Dec 28, 2019

+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
Copy link
Member

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

@bbushong
Copy link

bbushong commented Dec 2, 2020

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
Copy link
Author

mkormout commented Dec 2, 2020

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:

  • Require "bower-asset/chartjs-plugin-annotation" in composer.json.
  • Create a new module in assets folder called ChartJsAnnotationAsset.php and paste the class ChartJsAnnotationAsset inside...

OPTION 1 - local usage:

  • In View where you want to use it, call ChartJsAnnotationAsset::register($this)
  • You should call this in each view where the annotation plugin is needed...

OPTION 2 - global usage:

  • Create a new class for ChartJs extending \dosamigos\chartjs\ChartJs and override registerClientScript method like in the example above.
  • Use the new class instead of original one...
  • With this option, registration of the annotation plugin is automatic...

@bbushong
Copy link

bbushong commented Dec 2, 2020

@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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants