|
16 | 16 | <script src='https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.0.1/spin.min.js'></script> |
17 | 17 | </head> |
18 | 18 | <body> |
19 | | -<section class="timeline" id = "WordcloudImg"></section > |
| 19 | +<section class="timeline" id="WordcloudImg"></section> |
20 | 20 | <script> |
21 | | - var categories = ["person","location","organization","miscellaneous"]; |
| 21 | + var categories = ["person", "location", "organization", "miscellaneous"]; |
| 22 | + var color = d3.scaleOrdinal(d3.schemeCategory10); |
22 | 23 |
|
23 | | - d3.json("../data/wcloudHuff.json", function(error, data){ |
| 24 | + d3.json("../data/wcloudHuff.json", function (error, data) { |
24 | 25 | drawWordCloud(data); |
25 | 26 | }); |
26 | 27 |
|
| 28 | + function getColor(topic) { |
| 29 | + if (topic === "person") return color(0); |
| 30 | + else if (topic === "location") return color(1); |
| 31 | + else if (topic === "organization") return color(2); |
| 32 | + else if (topic === "miscellaneous") return color(3); |
| 33 | + } |
| 34 | + |
| 35 | + function drawWordCloud(words) { |
27 | 36 |
|
28 | | - function drawWordCloud(words){ |
29 | | - var color = d3.scale.category10(); |
30 | | - var font ="Arial"; |
| 37 | + var font = "arial"; |
31 | 38 | var word_count = {}; |
32 | | - words.forEach(date=> { |
33 | | - categories.forEach((c,i) =>{ |
34 | | - date.words[c].slice(0,2).forEach(w=> word_count[w.text] = {sudden: w.sudden, category: [c,i]})}) |
| 39 | + words.forEach(date => { |
| 40 | + categories.forEach((c, i) => { |
| 41 | + date.words[c].slice(0, 2).forEach(w => word_count[w.text] = {sudden: w.sudden, category: [c, i]}) |
| 42 | + }) |
35 | 43 | }); |
36 | 44 | var margin = {top: 0, right: 0, bottom: 0, left: 0}; |
37 | 45 | var width = 1400; |
38 | | - var height = 800; |
| 46 | + var height = 900; |
39 | 47 | d3.select("#WordcloudImg").select('svg').remove(); |
40 | 48 | d3.select("#WordcloudImg") |
41 | 49 | .append('svg') |
42 | 50 | .attr("width", width) |
43 | 51 | .attr("height", height) |
44 | 52 | .append("g") |
45 | | - .attr("id","wordchart") |
| 53 | + .attr("id", "wordchart") |
46 | 54 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
47 | 55 | var svg_location = "#wordchart"; |
48 | 56 |
|
49 | | - |
50 | | - // var fill = color; |
51 | | - |
52 | 57 | var word_entries = d3.entries(word_count); |
53 | 58 |
|
54 | 59 | var xScale = d3.scaleLinear() |
55 | | - .domain([0, d3.max(word_entries, function(d) { |
| 60 | + .domain([0, d3.max(word_entries, function (d) { |
56 | 61 | return d.value.sudden; |
57 | 62 | }) |
58 | 63 | ]) |
59 | | - .range([26,50]); |
| 64 | + .range([26, 60]); |
60 | 65 |
|
61 | 66 | var opacity = d3.scaleSqrt() |
62 | | - .domain([0, d3.max(word_entries, function(d) { |
| 67 | + .domain([0, d3.max(word_entries, function (d) { |
63 | 68 | return d.value.sudden; |
64 | 69 | }) |
65 | 70 | ]) |
66 | | - .range([0.4,1]); |
| 71 | + .range([0.4, 1]); |
67 | 72 |
|
68 | 73 | d3.layout.cloud().size([width, height]) |
69 | 74 | .timeInterval(20) |
70 | 75 | .words(word_entries) |
71 | | - .fontSize(function(d) { return xScale(d.value.sudden); }) |
72 | | - .text(function(d) { return d.key; }) |
| 76 | + .fontSize(function (d) { |
| 77 | + return xScale(d.value.sudden); |
| 78 | + }) |
| 79 | + .text(function (d) { |
| 80 | + return d.key; |
| 81 | + }) |
73 | 82 | //.rotate(function() { return (~~(Math.random() * 6) - 3) * 8 }) |
74 | 83 | .rotate(0) |
75 | 84 | .font(font) |
76 | 85 | .on("end", draw) |
77 | 86 | .start(); |
78 | 87 |
|
79 | 88 | console.log(words); |
| 89 | + |
80 | 90 | function draw(words) { |
81 | 91 | d3.select(svg_location).append("svg") |
82 | 92 | .attr("width", width) |
|
86 | 96 | .selectAll("text") |
87 | 97 | .data(words) |
88 | 98 | .enter().append("text") |
89 | | - .style("font-size", function(d) { return xScale(+d.value.sudden) + "px"; }) |
| 99 | + .style("font-size", function (d) { |
| 100 | + return xScale(+d.value.sudden) + "px"; |
| 101 | + }) |
90 | 102 | .style("font-family", font) |
91 | | - .style("fill", function(d, i) { return fill(d.value.category[1]); }) |
| 103 | + // .style("fill", function (d) { |
| 104 | + // let topic = d.value.category[0] |
| 105 | + // if (topic === "person") return color(0); |
| 106 | + // else if (topic === "location") return color(1); |
| 107 | + // else if (topic === "organization") return color(2); |
| 108 | + // else if (topic === "miscellaneous") return color(3); |
| 109 | + // }) |
92 | 110 | .style("fill", function(d, i) { return "#000" }) |
93 | | - .style("fill-opacity", function(d) { return opacity(+d.value.sudden) }) |
| 111 | + .style("fill-opacity", function (d) { |
| 112 | + return opacity(+d.value.sudden) |
| 113 | + }) |
94 | 114 | .attr("text-anchor", "middle") |
95 | | - .attr("transform", function(d) { |
| 115 | + .attr("transform", function (d) { |
96 | 116 | return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; |
97 | 117 | }) |
98 | | - .text(function(d) { return d.key; }); |
| 118 | + .text(function (d) { |
| 119 | + return d.key; |
| 120 | + }); |
99 | 121 | } |
100 | 122 |
|
101 | 123 | d3.layout.cloud().stop(); |
|
0 commit comments