├── .bowerrc ├── .gitignore ├── README.md ├── bower.json ├── chartjs-directive.js ├── index.html └── package.json /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "vendor", 3 | "analytics": false, 4 | "timeout": 120000 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Chart-js Directive 2 | [Homepage](http://earlonrails.github.io/angular-chartjs-directive/) 3 | 4 | 5 | ## Usage 6 | 1. Include [chartjs](https://github.com/nnnick/Chart.js) via script tag, can use bower or download min 7 | 2. Include `chartjs-directive.js`. 8 | 3. Add `chartjs-directive` as a dependency to your app. 9 | 4. Make ``s. 10 | 11 | ## Bower 12 | Installable via `bower`: 13 | 14 | ```bash 15 | bower install angular-chartjs-directive 16 | ``` 17 | 18 | ## Example 19 | See the [homepage](http://earlonrails.github.io/angular-chartjs-directive/) for an example. 20 | 21 | ```html 22 |
23 | 24 |
25 | ``` 26 | 27 | ## License 28 | MIT 29 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-chartjs-directive", 3 | "version": "1.0.0", 4 | "main": "./chartjs-directive.js", 5 | "ignore": [ 6 | ".bowerrc", 7 | ".gitignore", 8 | "bower.json", 9 | "package.json", 10 | "index.html", 11 | "vendor", 12 | "README.md" 13 | ], 14 | "dependencies": { 15 | "angular": ">=1", 16 | "chartjs" : "<=2.1.6" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /chartjs-directive.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | angular.module('chartjs-directive', []). 4 | .directive('chart', function() { 5 | var baseWidth = 600; 6 | var baseHeight = 400; 7 | 8 | return { 9 | restrict: 'E', 10 | template: '', 11 | scope: { 12 | chartObject: "=value" 13 | }, 14 | link: function(scope, element, attrs) { 15 | var canvas, context, chart, options; 16 | var init = function(type) { 17 | element.empty(); 18 | element.append(''); 19 | 20 | canvas = element.find("canvas")[0]; 21 | context = canvas.getContext("2d"); 22 | 23 | options = { 24 | type: type || "Line", 25 | width: attrs.width || baseWidth, 26 | height: attrs.height || baseHeight, 27 | last_type: false 28 | }; 29 | canvas.height = options.height; 30 | canvas.width = options.width; 31 | chart = new Chart(context); 32 | }; 33 | init(attrs.type); 34 | 35 | scope.$watch(function() { 36 | return element.attr('type'); 37 | }, function(value) { 38 | if (!value) return; 39 | init(value); 40 | var chartType = options.type; 41 | }); 42 | 43 | //Update when charts data changes 44 | scope.$watch(function() { 45 | return scope.chartObject; 46 | }, function(value) { 47 | if (!value) return; 48 | 49 | var chartType = options.type; 50 | if (scope.chartInstance) { 51 | scope.chartInstance.destroy(); 52 | } 53 | if (typeof scope.chartObject !== 'undefined') { 54 | scope.chartInstance = chart[chartType](scope.chartObject.data, scope.chartObject.options); 55 | } 56 | }, true); 57 | } 58 | } 59 | }); 60 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | AngularJS Chartjs Directive 7 | 8 | 9 | 18 | 19 | 20 |
21 |
22 |

Angular Chartjs Directive

23 |
24 |

25 |
26 |
27 |

Usage

28 | 29 |
30 |
31 |
    32 |
  1. Include chartjs-directive.js.
  2. 33 |
  3. Add directive 34 |
    <div ng-app="chartjs-directive">
     35 |   <chart value="myChart"></chart>
     36 | </div>
  4. 37 | 38 |
  5. Inject some data 39 |
    
     40 | function myContoller(){
     41 | 
     42 |   var data = {
     43 |     labels : ["January","February","March","April","May","June","July"],
     44 |     datasets : [
     45 |       {
     46 |         fillColor : "rgba(220,220,220,0.5)",
     47 |         strokeColor : "rgba(220,220,220,1)",
     48 |         pointColor : "rgba(220,220,220,1)",
     49 |         pointStrokeColor : "#fff",
     50 |         data : [65,59,90,81,56,55,40]
     51 |       },
     52 |       {
     53 |         fillColor : "rgba(151,187,205,0.5)",
     54 |         strokeColor : "rgba(151,187,205,1)",
     55 |         pointColor : "rgba(151,187,205,1)",
     56 |         pointStrokeColor : "#fff",
     57 |         data : [28,48,40,19,96,27,100]
     58 |       }
     59 |     ]
     60 |   }
     61 | 
     62 |   $scope.myChart.data = data;
     63 | }
     64 | 
  6. 65 |
66 |
67 |
68 |
69 |
70 |
71 |

In action

72 |
73 |
74 | 75 |
76 |
77 | 78 |

^ This is a chart

79 | 82 |
83 |
84 | 85 |
86 |
87 |

Tag Parameters

88 |
    89 |
  • value (default: "Line")
  • 90 |
  • width (default: 600)
  • 91 |
  • height (default: 400)
  • 92 |
93 | 94 |

value can be any of the chartjs chart types:

95 |
    96 |
  • 97 | 100 |
  • 101 |
    102 |
  • 103 | 106 |
  • 107 |
    108 |
  • 109 | 112 |
  • 113 |
    114 |
  • 115 | 118 |
  • 119 |
    120 |
  • 121 | 124 |
  • 125 |
    126 |
  • 127 | 130 |
  • 131 |
132 |
133 |
134 | 135 |
136 |
137 |

Chart Parameters

138 | 143 |
144 |
145 |
146 |
147 |
148 |

Bower

149 |

Installable via bower:

150 | bower install angular-chartjs-directive 151 |
152 |

License

153 |

MIT

154 |
155 |
156 | 157 | Fork me on GitHub 158 | 159 | 160 | 161 | 162 | 264 | 265 | 266 | 267 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-chartjs-directive", 3 | "version": "1.0.0", 4 | "description": "An Angular Js Directive which allows to simplify the use of Chartjs charts", 5 | "main": "chartjs-directive.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/earlonrails/angular-chartjs-directive.git" 12 | }, 13 | "keywords": [ 14 | "An", 15 | "Angular", 16 | "Js", 17 | "Directive", 18 | "which", 19 | "allows", 20 | "to", 21 | "simplify", 22 | "the", 23 | "use", 24 | "of", 25 | "Chartjs", 26 | "charts" 27 | ], 28 | "author": "Kevin Krauss ", 29 | "license": "ISC", 30 | "bugs": { 31 | "url": "https://github.com/earlonrails/angular-chartjs-directive/issues" 32 | }, 33 | "homepage": "https://github.com/earlonrails/angular-chartjs-directive#readme" 34 | } 35 | --------------------------------------------------------------------------------