Skip to content

Commit

Permalink
Merge branch 'release/0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
maxklenk committed Jul 14, 2014
2 parents fc39ddd + 76dfde3 commit 3367f9c
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 14 deletions.
6 changes: 5 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function (grunt) {
// default task
grunt.registerTask('default', ['jshint', 'karma:unit']);
grunt.registerTask('watch', ['karma:watch']);
grunt.registerTask('coverage', ['coveralls']);
grunt.registerTask('coverage', ['karma:coverage', 'coveralls']);


// perform test in Firefox on travis ci
Expand All @@ -28,6 +28,10 @@ module.exports = function (grunt) {
options: testConfig('test/karma.conf.js'),
singleRun: false,
autoWatch: true
},
coverage: {
options: testConfig('test/karma.conf.js'),
reporters: ['coverage']
}
},

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ bower install angular-chart

Add everything to your index.html:
```html
<link rel="stylesheet" href="bower_components/c3/c3.css" />

<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/d3/d3.js"></script>
<script src="bower_components/c3/c3.js"></script>
Expand Down
136 changes: 130 additions & 6 deletions angular-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
angular.module('angularChart', [])
.directive('angularchart',

function () {
function ($compile) {

var c3 = window.c3;

Expand All @@ -28,23 +28,41 @@ angular.module('angularChart', [])
keys: {
value: []
},
types: {}
types: {},
names: [],
selection: {
enabled: true,
multiple: false,
},
onselected: function (d, element) {
if (!scope.avoidSelections) {
scope.addSelected(d);
}
},
onunselected: function (d, element) {
if (!scope.avoidSelections) {
scope.removeSelected(d);
}
}
},

axis: {
x: {
tick: {}
}
},
legend: {
position: 'right'
}
};


// add unique identifier for each chart
//
scope.addIdentifier = function () {
scope.options.dataAttributeChartID = 'chartid' + Math.floor(Math.random() * 1000000001);
angular.element(element).attr('id', scope.options.dataAttributeChartID);
scope.configuration.bindto = '#' + scope.options.dataAttributeChartID;

angular.element(element).attr('style', 'display: block;');
};

// reload the charts data
Expand All @@ -66,16 +84,27 @@ angular.module('angularChart', [])
scope.configuration.data.json = scope.dataset.records;
}

// Chart type
//
if(scope.options.type) {
scope.configuration.data.type = scope.options.type;
}


// Add lines
//
if (!scope.options.rows) {
console.error('The rows to display have to be defined.');
} else {
scope.configuration.data.keys.value = [];
scope.options.rows.forEach(function (element) {
// TODO exists check? ERROR
scope.configuration.data.keys.value.push(element.name);

// data label
scope.configuration.data.names[element.name] = element.label ? element.label : element.name;

// chart type
if (element.type) {
// TODO valid type ERROR
scope.configuration.data.types[element.name] = element.type;
Expand All @@ -88,6 +117,11 @@ angular.module('angularChart', [])
if (!scope.options.xAxis || !scope.options.xAxis.name) {
console.error('no xAxis provided');
} else {
// key selection changed?
if (scope.configuration.data.keys.x !== null && scope.configuration.data.keys.x !== scope.options.xAxis.name) {
scope.options.selected = [];
}

scope.configuration.data.keys.x = scope.options.xAxis.name;
if (scope.options.xAxis.displayFormat) {
scope.configuration.axis.x.tick.format = scope.options.xAxis.displayFormat;
Expand All @@ -111,15 +145,104 @@ angular.module('angularChart', [])
}

scope.chart = c3.generate(scope.configuration);
scope.chooseXAxis();
};

// Choose x-axis
scope.chooseXAxis = function () {
if (scope.options.type === 'pie' ||scope.options.type === 'donut' ) {
return;
}
var el = angular.element('<span/>');
el.append('<select ng-model="options.xAxis.name" style="margin-left: 42%"><option ng-repeat="col in dataset.schema" value="{{col.name}}" ng-selected="col.name==options.xAxis.name">{{col.label ? col.label : col.name}}</option></select>');
$compile(el)(scope);
element.append(el);

angular.element(element).attr('style', angular.element(element).attr('style') + ' padding-bottom: 30px');
};

// Selections
//
scope.avoidSelections = false;
scope.options.selected = scope.options.selected ? scope.options.selected : [];

// handle chart event onselection
scope.addSelected = function (selection) {
scope.$apply(
scope.options.selected.push(selection)
);
};

// handle chart event onunselection
scope.removeSelected = function (selection) {
scope.$apply(
scope.options.selected = scope.options.selected.filter(function (selected) {
return selected.id !== selection.id || selected.index !== selection.index;
})
);
};

// select elements inside the chart
scope.performSelections = function (selections) {
scope.avoidSelections = true;
selections.forEach(function (selection) {
scope.chart.select([selection.id], [selection.index]);
});
scope.avoidSelections = false;
};

// unselect elements inside the chart
scope.performUnselections = function (selections) {
scope.avoidSelections = true;
selections.forEach(function (selection) {
scope.chart.unselect([selection.id], [selection.index]);
});
scope.avoidSelections = false;
};


// watcher of changes in options
//
scope.startOptionsWatcher = function () {

scope.$watch('options', function (newValue, oldValue) {
if (newValue === oldValue) { // skip the first run of $watch
return;
}

oldValue.selected = oldValue.selected ? oldValue.selected : [];
newValue.selected = newValue.selected ? newValue.selected : [];

// newSelections
var newSelections = newValue.selected.filter(function (elm) {
var isNew = true;
oldValue.selected.forEach(function (old) {
if (old.id === elm.id && old.index === elm.index) {
isNew = false;
return;
}
});
return isNew;
});
if (newSelections.length > 0) {
return scope.performSelections(newSelections);
}

// removedSelections
var removedSelections = oldValue.selected.filter(function (elm) {
var isOld = true;
newValue.selected.forEach(function (old) {
if (old.id === elm.id && old.index === elm.index) {
isOld = false;
return;
}
});
return isOld;
});
if (removedSelections.length > 0) {
return scope.performUnselections(removedSelections);
}

scope.updateChart();
}, true); // checks for changes inside options
};
Expand All @@ -146,9 +269,10 @@ angular.module('angularChart', [])
// startup
scope.addIdentifier();
scope.updateChart();
scope.performSelections(scope.options.selected);
scope.startOptionsWatcher();
scope.startDatasetWatcher();

}
};
}
);
});
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-chart",
"version": "0.1.0",
"version": "0.1.1",
"authors": [
"maxklenk"
],
Expand Down
16 changes: 15 additions & 1 deletion demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var app = angular.module('demoApp', ['angularChart', 'json-tree']);

app.controller('Controller', function ($scope) {
$scope.dataset = window.dataset;
$scope.options = {
$scope.options1 = {
rows: [{
name: 'income',
type: 'bar'
Expand All @@ -18,4 +18,18 @@ app.controller('Controller', function ($scope) {
yAxis: {
}
};
$scope.options2 = {
rows: [{
name: 'income'
}, {
name: 'sales'
}],
type: 'pie',
xAxis: {
name: 'dayString',
// displayFormat: '%Y-%m-%d %H:%M:%S'
},
yAxis: {
}
};
});
9 changes: 7 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ <h1 style="margin-bottom: 20px">angular-chart Demo App</h1>
<div ng-controller="Controller">
<angularchart
dataset="dataset"
options="options">
options="options1">
</angularchart>
<angularchart
dataset="dataset"
options="options2">
</angularchart>

<h2>dataset</h2>
<json-tree json='dataset'></json-tree>
<h2>options</h2>
<json-tree json='options'></json-tree>
<json-tree json='options1'></json-tree>
<json-tree json='options2'></json-tree>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-chart",
"version": "0.1.0",
"version": "0.1.1",
"description": "C3-based reusable chart directive",
"main": "angular-chart.js",
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions test/angular-chart_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ describe('angularChart', function () {
//console.log(element);
});

it('watch selections', function () {
var elementScope = scope.getElementScope(element);

// chart selection
elementScope.configuration.data.onselected({}, {});

// chart unselection
elementScope.configuration.data.onunselected({}, {});

// external selection
scope.options.selected.push({id: 'test', index: 3});
scope.$apply();
scope.options.selected.push({id: 'test', index: 4});
scope.$apply();

// external unselection
scope.options.selected = scope.options.selected.splice(0,1);
scope.$apply();
});

});

Expand Down
3 changes: 1 addition & 2 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ module.exports = function (config) {
plugins: [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-coverage'
],

// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters: ['coverage'],
reporters: ['dots', 'coverage'],

coverageReporter: {
type: 'lcov', // lcov or lcovonly are required for generating lcov.info files
Expand Down

0 comments on commit 3367f9c

Please sign in to comment.