GithubHelp home page GithubHelp logo

vonwenm / angular-d3-word-cloud Goto Github PK

View Code? Open in Web Editor NEW

This project forked from weihanchen/angular-d3-word-cloud

0.0 2.0 0.0 1.48 MB

angular directive of D3 word cloud plugin, include simple options to binding.

Home Page: https://weihanchen.github.io/angular-d3-word-cloud/

JavaScript 92.71% Shell 7.29%

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]/jquery/dist/jquery.min.js"></script>
<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

  • words=[array] -> [{text: '',size: 0}]
  • height=[number]
  • width=[number]
  • padding=[string] -> [optional] padding for each word, defaults to 5
  • on-click=[function] -> word clicked callback

Directive Usage

<div id="wordsCloud">
   <word-cloud words="appCtrl.words" width="appCtrl.width" height="appCtrl.height" padding="5" 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.words = [
			{text: 'Angular',size: 25},
			{text: 'Angular2',size: 35}
		]

		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 = $element.find('#wordsCloud');
     var height = $window.innerHeight * 0.75;
     element.height(height);
     var width = element[0].offsetWidth;
     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))
         }
     })
     self.width = width;
     self.height = height;

References

angular-d3-word-cloud's People

Contributors

weihanchen avatar

Watchers

 avatar  avatar

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.