• Stars
    star
    136
  • Rank 260,040 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A word cloud react component built with d3-cloud.

react-d3-cloud

npm version Build Status

A word cloud react component built with d3-cloud.

image

Installation

npm install react-d3-cloud

Usage

Simple:

import React from 'react';
import { render } from 'react-dom';
import WordCloud from 'react-d3-cloud';

const data = [
  { text: 'Hey', value: 1000 },
  { text: 'lol', value: 200 },
  { text: 'first impression', value: 800 },
  { text: 'very cool', value: 1000000 },
  { text: 'duck', value: 10 },
];

render(<WordCloud data={data} />, document.getElementById('root'));

More configuration:

import React from 'react';
import { render } from 'react-dom';
import WordCloud from 'react-d3-cloud';
import { scaleOrdinal } from 'd3-scale';
import { schemeCategory10 } from 'd3-scale-chromatic';

const data = [
  { text: 'Hey', value: 1000 },
  { text: 'lol', value: 200 },
  { text: 'first impression', value: 800 },
  { text: 'very cool', value: 1000000 },
  { text: 'duck', value: 10 },
];

const schemeCategory10ScaleOrdinal = scaleOrdinal(schemeCategory10);

render(
  <WordCloud
    data={data}
    width={500}
    height={500}
    font="Times"
    fontStyle="italic"
    fontWeight="bold"
    fontSize={(word) => Math.log2(word.value) * 5}
    spiral="rectangular"
    rotate={(word) => word.value % 360}
    padding={5}
    random={Math.random}
    fill={(d, i) => schemeCategory10ScaleOrdinal(i)}
    onWordClick={(event, d) => {
      console.log(`onWordClick: ${d.text}`);
    }}
    onWordMouseOver={(event, d) => {
      console.log(`onWordMouseOver: ${d.text}`);
    }}
    onWordMouseOut={(event, d) => {
      console.log(`onWordMouseOut: ${d.text}`);
    }}
  />,
  document.getElementById('root')
);

Please checkout demo

for more detailed props, please refer to below:

Props

name description type required default
data The words array { text: string, value: number }>[]
width Width of the layout (px) number 700
height Height of the layout (px) number 600
font The font accessor function, which indicates the font face for each word. A constant may be specified instead of a function. string | (d) => string 'serif'
fontStyle The fontStyle accessor function, which indicates the font style for each word. A constant may be specified instead of a function. string | (d) => string 'normal'
fontWeight The fontWeight accessor function, which indicates the font weight for each word. A constant may be specified instead of a function. string | number | (d) => string | number 'normal'
fontSize The fontSize accessor function, which indicates the numerical font size for each word. (d) => number (d) => Math.sqrt(d.value)
rotate The rotate accessor function, which indicates the rotation angle (in degrees) for each word. (d) => number () => (~~(Math.random() * 6) - 3) * 30
spiral The current type of spiral used for positioning words. This can either be one of the two built-in spirals, "archimedean" and "rectangular", or an arbitrary spiral generator can be used 'archimedean' | 'rectangular' | ([width, height]) => t => [x, y] 'archimedean'
padding The padding accessor function, which indicates the numerical padding for each word. number | (d) => number 1
random The internal random number generator, used for selecting the initial position of each word, and the clockwise/counterclockwise direction of the spiral for each word. This should return a number in the range [0, 1). (d) => number Math.random
fill The fill accessor function, which indicates the color for each word. (d, i) => string (d, i) => schemeCategory10ScaleOrdinal(i)
onWordClick The function will be called when click event is triggered on a word (event, d) => {} null
onWordMouseOver The function will be called when mouseover event is triggered on a word (event, d) => {} null
onWordMouseOut The function will be called when mouseout event is triggered on a word (event, d) => {} null

FAQ

How to Use with Next.js/SSR

To make <WordCloud /> work with Server-Side Rendering (SSR), you need to avoid rendering it on the server:

{
  typeof window !== 'undefiend' && <WordCloud data={data} />;
}

How to Avoid Unnecessary Re-render

As of version 0.10.1, <WordCloud /> has been wrapped by React.memo() and deep equal comparison under the hood to avoid unnecessary re-render. All you need to do is to make your function props deep equal comparable using useCallback():

import React, { useCallback } from 'react';
import { render } from 'react-dom';
import WordCloud from 'react-d3-cloud';
import { scaleOrdinal } from 'd3-scale';
import { schemeCategory10 } from 'd3-scale-chromatic';

