2016-08-22 16:21:30 -04:00
|
|
|
/* eslint-disable no-use-before-define */
|
|
|
|
import d3 from 'd3';
|
|
|
|
import cloudLayout from 'd3-cloud';
|
|
|
|
import { category21 } from '../javascripts/modules/colors';
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
function wordCloudChart(slice) {
|
2016-08-22 16:21:30 -04:00
|
|
|
const chart = d3.select(slice.selector);
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
function refresh() {
|
|
|
|
d3.json(slice.jsonEndpoint(), function (error, json) {
|
|
|
|
if (error !== null) {
|
2016-06-21 12:42:44 -04:00
|
|
|
slice.error(error.responseText, error);
|
2016-08-22 16:21:30 -04:00
|
|
|
return;
|
2016-03-18 02:44:58 -04:00
|
|
|
}
|
2016-08-22 16:21:30 -04:00
|
|
|
const data = json.data;
|
|
|
|
const range = [
|
2016-03-18 02:44:58 -04:00
|
|
|
json.form_data.size_from,
|
2016-07-21 00:32:20 -04:00
|
|
|
json.form_data.size_to,
|
2016-03-18 02:44:58 -04:00
|
|
|
];
|
2016-08-22 16:21:30 -04:00
|
|
|
const rotation = json.form_data.rotation;
|
|
|
|
let fRotation;
|
|
|
|
if (rotation === 'square') {
|
|
|
|
fRotation = () => ~~(Math.random() * 2) * 90;
|
|
|
|
} else if (rotation === 'flat') {
|
|
|
|
fRotation = () => 0;
|
2016-03-18 02:44:58 -04:00
|
|
|
} else {
|
2016-08-22 16:21:30 -04:00
|
|
|
fRotation = () => (~~(Math.random() * 6) - 3) * 30;
|
2016-03-18 02:44:58 -04:00
|
|
|
}
|
2016-08-22 16:21:30 -04:00
|
|
|
const size = [slice.width(), slice.height()];
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2016-08-22 16:21:30 -04:00
|
|
|
const scale = d3.scale.linear()
|
|
|
|
.range(range)
|
|
|
|
.domain(d3.extent(data, function (d) {
|
|
|
|
return d.size;
|
|
|
|
}));
|
2016-03-18 02:44:58 -04:00
|
|
|
|
|
|
|
function draw(words) {
|
2016-08-22 16:21:30 -04:00
|
|
|
chart.selectAll('*').remove();
|
2016-03-18 02:44:58 -04:00
|
|
|
|
2016-08-22 16:21:30 -04:00
|
|
|
chart.append('svg')
|
|
|
|
.attr('width', layout.size()[0])
|
|
|
|
.attr('height', layout.size()[1])
|
|
|
|
.append('g')
|
|
|
|
.attr('transform', `translate(${layout.size()[0] / 2},${layout.size()[1] / 2})`)
|
|
|
|
.selectAll('text')
|
|
|
|
.data(words)
|
|
|
|
.enter()
|
|
|
|
.append('text')
|
|
|
|
.style('font-size', (d) => d.size + 'px')
|
|
|
|
.style('font-family', 'Impact')
|
|
|
|
.style('fill', (d) => category21(d.text))
|
|
|
|
.attr('text-anchor', 'middle')
|
|
|
|
.attr('transform', (d) => `translate(${d.x}, ${d.y}) rotate(${d.rotate})`)
|
|
|
|
.text((d) => d.text);
|
2016-03-18 02:44:58 -04:00
|
|
|
}
|
2016-08-22 16:21:30 -04:00
|
|
|
|
|
|
|
const layout = cloudLayout()
|
|
|
|
.size(size)
|
|
|
|
.words(data)
|
|
|
|
.padding(5)
|
|
|
|
.rotate(fRotation)
|
|
|
|
.font('serif')
|
|
|
|
.fontSize((d) => scale(d.size))
|
|
|
|
.on('end', draw);
|
|
|
|
|
|
|
|
layout.start();
|
2016-03-16 23:25:41 -04:00
|
|
|
slice.done(json);
|
2016-03-18 02:44:58 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
render: refresh,
|
2016-07-21 00:32:20 -04:00
|
|
|
resize: refresh,
|
2016-03-18 02:44:58 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = wordCloudChart;
|