Built with blockbuilder.org
Created
February 7, 2019 12:37
-
-
Save vadym1930/8fba8aa53107729e499be1301a8442bf to your computer and use it in GitHub Desktop.
test chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letter | frequency | |
---|---|---|
A | .08167 | |
B | .01492 | |
C | .02782 | |
D | .04253 | |
E | .12702 | |
F | .02288 | |
G | .02015 | |
H | .06094 | |
I | .06966 | |
J | .00153 | |
K | .00772 | |
L | .04025 | |
M | .02406 | |
N | .06749 | |
O | .07507 | |
P | .01929 | |
Q | .00095 | |
R | .05987 | |
S | .06327 | |
T | .09056 | |
U | .02758 | |
V | .00978 | |
W | .02360 | |
X | .00150 | |
Y | .01974 | |
Z | .00074 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
svg { width: 100%; height: 100%; margin: 20px } | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<svg> | |
</svg> | |
<script> | |
const margin = 60; | |
const width = 500 - margin * 2; | |
const height = 300 - margin * 2; | |
const sample = [ | |
{ | |
language: 'Rust', | |
value: 78.9, | |
color: '#000000' | |
}, | |
{ | |
language: 'Kotlin', | |
value: 75.1, | |
color: '#00a2ee' | |
}, | |
{ | |
language: 'Python', | |
value: 68.0, | |
color: '#fbcb39' | |
}, | |
{ | |
language: 'TypeScript', | |
value: 67.0, | |
color: '#007bc8' | |
}, | |
{ | |
language: 'Go', | |
value: 65.6, | |
color: '#65cedb' | |
}, | |
{ | |
language: 'Swift', | |
value: 65.1, | |
color: '#ff6e52' | |
}, | |
{ | |
language: 'JavaScript', | |
value: 61.9, | |
color: '#f9de3f' | |
}, | |
{ | |
language: 'C#', | |
value: 60.4, | |
color: '#5d2f8e' | |
}, | |
{ | |
language: 'F#', | |
value: 59.6, | |
color: '#008fc9' | |
}, | |
{ | |
language: 'Clojure', | |
value: 59.6, | |
color: '#507dca' | |
} | |
]; | |
const svg = d3.select('svg'); | |
const chart = svg.append('g') | |
.attr('transform', `translate(${margin}, ${margin})`); | |
const yScale = d3.scaleLinear() | |
.domain([0, 100]) | |
.range([height, 0]); | |
chart.append('g') | |
.call(d3.axisLeft(yScale)) | |
const xScale = d3.scaleBand() | |
.domain(sample.map(s => s.language)) | |
.range([0, width]) | |
.padding(.2); | |
chart.append('g') | |
.attr('transform', `translate(0, ${height})`) | |
.call(d3.axisBottom(xScale)); | |
chart.selectAll() | |
.data(sample) | |
.enter().append('rect') | |
.attr('x', s => xScale(s.language)) | |
.attr('y', s => yScale(s.value)) | |
.attr('height', s => height - yScale(s.value)) | |
.attr('width', xScale.bandwidth()); | |
chart.append('g') | |
.attr('class', 'grid') | |
.call(d3.axisLeft() | |
.scale(yScale) | |
.tickSize(-width, 0, 0) | |
.tickFormat('')); | |
svg.append('text') | |
.attr('x', -(height / 2) - margin) | |
.attr('y', margin / 2.4) | |
.attr('transform', 'rotate(-90)') | |
.attr('text-anchor', 'middle') | |
.text('Value (%)'); | |
svg.append('text') | |
.attr('x', width / 2 + margin) | |
.attr('y', 40) | |
.attr('text-anchor', 'middle') | |
.text('Most loved programming languages in 2018') | |
svg.append('text') | |
.attr('x', width / 2 + margin) | |
.attr('y', height - margin * 1.2) | |
.attr('transform', `translate(0, ${height})`) | |
.attr('text-anchor', 'middle') | |
.text('Language name') | |
svg.selectAll('rect').on('mouseenter', function (actual, i) { | |
d3.select(this) | |
.transition() | |
.duration(300) | |
.attr('opacity', 0.6) | |
.attr('x', (a) => xScale(a.language) - 5) | |
.attr('width', xScale.bandwidth() + 10) | |
const y = yScale(actual.value) | |
chart.append('line') | |
.attr('id', 'line') | |
.attr('x1', 0) | |
.attr('y1', y) | |
.attr('x2', width) | |
.attr('y2', y) | |
.attr('stroke', 'red') | |
}); | |
svg.selectAll('rect').on('mouseleave', function (actual, i){ | |
d3.selectAll('.value') | |
.attr('opacity', 1) | |
d3.select(this) | |
.transition() | |
.duration(300) | |
.attr('opacity', 1) | |
.attr('x', (a) => xScale(a.language)) | |
.attr('width', xScale.bandwidth()) | |
chart.selectAll('#line').remove() | |
chart.selectAll('.divergence').remove() | |
}) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment