D3-cloud creates a beautiful mesh of overlapping words

I am encountering an issue while trying to create a keyword cloud using d3 and d3-cloud. The problem I am facing is that the words in the cloud are overlapping, and I cannot figure out the exact reason behind it. I suspect it might be related to the fontSize parameter, but I have not been able to pinpoint the exact cause. You can view a screenshot of the issue here

Below are the methods I am using:

    private _populate(keyword: any) {
        const dataset = kw.map(function (d: any) { return { text: d.text, size: d.value }; });
        console.log(dataset);

        d3.layout.cloud()
            .size([this._width, this._height])
            .words(dataset)
            .padding(1)
            .rotate(0)
            .fontSize((d: any) => d.size / 100)
            .on('end', () => {
                this._drawWordCloud(dataset);
            })
            .start();
    }

    private _drawWordCloud(words: any) {
        this._svg
            .append("g")
                .attr("transform", "translate(" + this._width / 2 + "," + this._height / 2 + ")")
                .selectAll("text")
                .data(words)
                .enter().append("text")
                    .style("font-size", (d: any) => d.size + "px")
                    .style("fill", "#69b3a2")
                    .style("font-family", "Impact")
                    .attr("text-anchor", "middle")
                    .attr("transform", (d: any) => "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")")
                    .text((d: any) => d.text);
    }

.fontSize((d: any) => d.size / 100) is used because my values are quite high (~ 10000).

If anyone could provide some guidance or assistance on resolving this issue, it would be greatly appreciated. I have tried a few solutions without success.

Thank you!

Answer №1

After much searching, I finally uncovered the solution. The key is to include the desired font in the initial method "_populate". For example, in my situation:

.font("Impact")

Furthermore, it's important not to alter the line in the second method:

.style("font-family", "Impact")

And that's all there is to it! Hopefully, this tip will prove useful to others.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unlock the Power of TWBS Ratchet: Manually Closing Modal Windows

Currently, I am in the process of developing a mobile web application using Ratchet. The main task at hand involves opening a modal, filling out a form, clicking a button to save the input data, and then closing the modal. Although I have managed to close ...

Customize specific styles for individual divs one at a time (without the use of jQuery)

Can you assist me with this issue? I am trying to display the <span class="badge badge-light"><i class="fa fa-check-circle"></i></span> tag (initially set to "visibility: hidden") when clicking on a div with the class "role-box". He ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

How can I implement a feature in Angular where clicking the edit button loads a form (in a separate component) pre-populated with previous data, along with an update button for

Within the employee-list component, there is a table displaying a list of employees. This table includes an option to edit details. <button type="button" class="btn btn-primary" routerLink="../create-employee">Edit</b ...

Ensure exclusive access to variables for sequential updates in jQuery or JavaScript

I am currently developing a series of small functions in jQuery aimed at tracking how long it takes for a user to complete a form. The form consists of various input types and 'continue' buttons located within the form itself. Below are some sa ...

Error: The react.js application is unable to access the property 'classList' of a null object

I've encountered a bug that's been causing me some trouble. Every time I try to run my react application, I keep getting an error message that reads TypeError: Cannot read property 'classList' of null. As someone who is new to react, I& ...

Guide to adding information to a file in Nodejs depending on a condition

Looking for assistance on how to append an annotation (@Circuit(name = backendB)) to a file if the "createEvent" name exists and the annotation is not already present. I am unsure of the process, so any help on checking and appending using streams would ...

Utilizing References in React Components

One of the challenges I am facing involves a Container that needs references to some of its child components: const Container = () => { const blocks: HTMLDivElement[] = []; return ( <div> <Navigation currentBlock={currentBlock} ...

RxJS Transformation is failing to return the updated object

In my Angular 5.1 single page application, I am encountering an issue with a service response while calling REST services. The problem lies in how the response is handled when it returns an array of Events. Here is how I am trying to transform the response ...

How to selectively display specific columns when outputting JSON to a dynamic HTML table?

I'm looking to output specific JSON data in an HTML table with JavaScript. The headers will remain the same, but the JSON content will vary. Currently, I have a working function that generates the entire table using JavaScript: <body> ...

Stop Jade from collapsing the directory hierarchy

When it comes to implementing a build solution using NPM scripts instead of Gulp or Grunt, I have been facing some challenges in managing multiple Jade files efficiently. I've referred to resources like and for guidance. The Jade CLI allows for com ...

Avoiding the triggering of jQuery events from child elements

Here is the code snippet I am working with: <html> <head> <script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"> </script> </head> <body> <div id=&q ...

Can a function be called when using ng-options with AngularJS select?

Here is some HTML code <select ng-model="selectedMarker" ng-options="shape.text for shape in Selects('shapes')"> </select> And below is the JavaScript code angular.module('todo', ['ionic']) . ...

Retrieve the selected option from the dropdown menu in the specified form

What should I do if I have numerous products and want users to be able to add new dropdown boxes dynamically? When the submit button is clicked, only the value of "category[]" within the form should be retrieved. https://i.stack.imgur.com/v1fnd.png Below ...

Executing a particular end-to-end test case using Angular 2, Karma, and Protractor

Is there a specific command I can use to run an individual e2e test case from my test suite? If that's not possible, is there some workaround for running a specific test suite? I'm currently using Jasmine, Karma, and Protractor. To start my tes ...

NodeJS can be used to convert JSON data into an XLSX file format and allow for

I am currently working on a project in nodejs where I need to convert JSON data into XLSX format and then download it to the client's browser. I have been using the XLSX npm module to successfully convert the JSON data into a Workbook, however, I am f ...

The object instance is failing to receive the Angular Injected service assignment

I am currently utilizing Angular6 and have the following code: 'use strict'; import {ChangeDetectorRef, Component, OnInit} from '@angular/core'; import {MainService} from "./services/main.service"; import {AppService} from "../../app. ...

Using Primeng to implement pagination and lazy loading in a dataView

Looking for a way to search through product data with filters and display it using primeng dataview? Consider implementing pagination in your API that pulls products from the database page by page. Set the products array as the value in dataview so that wh ...

What is the procedure for obtaining the corner coordinates of a boundary box in Cesium, expressed in CRS

I am currently developing a project where I am utilizing Cesium to display WMS layers on a map in 2D. To enhance performance, I have opted to use the SingleTileImageryProvider to request only one tile at a time. However, I've encountered an issue whe ...

The execution of *ngSwitchCase in Angular seems to be faulty

I am new to Angular and currently working with the ngSwitch directive in my program. However, I have encountered an issue where *ngSwitchDefault is executing instead of *ngSwitchCase every time the program runs. Below is the code snippet that I am using: ...