D3.js 实现线的渐变

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="../lib/d3/d3.v3.js"></script>
  <title>PolylineGradient</title>
</head>
<body>
<script type='text/javascript'>
  //数据
var lineData=[{"x":1,   "y":5},  {"x":20,  "y":20},
              {"x":40,  "y":10},{"x":60,  "y":40},
              {"x":80,  "y":5},  {"x":100,"y":60}];

//线生成器
var lineFunction = d3.svg.line()
                         .x(function(d){return d.x;})
                         .y(function(d){return d.y;})
                         .interpolate("cardinal");
 
//svg容器
var svgContainer = d3.select("body").append("svg")
                                    .attr("width",200)
                                    .attr("height",200);
// 定义渐变色带,可以参考SVG的定义
var a = d3.rgb(255,0,0);	//红色
var b = d3.rgb(0,255,0);	//绿色
var defs = svgContainer.append("defs");
var linerGradient = defs.append("linearGradient")
					.attr("id","linearColor")
					.attr("x1","0%")
					.attr("y1","0%")
					.attr("x2","100%")
					.attr("y2","0%");
var stop1 = linerGradient.append("stop")
					.attr("offset","0%")
					.style("stop-color",a.toString());
var stop2 = linerGradient.append("stop")
					.attr("offset","100%")
					.style("stop-color",b.toString());
//把path扔到容器中-- lineData和lineFunction,并给d赋属性
var lineGraph = svgContainer.append("path")
                    .attr("d",lineFunction(lineData))
                    //.attr("stroke","blue")
					// 线的渐变使用参数为stroke
					.attr("stroke","url(#"+linerGradient.attr("id")+")")
                    .attr("stroke-width",12)
                    .attr("fill","none");
</script>
</body>

</html>

 

转载于:https://my.oschina.net/u/615762/blog/1502945

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一个流行的JavaScript框架,而D3.js是一个强大的JavaScript图形库,用于创建各种类型的数据可视化。在Vue3中使用D3.js可以轻松地创建网络图。 以下是使用Vue3和D3.js创建网络图的步骤: 1. 首先,您需要在Vue3项目中安装D3.js。可以使用npm包管理器执行以下命令:npm install d3 2. 在Vue组件中导入D3.js:import * as d3 from 'd3' 3. 在组件的mounted生命周期钩子中,使用D3.js创建SVG容器并设置其大小和位置: ``` mounted() { const svg = d3.select('#network-chart') .append('svg') .attr('width', this.width) .attr('height', this.height) } ``` 4. 接下来,您需要将网络数据传递给Vue组件。可以通过props来实现这一点。 5. 使用D3.js创建节点和连线。这可以通过以下代码完成: ``` const node = svg.selectAll('.node') .data(nodes) .enter() .append('circle') .attr('class', 'node') .attr('r', this.nodeRadius) .attr('fill', 'blue') const link = svg.selectAll('.link') .data(links) .enter() .append('line') .attr('class', 'link') .attr('stroke', 'gray') ``` 6. 最后,在组件的updated生命周期钩子中更新节点和连线的位置: ``` updated() { const node = svg.selectAll('.node') .attr('cx', d => d.x) .attr('cy', d => d.y) const link = svg.selectAll('.link') .attr('x1', d => d.source.x) .attr('y1', d => d.source.y) .attr('x2', d => d.target.x) .attr('y2', d => d.target.y) } ``` 以上就是使用Vue3和D3.js实现网络图的简单步骤,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值