導航:首頁 > 編程語言 > js實現模板可視化編輯

js實現模板可視化編輯

發布時間:2025-06-11 00:30:19

1. 如何使用d3.js製作可視化圖表

D3是目前最流行的JavaScript可視化圖表庫之一,D3的圖表類型非常豐富,並且支持SVG格式,因此應用十分廣泛,也有很多圖表插件基於D3開發,比如MetricsGraphics.js,在D3上構建的數據圖表非常強大。


D3的特點

允許綁定任意數據到DOM,將數據驅動轉換應用到Document中。

不僅可以創建精美的HTML表格,而且可以繪制折線圖、柱形圖和餅圖等數據圖表。

支持SVG,在Web頁面上渲染毫無壓力。

回到頂部

D3的使用方法

關於D3的具體用法,可以看D3圖形庫API參考這篇文章。本文主要對介紹一些經典圖表的實現效果及代碼



<!DOCTYPEhtml>
<metacharset="utf-8">
<style>

svg{
font:10pxsans-serif;
}

.y.axispath{
display:none;
}

.y.axisline{
stroke:#fff;
stroke-opacity:.2;
shape-rendering:crispEdges;
}

.y.axis.zeroline{
stroke:#000;
stroke-opacity:1;
}

.title{
font:30078pxHelveticaNeue;
fill:#666;
}

.birthyear,
.age{
text-anchor:middle;
}

.birthyear{
fill:#fff;
}

rect{
fill-opacity:.6;
fill:#e377c2;
}

rect:first-child{
fill:#1f77b4;
}

</style>
<body>
<scriptsrc="http://d3js.org/d3.v3.min.js"></script>
<script>

varmargin={top:20,right:40,bottom:30,left:20},
width=960-margin.left-margin.right,
height=500-margin.top-margin.bottom,
barWidth=Math.floor(width/19)-1;

varx=d3.scale.linear()
.range([barWidth/2,width-barWidth/2]);

vary=d3.scale.linear()
.range([height,0]);

varyAxis=d3.svg.axis()
.scale(y)
.orient("right")
.tickSize(-width)
.tickFormat(function(d){returnMath.round(d/1e6)+"M";});

//AnSVGelementwithabottom-rightorigin.
varsvg=d3.select("body").append("svg")
.attr("width",width+margin.left+margin.right)
.attr("height",height+margin.top+margin.bottom)
.append("g")
.attr("transform","translate("+margin.left+","+margin.top+")");

//.
varbirthyears=svg.append("g")
.attr("class","birthyears");

//Alabelforthecurrentyear.
vartitle=svg.append("text")
.attr("class","title")
.attr("dy",".71em")
.text(2000);

d3.csv("population.csv",function(error,data){

//Convertstringstonumbers.
data.forEach(function(d){
d.people=+d.people;
d.year=+d.year;
d.age=+d.age;
});

//.
varage1=d3.max(data,function(d){returnd.age;}),
year0=d3.min(data,function(d){returnd.year;}),
year1=d3.max(data,function(d){returnd.year;}),
year=year1;

//Updatethescaledomains.
x.domain([year1-age1,year1]);
y.domain([0,d3.max(data,function(d){returnd.people;})]);

//[male,female].
data=d3.nest()
.key(function(d){returnd.year;})
.key(function(d){returnd.year-d.age;})
.rollup(function(v){returnv.map(function(d){returnd.people;});})
.map(data);

//.
svg.append("g")
.attr("class","yaxis")
.attr("transform","translate("+width+",0)")
.call(yAxis)
.selectAll("g")
.filter(function(value){return!value;})
.classed("zero",true);

//(sothatnoenterorexitisrequired).
varbirthyear=birthyears.selectAll(".birthyear")
.data(d3.range(year0-age1,year1+1,5))
.enter().append("g")
.attr("class","birthyear")
.attr("transform",function(birthyear){return"translate("+x(birthyear)+",0)";});

birthyear.selectAll("rect")
.data(function(birthyear){returndata[year][birthyear]||[0,0];})
.enter().append("rect")
.attr("x",-barWidth/2)
.attr("width",barWidth)
.attr("y",y)
.attr("height",function(value){returnheight-y(value);});

//Addlabelstoshowbirthyear.
birthyear.append("text")
.attr("y",height-4)
.text(function(birthyear){returnbirthyear;});

//Addlabelstoshowage(separate;notanimated).
svg.selectAll(".age")
.data(d3.range(0,age1+1,5))
.enter().append("text")
.attr("class","age")
.attr("x",function(age){returnx(year-age);})
.attr("y",height+4)
.attr("dy",".71em")
.text(function(age){returnage;});

//.
window.focus();
d3.select(window).on("keydown",function(){
switch(d3.event.keyCode){
case37:year=Math.max(year0,year-10);break;
case39:year=Math.min(year1,year+10);break;
}
update();
});

functionupdate(){
if(!(yearindata))return;
title.text(year);

birthyears.transition()
.ration(750)
.attr("transform","translate("+(x(year1)-x(year))+",0)");

birthyear.selectAll("rect")
.data(function(birthyear){returndata[year][birthyear]||[0,0];})
.transition()
.ration(750)
.attr("y",y)
.attr("height",function(value){returnheight-y(value);});
}
});

2. 誰能推薦個可視化編輯生成html5 畫布的js代

案例:製作畫圖板代碼。
Html5代碼:
<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8"/>
<title>canvas_01</title>
<styletype="text/css">
#chenhao{
border:1pxsolid#ccc;
}
</style>
<scriptsrc="jquery-1.7.2.js"></script>
</head>
<body>
<canvasid="chenhao"width="500"height="400">
</canvas>
<scriptsrc="345.js"></script>
</body>
</html>
Js代碼:
varc=document.getElementById("chenhao");
varchen=c.getContext("2d");
chen.lineWidth=5;//定義線條的寬度
chen.strokeStyle="red";//定義當前線條的顏色
varpp=false;//定義一個不能畫圖的
$("#chenhao").mousedown(function(e){//當滑鼠按下的時候
varmouseX=e.pageX-this.offsetLeft;//獲取區域坐標X,已經減去了當前頁面的左邊界
varmouseY=e.pageY-this.offsetTop;//獲取區域坐標Y,減去了當前頁面的上邊界。
pp=true;
chen.moveTo(mouseX,mouseY);//起始位置

});
$("#chenhao").mouseup(function(e){//當滑鼠抬起時
pp=false;
});
$("#chenhao").mousemove(function(e){//當滑鼠移動到這個區域時。
varmouseX=e.pageX-this.offsetLeft;//獲取區域坐標X,已經減去了當前頁面的左邊界
varmouseY=e.pageY-this.offsetTop;//獲取區域坐標Y,減去了當前頁面的上邊界。
if(pp){
chen.lineTo(mouseX,mouseY);//終止位置
chen.stroke();//結束圖形

}
});

閱讀全文

與js實現模板可視化編輯相關的資料

熱點內容
wps如何快速復制兩個表格數據 瀏覽:563
如何導入EPLAN數據 瀏覽:785
無限窗口彈出代碼 瀏覽:185
紅頭文件怎麼製作word 瀏覽:831
java遞歸查詢許可權 瀏覽:110
如何製作top數據表 瀏覽:984
騰訊視頻播放器舊版本 瀏覽:811
如何連接區域網資料庫 瀏覽:783
怎樣把存儲卡文件隱藏 瀏覽:528
哪個網站可以看到國內外的論文 瀏覽:811
西部數據硬碟如何使用 瀏覽:225
蘋果地圖怎麼設置網路連接伺服器 瀏覽:297
程序文件 瀏覽:200
安卓程序猿工作好找么 瀏覽:230
網路平台推廣人員 瀏覽:942
checkbox互斥js 瀏覽:131
word文件怎麼比a4大了 瀏覽:699
哪個網站能買到原版烹飪外文書籍 瀏覽:589
cdr的文件如何在ps里打開 瀏覽:657
刪不掉window10的文件夾 瀏覽:253

友情鏈接