Skip to content

Commit

Permalink
Merge pull request #12 from aomra015/updateEmberCLI1
Browse files Browse the repository at this point in the history
Update Ember and CLI versions
  • Loading branch information
aomran committed Mar 28, 2015
2 parents 3b52710 + 16d4c0a commit 962aa7a
Show file tree
Hide file tree
Showing 27 changed files with 457 additions and 3,588 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ indent_style = space
indent_size = 2

[*.hbs]
insert_final_newline = false
indent_style = space
indent_size = 2

Expand All @@ -29,5 +30,5 @@ indent_size = 2
indent_style = space
indent_size = 2

[*.md]
[*.{diff,md}]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# dependencies
/node_modules
/bower_components/*
/bower_components

# misc
/.sass-cache
Expand Down
32 changes: 32 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"predef": [
"document",
"window",
"-Promise"
],
"browser": true,
"boss": true,
"curly": true,
"debug": false,
"devel": true,
"eqeqeq": true,
"evil": true,
"forin": false,
"immed": false,
"laxbreak": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": false,
"nomen": false,
"onevar": false,
"plusplus": false,
"regexp": false,
"undef": true,
"sub": true,
"strict": false,
"white": false,
"eqnull": true,
"esnext": true,
"unused": true
}
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bower_components/
tests/
tmp/

.bowerrc
.editorconfig
.ember-cli
.travis.yml
.npmignore
**/.gitkeep
bower.json
Brocfile.js
testem.json
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
---
language: node_js
node_js:
- "0.12"

sudo: false

cache:
directories:
- node_modules

before_install:
- "npm config set spin false"
- "npm install -g npm@^2"

install:
- npm install -g bower
- npm install
Expand Down
1 change: 1 addition & 0 deletions Brocfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* jshint node: true */
/* global require, module */

var EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
Expand Down
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2015

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Ember Chart

This is an Ember CLI addon for adding a [ChartJS](http://www.chartjs.org/) component.
[![Build Status](https://travis-ci.org/aomra015/ember-cli-chart.svg)](https://travis-ci.org/aomra015/ember-cli-chart)

This is an Ember CLI addon for adding a [ChartJS](http://www.chartjs.org/) component. This addon is tested on Ember-CLI v0.2.1 and uses the `ember-try` addon to test against multiple Ember versions.

### Installation

```
$ npm install --save-dev ember-cli-chart
$ ember install:addon ember-cli-chart
```

### Usage
Expand Down Expand Up @@ -33,4 +35,4 @@ In your handlebars template just do:

```
{{ember-chart type='Line' data=model.chartData width=200 height=200 legend=true}}
```
```
56 changes: 56 additions & 0 deletions addon/chart-data-updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Ember from 'ember';

export default Ember.Object.extend({

updateByType: function () {
var data = this.get('data');

if (data.datasets) {return this.updateLinearCharts();}
if (Array.isArray(data)) {return this.updatePieCharts();}
},

updateLinearCharts: function () {
var datasets = this.get('data').datasets;
var chart = this.get('chart');

datasets.forEach(function(dataset, i) {
dataset.data.forEach(function(item, j) {
item = item || 0;
if (typeof chart.datasets[i] === 'undefined') {
chart.segments[j].value = item;
} else {
var dataSet = chart.datasets[i];

if(typeof dataSet.bars !== 'undefined') {
chart.datasets[i].bars[j].value = item;
} else {
chart.datasets[i].points[j].value = item;
}
}
});
});
return true;
},

updatePieCharts: function () {
var data = this.get('data');
var chart = this.get('chart');
var needUpdate = false;

data.forEach(function(segment, i) {
if (typeof chart.segments[i] !== 'undefined') {
segment.value = segment.value || 0;
if (chart.segments[i].value !== segment.value) {
chart.segments[i].value = segment.value;
needUpdate = true;
}
}
else {
// there are now more segments than the chart knows about; add them
chart.addData(segment, i, true);
needUpdate = true;
}
});
return needUpdate;
}
});
43 changes: 43 additions & 0 deletions addon/components/ember-chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Ember from 'ember';
import ChartDataUpdater from 'ember-cli-chart/chart-data-updater';
/* global Chart */

export default Ember.Component.extend({
tagName: 'canvas',
attributeBindings: ['width', 'height'],

renderChart: function(){
var context = this.get('element').getContext('2d');
var data = this.get('data');
var type = this.get('type').classify();
var options = Ember.merge({}, this.get('options'));

var chart = new Chart(context)[type](data, options);

if (this.get('legend')) {
var legend = chart.generateLegend();
this.$().parent().append(legend);
}

this.set('chart', chart);
}.on('didInsertElement'),

destroyChart: function(){
if (this.get('legend')) {
this.$().parent().children('[class$=legend]').remove();
}

this.get('chart').destroy();
}.on('willDestroyElement'),

updateChart: function(){
var chart = this.get('chart');
var data = this.get('data');
var needUpdate = ChartDataUpdater.create({
data: data,
chart: chart
}).updateByType();

if (needUpdate) { chart.update(); }
}.observes('data', 'data.[]', 'options')
});
92 changes: 2 additions & 90 deletions app/components/ember-chart.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,4 @@
import Ember from 'ember';
/* global Chart */
import EmberChart from 'ember-cli-chart/components/ember-chart';

export default Ember.Component.extend({
tagName: 'canvas',
attributeBindings: ['width', 'height'],

renderChart: function(){
var context = this.get('element').getContext('2d');
var data = this.get('data');
var type = this.get('type').classify();
var options = Ember.merge({}, this.get('options'));

var chart = new Chart(context)[type](data, options);

if (this.get('legend')) {
var legend = chart.generateLegend();
this.$().parent().append(legend);
};

this.set('chart', chart);
}.on('didInsertElement'),

destroyChart: function(){
if (this.get('legend')) {
this.$().parent().children('[class$=legend]').remove();
};

this.get('chart').destroy();
}.on('willDestroyElement'),

updateChart: function(){
try {
var chart = this.get('chart');
var data = this.get('data');
var needUpdate = this.updateChartBasedOnType(data, chart);

if (needUpdate) {
chart.update();
}
} catch(error) {
Ember.warn('Dataset is not equal in structure as previous values. Rebuilding chart...');
console.error(error);
this.destroyChart();
this.renderChart();
}
}.observes('data', 'data.[]', 'options'),

updateChartBasedOnType: function (data, chart) {
if (data.datasets) {return this.updateLinearCharts(data.datasets, chart)};
if (Array.isArray(data)) {return this.updatePieCharts(data, chart)};
},

updateLinearCharts: function (datasets, chart) {
datasets.forEach(function(dataset, i) {
dataset.data.forEach(function(item, j) {
item = item || 0;
if (typeof chart.datasets[i] === 'undefined') {
chart.segments[j].value = item;
} else {
var dataSet = chart.datasets[i];

if(typeof dataSet.bars !== 'undefined') {
chart.datasets[i].bars[j].value = item;
} else {
chart.datasets[i].points[j].value = item;
}
}
});
});
return true;
},

updatePieCharts: function (data, chart) {
var needUpdate = false;
data.forEach(function(segment, i) {
if (typeof chart.segments[i] !== 'undefined') {
segment.value = segment.value || 0
if (chart.segments[i].value != segment.value) {
chart.segments[i].value = segment.value;
needUpdate = true;
}
}
else {
// there are now more segments than the chart knows about; add them
chart.addData(segment, i, true);
needUpdate = true;
}
});
return needUpdate;
}
});
export default EmberChart;
7 changes: 7 additions & 0 deletions blueprints/ember-cli-chart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
normalizeEntityName: function() {},

afterInstall: function() {
return this.addBowerPackageToProject('chartjs', '1.0.1');
}
};
27 changes: 12 additions & 15 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
{
"name": "dummy",
"name": "ember-cli-chart",
"dependencies": {
"handlebars": "~1.3.0",
"jquery": "^1.11.1",
"ember": "1.7.0",
"ember-data": "1.0.0-beta.10",
"ember-resolver": "~0.1.7",
"loader": "stefanpenner/loader.js#1.0.1",
"ember-cli-shims": "stefanpenner/ember-cli-shims#0.0.3",
"ember-cli-test-loader": "rwjblue/ember-cli-test-loader#0.0.4",
"ember-load-initializers": "stefanpenner/ember-load-initializers#0.0.2",
"ember-qunit": "0.1.8",
"ember-qunit-notifications": "0.0.4",
"qunit": "~1.15.0"
},
"devDependencies": {
"chartjs": "1.0.1-beta.4"
"ember": "1.10.0",
"ember-data": "1.0.0-beta.16",
"ember-resolver": "~0.1.14",
"loader.js": "ember-cli/loader.js#3.2.0",
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3",
"ember-cli-test-loader": "ember-cli-test-loader#0.1.3",
"ember-load-initializers": "ember-cli/ember-load-initializers#0.0.2",
"ember-qunit": "0.2.8",
"ember-qunit-notifications": "0.0.7",
"qunit": "~1.17.1",
"chartjs": "1.0.1"
}
}
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* jshint node: true */
'use strict';

module.exports = {
name: 'Ember CLI Chart',
name: 'ember-cli-chart',

included: function included(app) {
this.app = app;
this._super.included(app);

app.import('vendor/chartjs/Chart.js');
app.import(app.bowerDirectory + '/chartjs/Chart.js');
}
};
};
Loading

0 comments on commit 962aa7a

Please sign in to comment.