Wordcloud of Lorem Ipsum
by javiros
HTML
<div id="container"></div>
<script src="https://code.highcharts.com/highcharts.src.js"></script>
<script>
/**
* @license Highcharts JS v5.0.12-modified (2017-08-04)
*
* (c) 2016 Highsoft AS
* Authors: Jon Arild Nygard
*
* License: www.highcharts.com/license
*/
'use strict';
(function(factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory;
} else {
factory(Highcharts);
}
}(function(Highcharts) {
(function(H) {
/**
* (c) 2016 Highsoft AS
* Authors: Jon Arild Nygard
*
* License: www.highcharts.com/license
*
* This is an experimental Highcharts module which enables visualization
* of a word cloud.
*/
var each = H.each,
extend = H.extend,
Series = H.Series;
/**
* isRectanglesIntersecting - Detects if there is a collision between two
* rectangles.
*
* @param {object} r1 First rectangle.
* @param {object} r2 Second rectangle.
* @return {boolean} Returns true if the rectangles overlap.
*/
var isRectanglesIntersecting = function isRectanglesIntersecting(r1, r2) {
return !(
r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top
);
};
/**
* intersectsAnyWord - Detects if a word collides with any previously placed
* words.
*
* @param {Point} point Point which the word is connected to.
* @param {Array} points Previously placed points to check against.
* @return {boolean} Returns true if there is collision.
*/
var intersectsAnyWord = function intersectsAnyWord(point, points) {
var intersects = false,
rect1 = point.rect,
rect2;
if...
JavaScript
(function (H) {
var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean bibendum erat ac justo sollicitudin, quis lacinia ligula fringilla. Pellentesque hendrerit, nisi vitae posuere condimentum, lectus urna accumsan libero, rutrum commodo mi lacus pretium erat. Phasellus pretium ultrices mi sed semper. Praesent ut tristique magna. Donec nisl tellus, sagittis ut tempus sit amet, consectetur eget erat. Sed ornare gravida lacinia. Curabitur iaculis metus purus, eget pretium est laoreet ut. Quisque tristique augue ac eros malesuada, vitae facilisis mauris sollicitudin. Mauris ac molestie nulla, vitae facilisis quam. Curabitur placerat ornare sem, in mattis purus posuere eget. Praesent non condimentum odio. Nunc aliquet, odio nec auctor congue, sapien justo dictum massa, nec fermentum massa sapien non tellus. Praesent luctus eros et nunc pretium hendrerit. In consequat et eros nec interdum. Ut neque dui, maximus id elit ac, consequat pretium tellus. Nullam vel accumsan lorem.';
var data = text
.split(',').join('') // remove commas
.split('.').join('') // remove periods
.split(' ') // split into words
.reduce(function (arr, word) {
var obj = arr.find(function (obj) {
return obj.name === word;
});
if (obj) {
obj.weight += 1;
} else {
obj = {
name: word,
weight: 1
};
arr.push(obj);
}
return arr;
}, []);
H.chart('container', {
chart: {
width: 600,
height: 600,
plotBorderWidth: 1
},
series: [{
type: 'wordcloud',
data: data
}],
title: {
text: 'Wordcloud of Lorem Ipsum'
}
});
}(Highcharts));