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

热点内容
vb窗体文件如何打开 浏览:810
百度找图怎么跳转网站 浏览:284
安卓44如何隐藏文件夹 浏览:92
sr100怎么编程序 浏览:795
苹果手机怎样保存信息 浏览:379
安卓patch是什么文件 浏览:819
2003word审阅 浏览:387
手机时间查找新文件 浏览:695
数据库管理员怎么入行 浏览:852
安卓nba2k17修改能力 浏览:868
win10设置nsf文件共享 浏览:445
同一个网站怎么发文章 浏览:725
苹果翻盖手机多少钱 浏览:815
flash80金鹰教程 浏览:374
怎么把美国的app账号换成中国的 浏览:375
贷款60秒app 浏览:408
捷安特骑行app有什么奖励 浏览:542
网站图片展示代码 浏览:167
asp找回密码 浏览:836
如何知道别人使用我的电脑和看了什么文件 浏览:712

友情链接