-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,242 +1,142 @@ | ||
import './src/css/index.css'; | ||
// import 'nprogress/nprogress.css' | ||
import 'normalize.css'; | ||
import 'pure-css-loader/dist/css-loader.css' | ||
import './src/css/index.css'; //主要样式表 | ||
import 'normalize.css'; //css reset | ||
import 'pure-css-loader/dist/css-loader.css' //css-loading 样式 | ||
|
||
// import nprogress from "nprogress"; | ||
import d3 from "./src/js/d3.min.js" | ||
|
||
import d3 from "./src/js/d3.min.js" | ||
import cloud from "d3-cloud"; | ||
import { word } from "./src/js/arr.js" | ||
import axios from "axios" | ||
import axios from "axios"; | ||
var fill = d3.scale.category20(); | ||
var loader = d3.select('.loader'); //css loader | ||
var msg = ['服务器君tm射爆卡 (╯‵□′)╯︵┻━┻', '你快回来,把我的数据还回来(⊙ˍ⊙)', '假猪套天下第一 🐷', '把他娘的意大利....面拿出来 ?ω?', '我反手就是一个....小心心你要吗💗'] | ||
|
||
var fill = d3.scale.category20(); | ||
var loader=d3.select('.loader'); //css loader | ||
var msg=['服务器君tm射爆卡 (╯‵□′)╯︵┻━┻','你快回来,把我的数据还回来(⊙ˍ⊙)','假猪套天下第一 🐷','把他娘的意大利....面拿出来 ?ω?','我反正就是一个....小心心你要吗💗'] | ||
|
||
// window.word=word | ||
//构造词云容器svg元素 | ||
var svg = d3.select('.svg-contain').append("svg") | ||
.attr("width", 960) | ||
.attr("height", 500) | ||
.attr("transform", "translate(0,35)") | ||
.append("g") | ||
.attr("transform", "translate(480,250)"); | ||
|
||
|
||
// // Send a POST request | ||
|
||
|
||
document.querySelector(".btn-primary").onclick = function () { | ||
request(document.querySelector('.text-input').value) | ||
request(document.querySelector('.text-input').value) | ||
} | ||
|
||
document.querySelectorAll(".singer-name").forEach(ele => ele.onclick = function () { request(ele.innerHTML) }) | ||
|
||
let request = function (val) { | ||
loader.classed('is-active', true) | ||
.attr('data-curtain-text',msg[Math.floor(msg.length*Math.random())]) | ||
axios({ | ||
method: 'get', | ||
url: 'http://112.74.111.33:3000/163', | ||
params: { | ||
name: val | ||
{ | ||
if (!NodeList.prototype.forEach) { | ||
NodeList.prototype.forEach = Array.prototype.forEach; | ||
} | ||
}).then(d => {document.querySelector(".svg-contain").innerHTML='';showNewWords(myWordCloud(d.data));loader.classed('is-active', ()=>false)}) | ||
.catch(() => {loader.classed('is-active', ()=>false);alert("没有此歌手")}); | ||
document.querySelectorAll(".singer-name").forEach(ele => ele.onclick = function () { | ||
request(ele.innerHTML) | ||
}) | ||
} | ||
|
||
// let reset = function (word) { | ||
// // nprogress.start(); | ||
|
||
// cloud().size([950, 550]) | ||
// .words(word.slice(0, Math.min(word.length, 250)).map(function (d, i) { | ||
// return { text: d.key, size: d3.scale["log"]().range([10, 30])(d.value) }; | ||
// })) | ||
// .padding(5) | ||
// .rotate(function () { return (~~(Math.random() * 6) - 3) * 30; }) | ||
// .font("Impact") | ||
// .fontSize(d => d.size) | ||
// .on("end", draw) | ||
// .start(); | ||
// // nprogress.done(); | ||
|
||
// } | ||
|
||
|
||
// function draw(word) { | ||
// var svg = d3.select(".svg-contain").append("svg") | ||
// .attr("width", 960) | ||
// .attr("height", 600) | ||
// .append("g") | ||
// .attr("transform", "translate(480,280)") | ||
|
||
|
||
// var clouder = svg.selectAll("g text") | ||
// .data(word, function (d) { return d.text; }) | ||
|
||
// // .selectAll("text") | ||
// // .data(words) | ||
// // .enter().append("text") | ||
// // .style("font-size", function (d) { return d.size + "px"; }) | ||
// // .style("font-family", "Impact") | ||
// // .style("fill", function (d, i) { return fill(i); }) | ||
// // .attr("text-anchor", "middle") | ||
// // .attr("transform", function (d) { | ||
// // return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; | ||
// // }) | ||
// // .text(function (d) { return d.text; }); | ||
// function transition() { | ||
// clouder.enter() | ||
// .append("text") | ||
// .style("font-family", "Impact") | ||
// .style("fill", function (d, i) { return fill(i); }) | ||
// .attr("text-anchor", "middle") | ||
// .attr('font-size', 1) | ||
// .text(function (d) { return d.key; }); | ||
|
||
|
||
// clouder | ||
// .transition() | ||
// .duration(600) | ||
// .style("font-size", function (d) { return d.size + "px"; }) | ||
// .attr("transform", function (d) { | ||
// return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; | ||
// }) | ||
// .style("fill-opacity", 1); | ||
|
||
|
||
// //Exiting words | ||
// clouder.exit() | ||
// .transition() | ||
// .duration(200) | ||
// .style('fill-opacity', 1e-6) | ||
// .attr('font-size', 1) | ||
// .remove(); | ||
|
||
// } | ||
// return { | ||
// update(word) { | ||
// cloud().size([950, 550]) | ||
// .words(word.slice(0, Math.min(word.length, 250)).map(function (d, i) { | ||
// console.log(d) | ||
// return { text: d.key, size: d3.scale["log"]().range([10, 2000])(d.value) }; | ||
// })) | ||
// .padding(5) | ||
// .rotate(function () { return (~~(Math.random() * 6) - 3) * 30; }) | ||
// .font("Impact") | ||
// .fontSize(d => d.size) | ||
// .on("end", transition) | ||
// .start(); | ||
// } | ||
// } | ||
// } | ||
|
||
//--------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
|
||
function wordCloud(selector,words) { | ||
|
||
|
||
var fill = d3.scale.category20(); | ||
|
||
|
||
//Construct the word cloud's SVG element | ||
var svg = d3.select(selector).append("svg") | ||
.attr("width", 960) | ||
.attr("height", 500) | ||
.attr("transform", "translate(0,50)") | ||
.append("g") | ||
.attr("transform", "translate(480,250)"); | ||
|
||
|
||
|
||
|
||
//Draw the word cloud | ||
let request = function (val) { | ||
loader.classed('is-active', true) | ||
.attr('data-curtain-text', msg[Math.floor(msg.length * Math.random())]) | ||
axios({ | ||
method: 'get', | ||
url: 'https://kspider.nbsaw.com/163', | ||
params: { | ||
name: val | ||
} | ||
}).then(d => { | ||
showNewWords(wordCloud(), d.data); | ||
}) | ||
.catch(() => { | ||
loader.classed('is-active', () => false); | ||
alert("没有此歌手") | ||
}); | ||
} | ||
|
||
|
||
|
||
function wordCloud(selector) { | ||
//画出词云函数 | ||
|
||
function draw(words) { | ||
var cloud = svg.selectAll("g text") | ||
.data(words, function(d) { return d.text; }) | ||
//Entering words | ||
.data(words, function (d) { | ||
return d.text; | ||
}) | ||
/** | ||
* 给每个词赋上css属性,添加进容器里 | ||
*/ | ||
cloud.enter() | ||
.append("text") | ||
.style("font-family", "Impact") | ||
.style("fill", function(d, i) { return fill(i); }) | ||
.style("fill", function (d, i) { | ||
return fill(i); | ||
}) | ||
.attr("text-anchor", "middle") | ||
.attr('font-size', 1) | ||
.text(function(d) { return d.text; }) | ||
|
||
.text(function (d) { | ||
return d.text; | ||
}) | ||
|
||
|
||
//Entering and existing words | ||
|
||
//词云入场动画 | ||
cloud.transition() | ||
.duration(600) | ||
.style("font-size", function(d) { return d.size + "px"; }) | ||
.attr("transform", "transform: translate(0,0)") | ||
.attr("transform", function(d) { | ||
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; | ||
}) | ||
.style("fill-opacity", 1); | ||
.duration(600) | ||
.style("font-size", function (d) { | ||
return d.size + "px"; | ||
}) | ||
.attr("transform", "transform: translate(0,0)") | ||
.attr("transform", function (d) { | ||
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; | ||
}) | ||
.style("fill-opacity", 1); | ||
|
||
|
||
|
||
//Exiting words | ||
cloud.exit() | ||
.transition() | ||
.duration(200) | ||
.style('fill-opacity', 1e-6) | ||
.attr('font-size', 1) | ||
.remove(); | ||
.duration(200) | ||
.style('fill-opacity', 1e-6) | ||
.attr('font-size', 1) | ||
.remove(); | ||
} | ||
|
||
|
||
|
||
|
||
//Use the module pattern to encapsulate the visualisation code. We'll | ||
// expose only the parts that need to be public. | ||
|
||
|
||
return { | ||
|
||
|
||
//Recompute the word cloud for a new set of words. This method will | ||
// asycnhronously call draw when the layout has been computed. | ||
//The outside world will need to call this function, so make it part | ||
// of the wordCloud return value. | ||
update: function(words) { | ||
cloud().size([960, 500]) | ||
|
||
update: function (words) { | ||
cloud().size([960, 500]) | ||
.words(words) | ||
.padding(5) | ||
.rotate(function () { return (~~(Math.random() * 6) - 3) * 30; }) | ||
.rotate(function () { | ||
return (~~(Math.random() * 6) - 3) * 30; | ||
}) | ||
.font("Impact") | ||
.fontSize(function(d) { return d.size; }) | ||
.fontSize(function (d) { | ||
return d.size; | ||
}) | ||
.on("end", draw) | ||
.start(); | ||
|
||
loader.classed('is-active', () => false); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
function showNewWords(vis, word) { | ||
|
||
//Some sample data - http://en.wikiquote.org/wiki/Opening_lines | ||
vis.update(word.slice(0,Math.min(200,word.length)).map(function (d) { | ||
return { | ||
text: d.key, | ||
size: d3.scale["log"]().range([10, 30])(d.value) | ||
}; | ||
})) | ||
|
||
//Prepare one of the sample sentences by removing punctuation, | ||
// creating an array of words and computing a random size attribute. | ||
function getWords(i) { | ||
// words[i] | ||
// .replace(/[!\.,:;\?]/g, '') | ||
// .split(' ') | ||
return word.map(function(d) { | ||
return {text: d.key, size:d3.scale["log"]().range([10, 30])(d.value)}; | ||
}) | ||
} | ||
|
||
|
||
//This method tells the word cloud to redraw with a new set of words. | ||
//In reality the new words would probably come from a server request, | ||
// user input or some other source. | ||
function showNewWords(vis, i) { | ||
i = i || 0; | ||
|
||
|
||
vis.update(getWords(i ++ % word.length)) | ||
//setTimeout(function() { showNewWords(vis, i + 1)}, 2000) | ||
} | ||
|
||
|
||
//Create a new instance of the word cloud visualisation. | ||
function myWordCloud(word){ | ||
return wordCloud('.svg-contain',word); | ||
} | ||
|
||
//Start cycling through the demo data | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters