javascript架構echarts外掛程式實現超酷人立方,javascriptecharts
最近由於項目需求,需要做一個類似人立方效果的網路關係效果,在查詢許多文檔後,發現百度出echarts開源組件非常的適合,而且載入速度很棒,echarts圖形主要是使用html5的新特性的做的,使用到了canvas畫板等。下面是我自己實現的一些過程和經驗,供大家參考。
這裡是echarts的網站:http://echarts.baidu.com/doc/example.html
裡面有詳細的使用流程和例子,只要是稍微懂一些javascript就能看懂。
第一步:
下載echarts的外掛程式: 在項目中匯入一下倆個檔案
1 esl.js
2 echarts-original.js
3 jquery-1.9.1.min.js jQuery壓縮檔
第二步:
定義一個div容器,用來盛放echarts的圖形,必須定義一個id。如下:
<div id="rel" style="height: 500px; margin: 0 auto;"></div>
第三步:
配置相關圖形的屬性,載入圖形到容器中,啟動圖形。
詳細代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>網路關係圖</title><script type="text/javascript" src="js/jquery-1.9.1.min.js"></script><script type="text/javascript" src="js/echarts/esl.js"></script><script type="text/javascript">function relativeWord() {// 路徑配置require.config({paths : {"echarts" : "js/echarts/echarts-original","echarts/chart/force" : "js/echarts/echarts-original"}});// 使用require([ "echarts", "echarts/chart/force" ], function(ec) { // 使用力向圖模組,按需載入// 基於準備好的dom,初始化echarts圖表var myChart = ec.init(document.getElementById("rel"));var option = {title : {text : "網路關係圖",subtext : "資料來源andy",x : "right",y : "bottom",padding : [ 10, 30 ]},dataRange : {color : [ '#1178ad', '#72bbd0' ]},tooltip : {trigger : "item",formatter : "{a0}:{b0}<br>關係值:{d}",borderRadius : 3,backgroundColor : "rgba(0,0,0,0.4)",padding : 4,axisPointer : {type : 'none',lineStyle : {color : '#4b8',width : 2,type : 'dashed'},crossStyle : {color : '#1e90ff',width : 5,type : 'dashed'},shadowStyle : {size : 'auto',color : 'rgba(150,150,150,0.3)'}}},color : [ '#33ff33', '#cc33ff' ],legend : {data : [ {name : "中心詞",textStyle : {color : "#33ff33"}}, {name : "關係詞",textStyle : {color : "#cc33ff"}} ]},series : [ {type : "force",name : "詞關係",categories : [ {name : "中心詞",}, {name : "關係詞",} ],itemStyle : {normal : {label : {show : true,textStyle : {color : "#FFF",fontFamily : "微軟雅黑",fontSize : 12}},nodeStyle : {brushType : "both",color : '#333333',strokeColor : "rgba(255,215,0,0.4)",lineWidth : 10},linkStyle : {strokeColor : "#8cdfc3",type : 'dashed',lineWidth : 2}},emphasis : {label : {show : false// textStyle: null // 預設使用全域文本樣式,詳見TEXTSTYLE},nodeStyle : {//color:"#4B8"//r: 30},}},useWorker : false,minRadius : 25,maxRadius : 35,gravity : 0.4,scaling : 1.5,animation : true,large : true,useWorker : 20,linkSymbol : "none",nodes : [ {"category" : 0,"name" : '幸福',"value" : 10}, {"category" : 1,"name" : '你',"value" : 4}, {"category" : 1,"name" : '我',"value" : 5}, {"category" : 1,"name" : '他',"value" : 4}, {"category" : 1,"name" : '家',"value" : 22}, {"category" : 1,"name" : '微暖',"value" : 3}, {"category" : 1,"name" : '愛情',"value" : 4}, {"category" : 1,"name" : '友情',"value" : 9}, {"category" : 1,"name" : '情親',"value" : 11}, {"category" : 1,"name" : '房子',"value" : 14}, {"category" : 1,"name" : '車',"value" : 10}, {"category" : 1,"name" : '美滿',"value" : 11}, {"category" : 1,"name" : '健康',"value" : 14}, {"category" : 1,"name" : '事業',"value" : 12}, {"category" : 1,"name" : '別墅',"value" : 10} ],links : [ {source : '幸福',target : '你',weight : 2}, {source : '幸福',target : '我',weight : 9}, {source : '幸福',target : '他',weight : 11}, {source : '幸福',target : '家',weight : 2}, {source : '幸福',target : '美滿',weight : 11}, {source : '幸福',target : '健康',weight : 14}, {source : '幸福',target : '事業',weight : 12}, {source : '幸福',target : '別墅',weight : 10}, {source : '你',target : '車',weight : 22}, {source : '你',target : '微暖',weight : 10}, {source : '你',target : '房子',weight : 8}, {source : '我',target : '情親',weight : 5}, {source : '房子',target : '情親',weight : 10}, {source : '車',target : '家',weight : 5}, {source : '幸福',target : '房子',weight : 7},{source : '我',target : '愛情',weight : 9}, {source : '我',target : '友情',weight : 7},]} ]};var ecConfig = require("echarts/config");function focus(param) {var data = param.data;var links = option.series[0].links;var nodes = option.series[0].nodes;if (data.source !== undefined && data.target !== undefined) { //點擊的是邊var sourceNode = nodes[data.source];var targetNode = nodes[data.target];console.log("選中了邊 " + sourceNode.name + " -> "+ targetNode.name + " (" + data.weight + ")");} else { // 點擊的是點console.log("選中了" + data.name + "(" + data.value + ")");}console.log(param);}myChart.on(ecConfig.EVENT.CLICK, focus);// 為echarts對象載入資料 myChart.setOption(option);});}relativeWord();</script></head><body><div><div style="margin: 20px 0 0 20px; font-size: 14px;"><!-- 詞網路容器 --><div id="rel" style="height: 500px; margin: 0 auto;"></div></div></div></body></html>
其入下: