在散点图d3js中扩展x和y轴长度 [英] extend x and y axis length in scatterplot, d3js

查看:30
本文介绍了在散点图d3js中扩展x和y轴长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展x和y轴的长度,并在最后添加箭头.这是我的代码和插件.我需要x轴超出4.6,y轴超出"AS",并在末尾添加箭头.请帮助. https://plnkr.co/edit/tA6oyKQCCmhNadbARR89?p=preview

I am trying to extend the x and y axis length and add the arrow at the end. Here is my code and plunker. I need x axis to extend beyond 4.6 and y-axis beyond "AS" and add an arrow at the end. Kindly help. https://plnkr.co/edit/tA6oyKQCCmhNadbARR89?p=preview

var margin = {top: 20, right: 20, bottom: 20, left: 40},
width = 420 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
.range([0, width]);

var y = d3.scale.ordinal().rangePoints([height, 0])

var color = d3.scale.category10();

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom").outerTickSize(0);

var yAxis = d3.svg.axis()
.scale(y).tickSize(-width).outerTickSize(0)
.orient("left");

var svg = 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 + ")");

d3.tsv("data.tsv", function(error, data) {
if (error) throw error;

data.forEach(function(d) {
d.sepalWidth = +d.sepalWidth;
});

x.domain(d3.extent(data, function(d) { return d.sepalWidth; })).nice();
y.domain(data.map(function(d) {   return d.sepalLength;}));

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (height) + ")")
.call(xAxis);

svg.append("g")
.attr("class", "y axis")
.call(yAxis);

svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.sepalWidth); })
.attr("cy", function(d) { return y(d.sepalLength); })
.style("fill", function(d) { return color(d.species); });

var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});

推荐答案

最简单的方法是沿轴的末端添加自定义路径:

Easiest way is to just tack on a custom path to the end of the axis:

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + (height) + ")")
  .call(xAxis)
  .append("path")
  // add extension arrow
  .attr("d", "M" + width + ",0L" + (width + 20) + ",0L" + (width + 20) + ",-5L" + (width + 30) + ",0L"  + (width + 20) + ",5L" + (width + 20) + ",0L");

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("path")
  // add extension arrow
  .attr("d", "M0,0L0,-20L-5,-20L0,-30L5,-20,L0,-20Z");

更新了朋克车.

这篇关于在散点图d3js中扩展x和y轴长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