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

Random seed #160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ chart.setOption({
// NOTE disable it will lead to UI blocking when there is lots of words.
layoutAnimation: true,

// Set to any string to set the seed for the random word placement
// and get consistent results
randomSeed: null,

// Global text style
textStyle: {
fontFamily: 'sans-serif',
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"prettier": "^2.5.1",
"webpack": "^5.11.1",
"webpack-cli": "^4.3.1"
},
"dependencies": {
"seedrandom": "^3.0.5"
}
}
24 changes: 15 additions & 9 deletions src/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

import seedrandom from 'seedrandom'

// setImmediate
if (!window.setImmediate) {
window.setImmediate = (function setupSetImmediate() {
Expand Down Expand Up @@ -169,9 +171,9 @@ var getItemExtraData = function (item) {
};

// Based on http://jsfromhell.com/array/shuffle
var shuffleArray = function shuffleArray(arr) {
var shuffleArray = function shuffleArray(arr, random) {
for (var j, x, i = arr.length; i; ) {
j = Math.floor(Math.random() * i);
j = Math.floor(random() * i);
x = arr[--i];
arr[i] = arr[j];
arr[j] = x;
Expand Down Expand Up @@ -245,7 +247,9 @@ var WordCloud = function WordCloud(elements, options) {
classes: null,

hover: null,
click: null
click: null,

randomSeed: null
};

if (options) {
Expand Down Expand Up @@ -371,6 +375,8 @@ var WordCloud = function WordCloud(elements, options) {
var minRotation = Math.min(settings.maxRotation, settings.minRotation);
var rotationStep = settings.rotationStep;

var random = settings.randomSeed == null ? Math.random : seedrandom(settings.randomSeed.toString());

/* information/object available to all functions, set when start() */
var grid, // 2d array containing filling information
ngx,
Expand All @@ -386,11 +392,11 @@ var WordCloud = function WordCloud(elements, options) {
function randomHslColor(min, max) {
return (
'hsl(' +
(Math.random() * 360).toFixed() +
(random() * 360).toFixed() +
',' +
(Math.random() * 30 + 70).toFixed() +
(random() * 30 + 70).toFixed() +
'%,' +
(Math.random() * (max - min) + min).toFixed() +
(random() * (max - min) + min).toFixed() +
'%)'
);
}
Expand Down Expand Up @@ -538,15 +544,15 @@ var WordCloud = function WordCloud(elements, options) {
return 0;
}

if (Math.random() > settings.rotateRatio) {
if (random() > settings.rotateRatio) {
return 0;
}

if (rotationRange === 0) {
return minRotation;
}

return minRotation + Math.round(Math.random() * rotationRange / rotationStep) * rotationStep;
return minRotation + Math.round(random() * rotationRange / rotationStep) * rotationStep;
};

var getTextInfo = function getTextInfo(
Expand Down Expand Up @@ -1068,7 +1074,7 @@ var WordCloud = function WordCloud(elements, options) {

if (settings.shuffle) {
points = [].concat(points);
shuffleArray(points);
shuffleArray(points, random);
}

// Try to fit the words by looking at each point.
Expand Down
4 changes: 3 additions & 1 deletion src/wordCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ echarts.registerLayout(function (ecModel, api) {

shuffle: false,

shape: seriesModel.get('shape')
shape: seriesModel.get('shape'),

randomSeed: seriesModel.get('randomSeed')
});

function onWordCloudDrawn(e) {
Expand Down