Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Ember from 'ember';
  2.  
  3. export default Ember.Component.extend({
  4.     tagName: 'canvas',
  5.     setup: false,
  6.  
  7.     /**
  8.      * Construction handler
  9.      * This will create the canvas and check the given
  10.      * input values since Chart.js can react pretty odd
  11.      * when getting wrong and/or missing values.
  12.      */
  13.     didInsertElement: function(){
  14.         var canvas = this.get('element');
  15.         var context = canvas.getContext('2d');
  16.  
  17.         canvas.width  = Ember.$(canvas).parent().width();
  18.         canvas.height = Ember.$(canvas).parent().height();
  19.  
  20.         var data = this.get('data');
  21.         var type = this.get('type').charAt(0).toUpperCase() + this.get('type').slice(1);
  22.         if(!type.match(/(Line|Bar|Radar|PolarArea|Pie|Doughnut)/)) {
  23.             type = "Line";
  24.         }
  25.         var options = (this.get('options') !== undefined) ? this.get('options') : {};
  26.  
  27.         this.setProperties({
  28.             '_data': data,
  29.             '_type': type,
  30.             '_canvas':  canvas,
  31.             '_context': context,
  32.             '_options': options
  33.         });
  34.         this.chartRender();
  35.     },
  36.  
  37.     /**
  38.      * Render the chart to the canvas
  39.      * This function is separated from the event hook to
  40.      * allow data overwriting which more or less results
  41.      * in updating the chart.
  42.      */
  43.     chartRender: function(){
  44.         var chart = new window.Chart(this.get('_context'))[this.get('_type')](this.get('_data'), this.get('_options'));
  45.         this.setProperties({
  46.             '_chart': chart,
  47.             'setup': true
  48.         });
  49.     },
  50.  
  51.     /**
  52.      * Chart Update Handler
  53.      * This will re-render the chart whenever its data or
  54.      * options changes, if the 'update' property is set to true
  55.      */
  56.     chartUpdate : function() {
  57.         if (this.get('update') === true && this.get('setup') === true) {
  58.             this.setProperties({
  59.                 '_data' : this.get('data'),
  60.                 '_options' : this.get('options')
  61.             });
  62.             this.chartRender();
  63.         }
  64.     }.observes('data', 'options')
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement