2016年12月20日 星期二

Word Cloud 文字雲 - jQCloud & WordCloud2

Sample Step:

1.Download word cloud jquery:
http://mistic100.github.io/jQCloud/#usage



2.Include package & jQuery CDN
- jqcloud.min.js- jqcloud.min.css
- jqcloud.min.js- jqcloud.min.js
- https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js

- 程式碼
  1. <head>
  2. <meta charset="utf-8">
  3. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
  4. <script src="jqcloud.min.js"></script>
  5. <link rel="stylesheet" href="jqcloud.min.css">
  6. <title>test</title>
  7. </head>

3.設定『資料』 & 啟用文字雲
- 要記得給文字雲『範圍大小』,否則畫面上看不到
  1. $('#keywords').jQCloud(words, {
  2. width: 500,
  3. height: 350
  4. });

- 設定假資料
  1. <script>
  2. $(function(){
  3. var words = [
  4. {text: "Lorem", weight: 13},
  5. {text: "Ipsum", weight: 10.5},
  6. {text: "Dolor", weight: 9.4},
  7. {text: "Sit", weight: 8},
  8. {text: "Amet", weight: 6.2},
  9. {text: "Consectetur", weight: 5},
  10. {text: "Adipiscing", weight: 5},
  11. ];
  12.  
  13. <!-- $('#demo').jQCloud(words); -->
  14. $('#demo').jQCloud(words, {
  15. width: 600,
  16. height: 600
  17. });
  18. });
  19. </script>

4.Result



5.Sample code
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
  6. <script src="jqcloud.min.js"></script>
  7. <link rel="stylesheet" href="jqcloud.min.css">
  8. <title>test</title>
  9. </head>
  10. <script>
  11. $(function(){
  12. var words = [
  13. {text: "Lorem", weight: 13},
  14. {text: "Ipsum", weight: 10.5},
  15. {text: "Dolor", weight: 9.4},
  16. {text: "Sit", weight: 8},
  17. {text: "Amet", weight: 6.2},
  18. {text: "Consectetur", weight: 5},
  19. {text: "Adipiscing", weight: 5},
  20. ];
  21.  
  22. <!-- $('#demo').jQCloud(words); -->
  23. $('#demo').jQCloud(words, {
  24. width: 600,
  25. height: 600
  26. });
  27. });
  28.  
  29. </script>
  30. <body>
  31. <div id="demo" style="border:thin solid black">
  32. </div>
  33. </body>
  34.  
  35. </html>

6.Advance Skill 
[自訂設定]
- delay: 50 (字體顯示的速度)
- steps : Number of "steps" to map the words on, depending on their weight (大約會有幾種字體大小,會與自設的color進行比對)
- colors: 自訂顏色
-from-to: 字體最大-最小(根據container的大小)
  1. $('#wordCloud').jQCloud(words, {
  2. delay: 50,
  3. width: 500,
  4. height: 500,
  5. autoResize: true,
  6. steps : 50,
  7. //最凸顯
  8. colors: ["#CC0066", "#CC0066", "#009999", "#009999", "#FFCC33", "#FFCC33", "#0099CC", "#0099CC", "#FF6666","#FF6666",
  9. "#FF33CC", "#FF33CC", "#FF6600", "#FF6600", "#009966", "#009966", "#CC6633", "#CC6633", "#FF6666","#FF6666",
  10. "#555555", "#555555", "#555555", "#555555", "#555555", "#444444", "#444444", "#444444", "#444444","#444444",
  11. "#666633", "#666633", "#666633", "#666633", "#666699", "#666666", "#666666", "#666666", "#666666","#666666",
  12. "#333333", "#333333", "#333333", "#333333", "#333333", "#999999", "#999999", "#999999", "#999999","#999999"
  13. ],
  14. fontSize: {
  15. from: 0.08,
  16. to: 0.02
  17. }
  18. });

[文字雲特殊效果(字體隨滑鼠移出移入改變)]
  1. //文字雲特殊效果
  2. $(document).on('mouseenter','.jqcloud-word',function(){
  3. var font_size = parseFloat($(this).css("font-size"));
  4. var newSize = font_size + 10;
  5. $(this).css("font-size",newSize + 'px');
  6. }).on('mouseout','.jqcloud-word',function(){
  7. var font_size = parseFloat($(this).css("font-size"));
  8. var newSize = font_size - 10;
  9. $(this).css("font-size",newSize + 'px');
  10. });

[使用者可調整"字體顏色"、文字雲型狀]

1.按下"更新"
2.刪掉文字雲
3.重建文字雲
  1. //delete
  2. wordCloud.jQCloud('destroy');
  3. //update word cloud
  4. wordCloud.jQCloud(words, {
  5. delay: 40,
  6. width: width,
  7. height: height,
  8. autoResize: true,
  9. steps : 50,
  10. colors: colorStyle,
  11. shape: shape,
  12. fontSize: {
  13. from: 0.08,
  14. to: 0.02
  15. }
  16. });

4.sample code
  1. //文字雲update
  2. $('#updateButton').on('click', function() {
  3. var wordCloud = $('#wordCloud');
  4. var selectColorStyle = $('#colorStyle').val();
  5. var selectShape = $('#shape').val();
  6. var colorStyle;
  7. //custom color style
  8. if (selectColorStyle == '1') {
  9. colorStyle = ["#CC0066", "#CC0066", "#009999", "#009999", "#FFCC33", "#FFCC33", "#0099CC", "#0099CC", "#FF6666","#FF6666",
  10. "#FF33CC", "#FF33CC", "#FF6600", "#FF6600", "#009966", "#009966", "#CC6633", "#CC6633", "#FF6666","#FF6666",
  11. "#555555", "#555555", "#555555", "#555555", "#555555", "#444444", "#444444", "#444444", "#444444","#444444",
  12. "#666633", "#666633", "#666633", "#666633", "#666699", "#666666", "#666666", "#666666", "#666666","#666666",
  13. "#333333", "#333333", "#333333", "#333333", "#333333", "#999999", "#999999", "#999999", "#999999","#999999"
  14. ];
  15. } else if (selectColorStyle == '2') {
  16. colorStyle = ["#CC0066", "#CC0066", "#009999", "#009999", "#FFCC33", "#FFCC33", "#0099CC", "#0099CC", "#FF6666","#FF6666",
  17. "#FF33CC", "#FF33CC", "#FF6600", "#FF6600", "#009966", "#009966", "#CC6633", "#CC6633", "#FF6666","#FF6666",
  18. "#3399CC", "#3399CC", "#CC6600", "#CC6600", "#999999", "#444444", "#444444", "#444444", "#444444","#444444",
  19. "#666633", "#666633", "#666633", "#666633", "#666699", "#666666", "#666666", "#666666", "#666666","#666666",
  20. "#333333", "#333333", "#333333", "#333333", "#333333", "#999999", "#999999", "#999999", "#999999","#999999"
  21. ];
  22. } else if (selectColorStyle == '3') {
  23. colorStyle = ["#CC0066", "#CC0066", "#009999", "#009999", "#FFCC33", "#FFCC33", "#0099CC", "#0099CC", "#FF6666","#FF6666",
  24. "#FF33CC", "#FF33CC", "#FF6600", "#FF6600", "#009966", "#009966", "#CC6633", "#CC6633", "#FF6666","#FF6666",
  25. "#3399CC", "#3399CC", "#CC6600", "#CC6600", "#999999", "#999999", "#CCCC33", "#CCCC33", "#CCCC33","#CCCC33",
  26. "#FF9933", "#FF9933", "#CC9999", "#CC9999", "#666699", "#666699", "#FF9900", "#FF9900", "#0099CC","#0099CC",
  27. "#CCCC99", "#CCCC99", "#FF6666", "#FF6666", "#FFCC99", "#FFCC99", "#CC3399", "#CC3399", "#99CC33","#99CC33"
  28. ];
  29. }
  30. //custom shape
  31. var width;
  32. var height;
  33. var shape;
  34. if (selectShape == '1') {
  35. width = 550;
  36. height = 400;
  37. shape = 'elliptic';
  38. } else if (selectShape == '2') {
  39. width = 500;
  40. height = 500;
  41. shape = 'elliptic';
  42. } else {
  43. width = 550;
  44. height = 400;
  45. shape = 'rectangular';
  46. }
  47. //delete
  48. wordCloud.jQCloud('destroy');
  49. //update word cloud
  50. wordCloud.jQCloud(words, {
  51. delay: 40,
  52. width: width,
  53. height: height,
  54. autoResize: true,
  55. steps : 50,
  56. colors: colorStyle,
  57. shape: shape,
  58. fontSize: {
  59. from: 0.08,
  60. to: 0.02
  61. }
  62. });
  63. });

Word Cloud 2

1.下載:
https://timdream.org/wordcloud2.js/#red-chamber

2.Sample Code:
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
  6. <script src="wordcloud2.js"></script>
  7. <title>test</title>
  8. </head>
  9. <script>
  10. $(function(){
  11. var words = [
  12. ["哈哈哈哈",60],
  13. ["Ipsum",19],
  14. ["Dolor",18],
  15. ["Sit",17],
  16. ["Amet",16],
  17. ["Consectetur",15],
  18. ["Adipiscing",14],
  19. ["dsfsf",13],
  20. ["Ipsdfsum",12],
  21. ["Dosdfsdflor",11],
  22. ["台灣",10],
  23. ["Ipsum",9],
  24. ["Dolor",8],
  25. ["Sit",7],
  26. ["Amet",6],
  27. ["Consectetur",5],
  28. ["Adipiscing",4],
  29. ["dsfsf",3],
  30. ["Ipsdfsum",2],
  31. ["Dosdfsdflor",1],
  32. ["台灣",25],
  33. ["Ipsum",24],
  34. ["Dolor",23],
  35. ["Sit",22],
  36. ["Amet",21],
  37. ];
  38.  
  39. WordCloud($('#wordCloud')[0], {
  40. list: words,
  41. classes: 'wordCloud',
  42. wait: 30,
  43. gridSize: 10,
  44. weightFactor: 1.6,
  45. fontFamily: 'Hiragino Mincho Pro, serif',
  46. color: 'random-dark',
  47. backgroundColor: '#FFFFFF',
  48. rotateRatio: 0
  49. });
  50. //文字雲update
  51. $('#updateButton').on('click', function() {
  52. //custom shape
  53. var selectShape = $('#shape').val();
  54. var shape;
  55. if (selectShape == '1') {
  56. shape = 'circle';
  57. } else if (selectShape == '2') {
  58. shape = 'star';
  59. } else if (selectShape == '3') {
  60. shape = 'pentagon';
  61. } else if (selectShape == '4'){
  62. shape = 'diamond';
  63. } else if (selectShape == '5') {
  64. shape = 'triangle';
  65. }
  66. WordCloud($('#wordCloud')[0], {
  67. list: words,
  68. classes: 'wordCloud',
  69. wait: 30,
  70. shape: shape,
  71. gridSize: 10,
  72. weightFactor: 1.6,
  73. fontFamily: 'Hiragino Mincho Pro, serif',
  74. color: 'random-dark',
  75. backgroundColor: '#FFFFFF',
  76. rotateRatio: 0
  77. });
  78. });
  79. //文字雲特殊效果
  80. $(document).on('mouseenter','.wordCloud',function(){
  81. var font_size = parseFloat($(this).css("font-size"));
  82. var newSize = font_size + 10;
  83. $(this).css("font-size",newSize + 'px');
  84. }).on('mouseout','.wordCloud',function(){
  85. var font_size = parseFloat($(this).css("font-size"));
  86. var newSize = font_size - 10;
  87. $(this).css("font-size",newSize + 'px');
  88. });
  89. });
  90. </script>
  91. <body>
  92. <div style="border:thin solid red;align:center">
  93. <div id="wordCloud" style="width:500px;height:500px;border:thin solid black;margin:0 auto">
  94. </div>
  95. </div>
  96. <select id="shape" class="pull-right" style="height: 27px;width: 100px;margin: 3px" type="select">
  97. <option value='1' selected="selected">雲朵</option>
  98. <option value='2' >星星</option>
  99. <option value='3'>五角大廈</option>
  100. <option value='4'>鑽石</option>
  101. <option value='5'>金字塔</option>
  102. </select>
  103. </body>
  104. <button class="" id="updateButton">Update</button>
  105.  
  106. </html>

沒有留言:

張貼留言