function App() {
  const data = [
    { text: 'Hey', value: 1000 },
    { text: 'lol', value: 200 },
    { text: 'first impression', value: 800 },
    { text: 'very cool', value: 1000000 },
    { text: 'duck', value: 10 },
  ];

  const fontSize = useCallback((word) => Math.log2(word.value) * 5, []);
  const rotate = useCallback((word) => word.value % 360, []);
  const fill = useCallback((d, i) => scaleOrdinal(schemeCategory10)(i), []);
  const onWordClick = useCallback((word) => {
    console.log(`onWordClick: ${word}`);
  }, []);
  const onWordMouseOver = useCallback((word) => {
    console.log(`onWordMouseOver: ${word}`);
  }, []);
  const onWordMouseOut = useCallback((word) => {
    console.log(`onWordMouseOut: ${word}`);
  }, []);

  return (
    <WordCloud
      data={data}
      width={500}
      height={500}
      font="Times"
      fontStyle="italic"
      fontWeight="bold"
      fontSize={fontSize}
      spiral="rectangular"
      rotate={rotate}
      padding={5}
      random={Math.random}
      fill={fill}
      onWordClick={onWordClick}
      onWordMouseOver={onWordMouseOver}
      onWordMouseOut={onWordMouseOut}
    />
  );
);

Build

npm run build

Test

pre-install

Mac OS X

brew install pkg-config cairo pango libpng jpeg giflib librsvg
npm install

Ubuntu and Other Debian Based Systems

sudo apt-get update
sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
npm install

For more details, please check out Installation guides at node-canvas wiki.

Run Tests

npm test

License

MIT © Yoctol

More Repositories

1

bottender

⚡️ A framework for building conversational user interfaces.
TypeScript
4,166
star
2

react-messenger-customer-chat

React component for messenger customer chat plugin
JavaScript
270
star
3

seq2vec

Transform sequence of words into a fix-length representation vector
Python
68
star
4

ADEM

TOWARDS AN AUTOMATIC TURING TEST: LEARNING TO EVALUATE DIALOGUE RESPONSES
Python
27
star
5

fetch-graphql-schema

fetch GraphQL schema via introspection query
JavaScript
27
star
6

leader-of-lunch

LOL
TypeScript
19
star
7

yoctol-keras-layer-zoo

Some customized keras layers used in Yoctol NLU.
Python
19
star
8

react-messenger-send-to-messenger

React component for messenger send to messenger plugin
JavaScript
17
star
9

text-normalizer

Normalize text string
Python
13
star
10

react-messenger-message-us

React component for messenger message us plugin
JavaScript
10
star
11

tailor-ui

A bespoke UI collection for building web application.
TypeScript
10
star
12

yoctol-nlu-py

Yoctol understood SDK for python.
Jupyter Notebook
10
star
13

react-messenger-checkbox

React component for messenger checkbox plugin
JavaScript
10
star
14

HRED

Hierarchical Encoder Decoder RNN
Python
8
star
15

serving-utils

Some utilities for tensorflow serving
Python
8
star
16

agricola-score

農家樂分數計算機
JavaScript
7
star
17

ansible-docker-postgres

Ansible role to run postgres container.
6
star
18

tw-stock-info-bot

JavaScript
5
star
19

bookshelf-test-utils

JavaScript
5
star
20

bottender-google-sheet-sync-demo

a demo of google sheets api on bottender
JavaScript
5
star
21

eslint-config-yoctol

Yoctol specific linting rules for ESLint
JavaScript
5
star
22

strpipe

text preprocessing pipeline
Python
5
star
23

async-update-props

JavaScript
4
star
24

us-stock-info-bot

JavaScript
4
star
25

DS-post-interview-assignment

A Post Interview Assignment for Candidates.
Python
4
star
26

stylelint-config-yoctol

Yoctol's stylelint config
JavaScript
3
star
27

purewords

Create pure sentences
Python
3
star
28

word-embedder

Get pretrained word embedding
Python
3
star
29

ansible-env-file

Role to render env file with ease.
3
star
30

bookshelf-plugin

Yoctol specific plugins for Bookshelf
JavaScript
2
star
31

talos

Neural Network Builder
Python
2
star
32

hubot-messenger-bot-adapter

A Hubot adapter for Facebook Messenger Platform
JavaScript
2
star
33

eslint-config-yoctol-base

Yoctol specific base linting rules for ESLint
JavaScript
2
star
34

yoctol-nlu-node

Yoctol Natural Language Understanding SDK for Node.js
JavaScript
2
star
35

yoctol-printer

print files using web interface
JavaScript
2
star
36

ansible-dockerfile

1
star
37

koa-final-handler

Final handler middleware for koa.
JavaScript
1
star
38

uttut

Utterance utilities.
Python
1
star
39

liff-test

Using Bottender to test behavior of LIFF in any situation.
JavaScript
1
star
40

react-paperjs-experiment

JavaScript
1
star
41

messenger-batch

Gracefully batching messenger requests.
JavaScript
1
star
42

flake8-config-yoctol

Yoctol Flake8 config
Python
1
star
43

ddenv

Debug Friendly Environment
JavaScript
1
star
44

babel-preset-yoctol

Yoctol specific configuration for Babel
JavaScript
1
star