Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #125 #152

Merged
merged 2 commits into from
Oct 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = function(grunt) {
files: ['lib/**/*.coffee', 'test/unit/**/*.mocha.coffee'],
tasks: ['concat', 'coffeelint', 'coffee', 'karma:unminified', 'uglify', 'karma:minified'],
// tasks: ['concat', 'coffee', 'uglify']

},

karma: {
Expand Down
33 changes: 16 additions & 17 deletions dist/line-chart.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###
line-chart - v1.1.3 - 10 September 2014
line-chart - v1.1.3 - 14 October 2014
https://github.com/n3-charts/line-chart
Copyright (c) 2014 n3-charts
###
Expand All @@ -16,6 +16,9 @@ directive('linechart', ['n3utils', '$window', '$timeout', (n3utils, $window, $ti
_u = n3utils
dim = _u.getDefaultMargins()

# Hacky hack so the chart doesn't grow in height when resizing...
element[0].style['font-size'] = 0

scope.updateDimensions = (dimensions) ->
top = _u.getPixelCssProp(element[0].parentElement, 'padding-top')
bottom = _u.getPixelCssProp(element[0].parentElement, 'padding-bottom')
Expand All @@ -24,20 +27,18 @@ directive('linechart', ['n3utils', '$window', '$timeout', (n3utils, $window, $ti
dimensions.width = (element[0].parentElement.offsetWidth || 900) - left - right
dimensions.height = (element[0].parentElement.offsetHeight || 500) - top - bottom

scope.update = ->
scope.redraw = ->
scope.updateDimensions(dim)
scope.redraw(dim)
scope.update(dim)


isUpdatingOptions = false
initialHandlers =
onSeriesVisibilityChange: ({series, index, newVisibility}) ->
isUpdatingOptions = true
scope.options.series[index].visible = newVisibility
scope.$apply()
isUpdatingOptions = false

scope.redraw = (dimensions) ->
scope.update = (dimensions) ->
options = _u.sanitizeOptions(scope.options, attrs.mode)
handlers = angular.extend(initialHandlers, _u.getTooltipHandlers(options))
dataPerSeries = _u.getDataPerSeries(scope.data, options)
Expand Down Expand Up @@ -84,15 +85,12 @@ directive('linechart', ['n3utils', '$window', '$timeout', (n3utils, $window, $ti
promise = undefined
window_resize = ->
$timeout.cancel(promise) if promise?
promise = $timeout(scope.update, 1)
promise = $timeout(scope.redraw, 1)

$window.addEventListener('resize', window_resize)

scope.$watch('data', scope.update, true)
scope.$watch('options', (v) ->
return if isUpdatingOptions
scope.update()
, true)
scope.$watch('data', scope.redraw, true)
scope.$watch('options', scope.redraw, true)

return {
replace: true
Expand Down Expand Up @@ -403,10 +401,11 @@ mod.factory('n3utils', ['$window', '$log', '$rootScope', ($window, $log, $rootSc
)

item.on('click', (s, i) ->
isNowVisible = that.toggleSeries(svg, i)

d3.select(this).attr('opacity', if isNowVisible then '1' else '0.2')
handlers.onSeriesVisibilityChange?({series: s, index: i, newVisibility: isNowVisible})
handlers.onSeriesVisibilityChange?({
series: s,
index: i,
newVisibility: !(s.visible isnt false)
})
)

item.append('circle')
Expand Down Expand Up @@ -1095,7 +1094,7 @@ mod.factory('n3utils', ['$window', '$log', '$rootScope', ($window, $log, $rootSc
return [] unless o = options.axes[key]

domain = this.yExtent(
series.filter (s) -> s.axis is key
series.filter (s) -> s.axis is key and s.visible isnt false
data
options.stacks.filter (stack) -> stack.axis is key
)
Expand Down
31 changes: 11 additions & 20 deletions dist/line-chart.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*
line-chart - v1.1.3 - 10 September 2014
line-chart - v1.1.3 - 14 October 2014
https://github.com/n3-charts/line-chart
Copyright (c) 2014 n3-charts
*/
Expand All @@ -23,6 +23,7 @@ directive('linechart', [
var dim, initialHandlers, isUpdatingOptions, promise, window_resize, _u;
_u = n3utils;
dim = _u.getDefaultMargins();
element[0].style['font-size'] = 0;
scope.updateDimensions = function(dimensions) {
var bottom, left, right, top;
top = _u.getPixelCssProp(element[0].parentElement, 'padding-top');
Expand All @@ -32,22 +33,20 @@ directive('linechart', [
dimensions.width = (element[0].parentElement.offsetWidth || 900) - left - right;
return dimensions.height = (element[0].parentElement.offsetHeight || 500) - top - bottom;
};
scope.update = function() {
scope.redraw = function() {
scope.updateDimensions(dim);
return scope.redraw(dim);
return scope.update(dim);
};
isUpdatingOptions = false;
initialHandlers = {
onSeriesVisibilityChange: function(_arg) {
var index, newVisibility, series;
series = _arg.series, index = _arg.index, newVisibility = _arg.newVisibility;
isUpdatingOptions = true;
scope.options.series[index].visible = newVisibility;
scope.$apply();
return isUpdatingOptions = false;
return scope.$apply();
}
};
scope.redraw = function(dimensions) {
scope.update = function(dimensions) {
var axes, columnWidth, dataPerSeries, handlers, isThumbnail, options, svg;
options = _u.sanitizeOptions(scope.options, attrs.mode);
handlers = angular.extend(initialHandlers, _u.getTooltipHandlers(options));
Expand Down Expand Up @@ -86,16 +85,11 @@ directive('linechart', [
if (promise != null) {
$timeout.cancel(promise);
}
return promise = $timeout(scope.update, 1);
return promise = $timeout(scope.redraw, 1);
};
$window.addEventListener('resize', window_resize);
scope.$watch('data', scope.update, true);
return scope.$watch('options', function(v) {
if (isUpdatingOptions) {
return;
}
return scope.update();
}, true);
scope.$watch('data', scope.redraw, true);
return scope.$watch('options', scope.redraw, true);
};
return {
replace: true,
Expand Down Expand Up @@ -431,13 +425,10 @@ mod.factory('n3utils', [
}
});
item.on('click', function(s, i) {
var isNowVisible;
isNowVisible = that.toggleSeries(svg, i);
d3.select(this).attr('opacity', isNowVisible ? '1' : '0.2');
return typeof handlers.onSeriesVisibilityChange === "function" ? handlers.onSeriesVisibilityChange({
series: s,
index: i,
newVisibility: isNowVisible
newVisibility: !(s.visible !== false)
}) : void 0;
});
item.append('circle').attr({
Expand Down Expand Up @@ -1171,7 +1162,7 @@ mod.factory('n3utils', [
return [];
}
domain = this.yExtent(series.filter(function(s) {
return s.axis === key;
return s.axis === key && s.visible !== false;
}), data, options.stacks.filter(function(stack) {
return stack.axis === key;
}));
Expand Down
4 changes: 2 additions & 2 deletions dist/line-chart.min.js

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions lib/line-chart.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ directive('linechart', ['n3utils', '$window', '$timeout', (n3utils, $window, $ti
_u = n3utils
dim = _u.getDefaultMargins()

# Hacky hack so the chart doesn't grow in height when resizing...
element[0].style['font-size'] = 0

scope.updateDimensions = (dimensions) ->
top = _u.getPixelCssProp(element[0].parentElement, 'padding-top')
bottom = _u.getPixelCssProp(element[0].parentElement, 'padding-bottom')
Expand All @@ -18,20 +21,18 @@ directive('linechart', ['n3utils', '$window', '$timeout', (n3utils, $window, $ti
dimensions.width = (element[0].parentElement.offsetWidth || 900) - left - right
dimensions.height = (element[0].parentElement.offsetHeight || 500) - top - bottom

scope.update = ->
scope.redraw = ->
scope.updateDimensions(dim)
scope.redraw(dim)
scope.update(dim)


isUpdatingOptions = false
initialHandlers =
onSeriesVisibilityChange: ({series, index, newVisibility}) ->
isUpdatingOptions = true
scope.options.series[index].visible = newVisibility
scope.$apply()
isUpdatingOptions = false

scope.redraw = (dimensions) ->
scope.update = (dimensions) ->
options = _u.sanitizeOptions(scope.options, attrs.mode)
handlers = angular.extend(initialHandlers, _u.getTooltipHandlers(options))
dataPerSeries = _u.getDataPerSeries(scope.data, options)
Expand Down Expand Up @@ -78,15 +79,12 @@ directive('linechart', ['n3utils', '$window', '$timeout', (n3utils, $window, $ti
promise = undefined
window_resize = ->
$timeout.cancel(promise) if promise?
promise = $timeout(scope.update, 1)
promise = $timeout(scope.redraw, 1)

$window.addEventListener('resize', window_resize)

scope.$watch('data', scope.update, true)
scope.$watch('options', (v) ->
return if isUpdatingOptions
scope.update()
, true)
scope.$watch('data', scope.redraw, true)
scope.$watch('options', scope.redraw, true)

return {
replace: true
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/legend.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
)

item.on('click', (s, i) ->
isNowVisible = that.toggleSeries(svg, i)

d3.select(this).attr('opacity', if isNowVisible then '1' else '0.2')
handlers.onSeriesVisibilityChange?({series: s, index: i, newVisibility: isNowVisible})
handlers.onSeriesVisibilityChange?({
series: s,
index: i,
newVisibility: !(s.visible isnt false)
})
)

item.append('circle')
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/scales.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
return [] unless o = options.axes[key]

domain = this.yExtent(
series.filter (s) -> s.axis is key
series.filter (s) -> s.axis is key and s.visible isnt false
data
options.stacks.filter (stack) -> stack.axis is key
)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/legend.mocha.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ describe 'legend', ->
expect(element.childrenByClass('legendItem')[0].getAttribute('opacity')).to.equal('0.2')

fakeMouse.clickOn(l_0.childNodes[0])
expect(fn('lineGroup series_0')).to.equal('initial')
expect(fn('dotGroup series_0')).to.equal('initial')
expect(fn('lineGroup series_0')).to.equal('')
expect(fn('dotGroup series_0')).to.equal('')
expect(element.childrenByClass('legendItem')[0].getAttribute('opacity')).to.equal('1')

it 'should be able to hide a series at startup', ->
Expand Down
20 changes: 10 additions & 10 deletions test/unit/size.mocha.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ describe 'size', ->
</div>
"""
innerScope = element.childByClass('chart').aElement.isolateScope()
sinon.stub(innerScope, 'redraw', ->)
sinon.spy(innerScope, 'update')
sinon.stub(innerScope, 'update', ->)
sinon.spy(innerScope, 'redraw')

it 'should update when $window resize', inject ($window) ->
it 'should redraw when $window resize', inject ($window) ->
e = document.createEvent('HTMLEvents')
e.initEvent 'resize', true, false
$window.dispatchEvent e

it 'should pass the new dimensions to redraw when $window is resized ', inject ($window) ->
it 'should pass the new dimensions to update when $window is resized ', inject ($window) ->
sinon.stub innerScope, 'updateDimensions', (d) ->
d.width = 120
d.height = 50
Expand All @@ -31,7 +31,7 @@ describe 'size', ->
e.initEvent 'resize', true, false
$window.dispatchEvent e

describe 'size computation method', ->
describe 'computation method', ->
it 'should have default size', inject (pepito) ->
{element, outerScope} = pepito.directive("""
<div>
Expand All @@ -40,12 +40,12 @@ describe 'size', ->
""",
(element) ->
innerScope = element.children()[0].aElement.isolateScope()
sinon.spy innerScope, 'redraw'
sinon.spy innerScope, 'update'
sinon.spy innerScope, 'redraw'
)

innerScope = element.childByClass('chart').aElement.isolateScope()
expect(innerScope.redraw.args[0][0]).to.eql
expect(innerScope.update.args[0][0]).to.eql
top: 20
right: 50
bottom: 60
Expand All @@ -61,8 +61,8 @@ describe 'size', ->
""",
(element) ->
innerScope = element.children()[0].aElement.isolateScope()
sinon.stub innerScope, 'redraw', ->
sinon.spy innerScope, 'update'
sinon.stub innerScope, 'update', ->
sinon.spy innerScope, 'redraw'

sinon.stub n3utils, 'getPixelCssProp', (element, property) ->
throw new Error('Invalid id given to getPixelCssProp function') if element.id isnt 'toto'
Expand All @@ -83,7 +83,7 @@ describe 'size', ->
innerScope = element.children()[0].aElement.isolateScope()

outerScope.$digest()
expect(innerScope.redraw.args[1][0]).to.eql
expect(innerScope.update.args[1][0]).to.eql
top: 20
right: 50
bottom: 60
Expand Down