Socket
Socket
Sign inDemoInstall

d3-cloud

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

d3-cloud

Generate word clouds in JavaScript.


Version published
Weekly downloads
149K
decreased by-8.81%
Maintainers
1
Weekly downloads
 
Created

What is d3-cloud?

The d3-cloud npm package is a layout algorithm for positioning words in a cloud, where the size of each word is determined by a specified weight. It is part of the D3.js family and is used to create word clouds, which are visual representations of text data where the importance of each word is shown with font size or color.

What are d3-cloud's main functionalities?

Basic Word Cloud

This code sample demonstrates how to create a basic word cloud using the d3-cloud package. It sets up a list of words with associated sizes, configures the layout, and draws the word cloud in an SVG element.

const d3 = require('d3');
const cloud = require('d3-cloud');

const words = [
  { text: 'Hello', size: 40 },
  { text: 'World', size: 30 },
  { text: 'D3', size: 20 },
  { text: 'Cloud', size: 50 }
];

const layout = cloud()
  .size([500, 500])
  .words(words)
  .padding(5)
  .rotate(() => ~~(Math.random() * 2) * 90)
  .font('Impact')
  .fontSize(d => d.size)
  .on('end', draw);

layout.start();

function draw(words) {
  d3.select('body').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')
    .attr('text-anchor', 'middle')
    .attr('transform', d => 'translate(' + [d.x, d.y] + ')rotate(' + d.rotate + ')')
    .text(d => d.text);
}

Custom Word Rotation

This code sample demonstrates how to customize the rotation of words in the word cloud. In this example, words longer than 4 characters are rotated by 90 degrees, while shorter words are not rotated.

const d3 = require('d3');
const cloud = require('d3-cloud');

const words = [
  { text: 'Hello', size: 40 },
  { text: 'World', size: 30 },
  { text: 'D3', size: 20 },
  { text: 'Cloud', size: 50 }
];

const layout = cloud()
  .size([500, 500])
  .words(words)
  .padding(5)
  .rotate(d => d.text.length > 4 ? 90 : 0)
  .font('Impact')
  .fontSize(d => d.size)
  .on('end', draw);

layout.start();

function draw(words) {
  d3.select('body').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')
    .attr('text-anchor', 'middle')
    .attr('transform', d => 'translate(' + [d.x, d.y] + ')rotate(' + d.rotate + ')')
    .text(d => d.text);
}

Other packages similar to d3-cloud

Keywords

FAQs

Package last updated on 07 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc