导航:首页 > 编程语言 > 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实现模板可视化编辑相关的资料

热点内容
如何连接局域网数据库 浏览:783
怎样把存储卡文件隐藏 浏览:528
哪个网站可以看到国内外的论文 浏览:811
西部数据硬盘如何使用 浏览:225
苹果地图怎么设置网络连接服务器 浏览:297
程序文件 浏览:200
安卓程序猿工作好找么 浏览:230
网络平台推广人员 浏览:942
checkbox互斥js 浏览:131
word文件怎么比a4大了 浏览:699
哪个网站能买到原版烹饪外文书籍 浏览:589
cdr的文件如何在ps里打开 浏览:657
删不掉window10的文件夹 浏览:253
在线javaapi 浏览:879
如何查看被隐藏的文件或文件夹 浏览:754
苹果5s怎么设置使用2g网络 浏览:519
下载word到桌面 浏览:777
java中mathpow 浏览:10
日亚代拍哪个网站最好 浏览:252
linux修改配置文件锁定用户 浏览:272

友情链接