You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.8KB

  1. <?php
  2. /**
  3. * @link https://github.com/2amigos/yii2-chartjs-widget
  4. * @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
  5. * @license http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. namespace dosamigos\chartjs;
  8. use yii\base\InvalidConfigException;
  9. use yii\base\Widget;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Html;
  12. use yii\helpers\Json;
  13. use yii\web\JsExpression;
  14. /**
  15. *
  16. * Chart renders a canvas ChartJs plugin widget.
  17. *
  18. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  19. * @link http://www.ramirezcobos.com/
  20. * @link http://www.2amigos.us/
  21. */
  22. class ChartJs extends Widget
  23. {
  24. /**
  25. * @var array the HTML attributes for the widget container tag.
  26. */
  27. public $options = [];
  28. /**
  29. * @var array the options for the underlying ChartJs JS plugin.
  30. * Please refer to the corresponding ChartJs type plugin Web page for possible options.
  31. * For example, [this page](http://www.chartjs.org/docs/#lineChart) shows
  32. * how to use the "Line chart" plugin.
  33. */
  34. public $clientOptions = [];
  35. /**
  36. * @var array the datasets configuration options and data to display on the chart.
  37. * See [its documentation](http://www.chartjs.org/docs/) for the different options.
  38. */
  39. public $data = [];
  40. /**
  41. * @var string the type of chart to display. The possible options are:
  42. * - "Line" : A line chart is a way of plotting data points on a line. Often, it is used to show trend data, and the
  43. * comparison of two data sets.
  44. * - "Bar" : A bar chart is a way of showing data as bars. It is sometimes used to show trend data, and the
  45. * comparison of multiple data sets side by side.
  46. * - "Radar" : A radar chart is a way of showing multiple data points and the variation between them. They are often
  47. * useful for comparing the points of two or more different data sets
  48. * - "PolarArea" : Polar area charts are similar to pie charts, but each segment has the same angle - the radius of
  49. * the segment differs depending on the value. This type of chart is often useful when we want to show a comparison
  50. * data similar to a pie chart, but also show a scale of values for context.
  51. * - "Pie" : Pie charts are probably the most commonly used chart there are. They are divided into segments, the arc
  52. * of each segment shows a the proportional value of each piece of data.
  53. * - "Doughnut" : Doughnut charts are similar to pie charts, however they have the centre cut out, and are therefore
  54. * shaped more like a doughnut than a pie!
  55. */
  56. public $type;
  57. /**
  58. * Initializes the widget.
  59. * This method will register the bootstrap asset bundle. If you override this method,
  60. * make sure you call the parent implementation first.
  61. */
  62. public function init()
  63. {
  64. parent::init();
  65. if ($this->type === null) {
  66. throw new InvalidConfigException("The 'type' option is required");
  67. }
  68. if (!isset($this->options['id'])) {
  69. $this->options['id'] = $this->getId();
  70. }
  71. }
  72. /**
  73. * Renders the widget.
  74. */
  75. public function run()
  76. {
  77. echo Html::tag('canvas', '', $this->options);
  78. $this->registerClientScript();
  79. }
  80. /**
  81. * Registers the required js files and script to initialize ChartJS plugin
  82. */
  83. protected function registerClientScript()
  84. {
  85. $id = $this->options['id'];
  86. $view = $this->getView();
  87. ChartJsAsset::register($view);
  88. $config = Json::encode(
  89. [
  90. 'type' => $this->type,
  91. 'data' => $this->data ?: new JsExpression('{}'),
  92. 'options' => $this->clientOptions ?: new JsExpression('{}')
  93. ]
  94. );
  95. $js = ";var chartJS_{$id} = new Chart($('#{$id}'),{$config});";
  96. $view->registerJs($js);
  97. }
  98. }