Code Monkey home page Code Monkey logo

angular-d3-word-cloud's Introduction

angular-d3-word-cloud

Build Status Coverage Status

Run examples with server

$ npm install
$ npm run test
$ npm run dev //default server port is 8000
$ npm run release //build release files

Dependencies

Demo

Watch the wordcloud component in action on the demo page.

How to use

Install

bower
$ bower install angular-d3-word-cloud

angular.js, d3.js, d3.layout.cloud.js would be install as dependencies auto. If it won't for some error, install it manually.

$ bower install angular
$ bower install d3
$ bower install d3-cloud
npm
$ npm install angular-d3-word-cloud

Inject scripts

Add dependencies to the section of your index html:

<meta charset="utf-8">  <!-- it's important for d3.js -->
<script src="[bower_components/node_modules]/angular/angular.min.js"></script>
<script src="[bower_components/node_modules]/d3/d3.min.js"></script>
<script src="[bower_components/node_modules]/d3-cloud/build/d3.layout.cloud.js"></script>
<script src="[bower_components/node_modules]/angular-d3-word-cloud/dist/angular-word-cloud.min.js"></script>

Options

Note: if words element not contains color property, default will use d3 schemeCategory20

  • words=[array] -> [{text: '',size: 0, color: '#6d989e', tooltipText: ''}]
  • height=[number]
  • width=[number]
  • padding=[number] -> [optional] padding for each word, defaults to 5
  • rotate=[number, function] -> [optional] rotation for each word, default to ~~(Math.random() * 2) * 60
  • random=[function] -> [optional] default to Math.random, If specified, sets 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).
  • use-tooltip=[boolean] default tooltip template
  • use-transition=[boolean] transition with font size
  • on-click=[function] -> word clicked callback

Directive Usage

<div id="wordsCloud">
   <word-cloud words="appCtrl.words" width="appCtrl.width" height="appCtrl.height" padding="5" rotate="appCtrl.rotate" use-tooltip="appCtrl.useTooltip"  random="appCtrl.random" use-transition="appCtrl.useTransition" on-click="appCtrl.wordClicked">
   </word-cloud>
</div>

Basic usage

Inject angular-d3-word-cloud into angular module, set up some options to our controller

(function(){
	angular.module('app',['angular-d3-word-cloud'])
		.controller('appController',['$window','$element',appController])
	function appController($window,$element){
		var self = this;
		self.height = $window.innerHeight * 0.5;
		self.width = $element.find('#wordsCloud')[0].offsetWidth;
		self.wordClicked = wordClicked;
        self.rotate = rotate;
        self.useTooltip = true;
        self.useTransition = false;
		self.words = [
			{text: 'Angular',size: 25, color: '#6d989e', tooltipText: 'Angular Tooltip'},
			{text: 'Angular2',size: 35, color: '#473fa3', tooltipText: 'Angular2 Tooltip'}
        ]
        self.random = random;

        function random() {
            return 0.4; // a constant value here will ensure the word position is fixed upon each page refresh.
        }

        function rotate() {
            return ~~(Math.random() * 2) * 90;
        }

		function wordClicked(word){
			alert('text: ' + word.text + ',size: ' + word.size);
		}
	}
})()

Advanced usage

Define some content and split(/\s+/g)

	var content = 'Angular Angular2 Angular3 Express Nodejs';
	originWords = self.content.split(/\s+/g);
    originWords = originWords.map(function(word) {
        return {
            text: word,
            count: Math.floor(Math.random() * maxWordCount)
        }
     }).sort(function(a, b) {
          return b.count - a.count;
     })

Font size calculations

	 var element = document.getElementById('wordsCloud');
     var height = $window.innerHeight * 0.75;
     element.style.height = height + 'px';
     var width = element.getBoundingClientRect().width;
     var maxCount = originWords[0].count;
     var minCount = originWords[originWords.length - 1].count;
     var maxWordSize = width * 0.15;
     var minWordSize = maxWordSize / 5;
     var spread = maxCount - minCount;
     if (spread <= 0) spread = 1;
     var step = (maxWordSize - minWordSize) / spread;
     self.words = originWords.map(function(word) {
         return {
             text: word.text,
             size: Math.round(maxWordSize - ((maxCount - word.count) * step)),
             color: '#473fa3'//you can assign custom color
         }
     })
     self.width = width;
     self.height = height;

References

angular-d3-word-cloud's People

Contributors

rodolfosilva avatar weihanchen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

angular-d3-word-cloud's Issues

Rotate

Hi,

How to disable rotation ?

Thanks

Angular 2

Any chance you have an angular 2 implementation?

Fix/update bower.json

Hello, thank you for the package.

But, please, update your bower.json file. It fails to setup the lib properly since your actual *.js file is dist/angular-word-cloud.js , while the reference in bower.json points to: "main": ["dist/angular-d3-word-cloud.js"].
It took quite a while to notice this nasty small typo.

For those who unable to use the lib right now, try to use overrides in the main bower.json file of the project like this:

 "overrides": {
    "angular-d3-word-cloud": {
      "main": [
        "dist/angular-word-cloud.js"
      ]
    }
  }

Word not displayed

I have one label called "Activity Account Access Report", that has a size of 110 and it's not getting displayed. Does this happend because the string is too long for the size?

Use with Ionic (3) - 'word-cloud' is not a known element

Hello,
I want to use angular-d3-word-cloud in my Ionic3 app project but I have difficulties importing the required dependencies.
I want to use the <word-cloud> element simply in my HTML, as described in https://github.com/weihanchen/angular-d3-word-cloud#directive-usage.

  1. I started by installing the module with npm install angular-d3-word-cloud.
  2. Then I created a custom copy config in order to include the required .js files into the build folder. I did this as described here: https://zerone-consulting.blog/2017/10/10/steps-to-include-custom-cssjs-files-in-ionic-3/
  3. Then, I included the js-files in the <head> of my index.html as follows:
<script src="build/angular.js"></script>
<script src="build/d3.js"></script>
<script src="build/d3.layout.cloud.js"></script>
<script src="build/angular-word-cloud.js"></script>

Note: I used angular.js, d3.js and angular-word-cloud.js instead of angular.min.js, d3.min.js, angular-word-cloud.min.js because i could not find the respective *.min.js-versions within my node_modules-folder.

However, when launching my app I get the following error:

Error: Template parse errors:
'word-cloud' is not a known element:
1. If 'word-cloud' is an Angular component, then verify that it is part of this module.
2. If 'word-cloud' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
        <ion-card-content>
            <div id="wordsCloud">
                [ERROR ->]<word-cloud words="allMealNames" width="300px" height="200px" padding="5"
                           "): ng:///AppModule/HistoryPage.html@37:16

The HTML page where I'm using the <word-cloud> element looks like this:

<div id="wordsCloud">
    <word-cloud words="allMealNames" width="300px" height="200px" padding="5"
                         use-tooltip="true" transition="true" on-click="wordClicked()">
    </word-cloud>
</div>

Has anyone got a suggestion for solving this problem?
Thanks

Remove jquery as dependency

Overview

This is a great looking angular lib with a nice API to integrate into existing projects. The only downside is that it still depends on jquery.

Can the lib be refactored to remove the jquery dep?

Struggling to implement this into an existing AngularJS project

I've added angular-d3-word-cloud into app.js .module - that's OK, no errors

I've added the function appController block into a custom controller - that's OK, no errors

I've added the HTML into the view that's calling the controller I added function appController into - that's OK, no errors

I've added $window, $element into the controllers function but I get the error - angular.js:15018 Error: [$injector:unpr] Unknown provider: $elementProvider <- $element

I'm using custom controllers and not putting the controller code inside of app.js

maximum font size?

I notice with smaller viewports/resolutions the word/s with the largest font size/s sometimes drop off. Is there a known issue/relationship between max font size and layout size? Thanks.

PS, handy library, thanks!

Tooltips on mouseover

The library has some css effects on mouseover of the word, thats great.
Any suggestions about adding custom tooltip on mouseover of words.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.