Skip to content

Commit 8d5a660

Browse files
committed
add bar-chart component
1 parent 826c55a commit 8d5a660

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Component from '@ember/component';
2+
import { scaleLinear, scaleBand } from 'd3-scale';
3+
import { axisLeft } from 'd3-axis';
4+
import { select, mouse } from 'd3-selection';
5+
6+
export default Component.extend({
7+
padding: 30,
8+
9+
didInsertElement() {
10+
let xAxisData = this.get('data').map(d => d.x)
11+
let yAxisData = this.get('data').map(d => d.y)
12+
const maxY = Math.max(...yAxisData)
13+
const svgHeight = this.$(this.$('svg')[0]).height() + 1
14+
const svgWidth = this.$(this.$('svg')[0]).width()
15+
const padding = this.get('padding')
16+
17+
const yScale = scaleLinear()
18+
.domain([0, maxY + 5])
19+
.range([svgHeight, 0])
20+
21+
const xScale = scaleBand()
22+
.domain(this.get('data').map(d => d.x))
23+
.range([padding, svgWidth])
24+
.paddingInner(0.12)
25+
26+
const color = scaleLinear()
27+
.domain([0, Math.max(...yAxisData)])
28+
.range(['#f9d1a1', '#fd8a00'])
29+
30+
const svg = select(this.$('svg')[0])
31+
32+
const yAxis = axisLeft(yScale)
33+
34+
svg.append('g')
35+
.call(yAxis)
36+
.attr('transform', 'translate(30, 0)')
37+
38+
var tooltip = select('.tooltip')
39+
.style('opacity', 0);
40+
41+
const mouseover = (event, d) => {
42+
tooltip.style("opacity", 1);
43+
};
44+
45+
const mouseleave = (event, d) => {
46+
tooltip.style('opacity', 0);
47+
}
48+
49+
const mousemove = (data, i, objs, event) => {
50+
const [pointerX, pointerY] = mouse(objs[i])
51+
tooltip.html(`${data.x}: ${Math.round(data.y)}`)
52+
.style("left", (pointerX + 10) + "px")
53+
.style("top", (pointerY - 20) + "px");
54+
};
55+
56+
svg.selectAll('rect').data(this.get('data'))
57+
.enter()
58+
.append('rect')
59+
.attr('width', xScale.bandwidth())
60+
.attr('height', data => yScale(data.y))
61+
.attr('x', data => xScale(data.x))
62+
.attr('y', data => yScale(data.y))
63+
.attr('fill', data => color(data.y))
64+
.on('click', data => {
65+
this.set('selectedDataPoint', data)
66+
})
67+
.on("mousemove", mousemove)
68+
.on("mouseleave", mouseleave)
69+
.on("mouseover", mouseover);
70+
}
71+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="chart-container position-relative">
2+
<div class="tooltip"></div>
3+
<svg style="height:350px; width:100%; position:relative;">
4+
<g class="tooltip-area">
5+
<text class="tooltip-area__text"></text>
6+
</g>
7+
</svg>
8+
</div>

app/styles/app.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ body{
209209
max-height: 56px;
210210
}
211211

212+
.tooltip {
213+
padding: 3px;
214+
border: solid 1px black;
215+
background: white;
216+
border-radius: 3px;
217+
display: inline-block;
218+
position: absolute;
219+
z-index: 100;
220+
font-size: 0.7em;
221+
}
222+
212223

213224
// Animations
214225

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { render } from '@ember/test-helpers';
4+
import hbs from 'htmlbars-inline-precompile';
5+
6+
module('Integration | Component | bar-chart', function(hooks) {
7+
setupRenderingTest(hooks);
8+
9+
test('it renders', async function(assert) {
10+
// Set any properties with this.set('myProperty', 'value');
11+
// Handle any actions with this.set('myAction', function(val) { ... });
12+
13+
await render(hbs`<BarChart />`);
14+
15+
assert.equal(this.element.textContent.trim(), '');
16+
17+
// Template block usage:
18+
await render(hbs`
19+
<BarChart>
20+
template block text
21+
</BarChart>
22+
`);
23+
24+
assert.equal(this.element.textContent.trim(), 'template block text');
25+
});
26+
});

0 commit comments

Comments
 (0)