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

Improve chart clickability #48

Merged
merged 1 commit into from
Mar 27, 2018
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
20 changes: 13 additions & 7 deletions addon/components/simple-chart-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default Component.extend(ChartProperties, {
const hover = get(this, 'hover');
const leave = get(this, 'leave');
const click = get(this, 'click');
const isClickable = get(this, 'isClickable');
const dataOrArray = data?data:[{data: 1, label: '', empty: true}];
const svg = select(this.element);
const color = scaleOrdinal(schemeCategory10);
Expand Down Expand Up @@ -57,20 +58,25 @@ export default Component.extend(ChartProperties, {
}
});

rect.on('click', data => {
if (click) {
click(data);
}
});

bars.selectAll('text').data(dataOrArray).enter()
const text = bars.selectAll('text').data(dataOrArray).enter()
.append("text")
.attr("fill", "#ffffff")
.style("font-size", ".8rem")
.attr("text-anchor", "middle")
.attr('x', d => `${xScale(d.label ) + xScale.bandwidth() / 2}%`)
.attr('y', d => `${110 - yScale(d.data)}%`)
.text(d => d.label);

if (isClickable) {
rect.on('click', data => {
click(data);
});
rect.style("cursor", "pointer");
text.on('click', data => {
click(data);
});
text.style("cursor", "pointer");
}
}
},
});
20 changes: 13 additions & 7 deletions addon/components/simple-chart-donut.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default Component.extend(ChartProperties, {
const hover = get(this, 'hover');
const leave = get(this, 'leave');
const click = get(this, 'click');
const isClickable = get(this, 'isClickable');
const color = scaleOrdinal(schemeCategory10);
const donutWidth = width * .2;

Expand Down Expand Up @@ -67,20 +68,25 @@ export default Component.extend(ChartProperties, {
}
});

path.on('click', ({data}) => {
if (click) {
click(data);
}
});

chart.selectAll('.slice')
const text = chart.selectAll('.slice')
.append("text")
.attr("fill", "#ffffff")
.style("font-size", ".8rem")
.attr('transform', d => "translate(" + createLabelArc.centroid(d) + ")")
.attr("dy", ".40rem")
.attr("text-anchor", "middle")
.text(d => d.data.label);

if (isClickable) {
path.on('click', data => {
click(data);
});
path.style("cursor", "pointer");
text.on('click', data => {
click(data);
});
text.style("cursor", "pointer");
}
}
},
});
20 changes: 13 additions & 7 deletions addon/components/simple-chart-horz-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default Component.extend(ChartProperties, {
const hover = get(this, 'hover');
const leave = get(this, 'leave');
const click = get(this, 'click');
const isClickable = get(this, 'isClickable');
const dataOrArray = data?data:[{data: 1, label: '', empty: true}];
const svg = select(this.element);
const color = scaleOrdinal(schemeCategory10);
Expand Down Expand Up @@ -57,13 +58,7 @@ export default Component.extend(ChartProperties, {
}
});

rect.on('click', data => {
if (click) {
click(data);
}
});

bars.selectAll('text').data(dataOrArray).enter()
const text = bars.selectAll('text').data(dataOrArray).enter()
.append("text")
.attr("fill", "#ffffff")
.style("font-size", ".8rem")
Expand All @@ -73,6 +68,17 @@ export default Component.extend(ChartProperties, {
.attr('y', d => `${yScale(d.label) + (yScale.bandwidth() / 2)}%`)
.attr('x', d => `${xScale(d.data) - 3}%`)
.text(d => d.label);

if (isClickable) {
rect.on('click', data => {
click(data);
});
rect.style("cursor", "pointer");
text.on('click', data => {
click(data);
});
text.style("cursor", "pointer");
}
}
},
});
20 changes: 13 additions & 7 deletions addon/components/simple-chart-pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default Component.extend(ChartProperties, {
const isIcon = get(this, 'isIcon');
const leave = get(this, 'leave');
const click = get(this, 'click');
const isClickable = get(this, 'isClickable');
const color = scaleOrdinal(schemeCategory10);

let createArc = arc().innerRadius(0).outerRadius(radius);
Expand Down Expand Up @@ -66,20 +67,25 @@ export default Component.extend(ChartProperties, {
}
});

path.on('click', ({data}) => {
if (click) {
click(data);
}
});

chart.selectAll('.slice')
const text = chart.selectAll('.slice')
.append("text")
.attr("fill", "#ffffff")
.style("font-size", ".8rem")
.attr('transform', d => "translate(" + createLabelArc.centroid(d) + ")")
.attr("dy", ".40rem")
.attr("text-anchor", "middle")
.text(d => d.data.label);

if (isClickable) {
path.on('click', data => {
click(data);
});
path.style("cursor", "pointer");
text.on('click', data => {
click(data);
});
text.style("cursor", "pointer");
}
}
}
});
5 changes: 5 additions & 0 deletions addon/components/simple-chart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Component from '@ember/component';
import { set, get, computed } from '@ember/object';
import { isPresent } from '@ember/utils';
import layout from '../templates/components/simple-chart';

export default Component.extend({
Expand All @@ -13,6 +14,10 @@ export default Component.extend({
const name = this.get('name');
return `simple-chart-${name}`;
}),
isClickable: computed('click', function () {
const click = get(this, 'click');
return isPresent(click);
}),
actions: {
async handleHover(data, tooltipTarget) {
const hover = get(this, 'hover');
Expand Down
1 change: 1 addition & 0 deletions addon/mixins/chart-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default Mixin.create(ResizeAware, {
data: null,
isIcon: false,
resizeListener: null,
isClickable: false,
didReceiveAttrs() {
// Anytime we get an update schedule a draw
run.scheduleOnce('afterRender', this, this.doDraw);
Expand Down
1 change: 1 addition & 0 deletions addon/templates/components/simple-chart.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
hover=(action 'handleHover')
click=(action 'handleClick')
leave=(action 'handleLeave')
isClickable=isClickable
}}

{{#if tooltipTarget}}
Expand Down