[英]Function is not correctly plotted + cosmetic questions d3js
I have two questions. 我有两个问题。 I am trying to plot a function with d3.js.
我正在尝试使用d3.js绘制函数。 I have already come far, but I have two problems.
我已经走得很远了,但是我有两个问题。
My code currently; 我的代码当前; https://jsfiddle.net/v0du66ey/
https://jsfiddle.net/v0du66ey/
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.grid .tick {
stroke: lightgrey;
opacity: 0.7;
}
.grid path {
stroke-width: 0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 50, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
padding = 50;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
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 + ")");
var data = [];
for (var k = 0; k < 100; k++) {
data.push({x: k, y: k});
}
var line = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");
// Define x domain
x.domain([-10, 10]);
// Define y domain
y.domain([-10, 10]);
// Add x axis
svg.append("g")
.attr("class","x axis")
.attr("transform","translate(0," + height + ")")
.call(xAxis)
// Add y axis
svg.append("g")
.attr("class","y axis")
.call(yAxis);
// Add x grid
svg.append("g")
.attr("class","grid")
.attr("transform","translate(0," + height + ")")
.call(xAxis
.tickSize(-height,-height,0)
.tickFormat("")
);
// Add y grid
svg.append("g")
.attr("class","grid")
.call(yAxis
.tickSize(-width,-width,0)
.tickFormat("")
);
// Add x axis label
svg.append("text")
.attr("transform", "translate(" + (width / 2) + "," + (height + margin.bottom) + ")")
.style("font-size","15")
.style("text-anchor", "middle")
.text("x axis");
// Add y axis label
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y",0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("font-size","15")
.style("text-anchor", "middle")
.text("y axis");
svg.append("path")
.attr("class", "line")
.attr("d",line(data));
</script>
As you can see the function which I have defined, y = x. 如您所见,我定义的函数y = x。 Is not correctly plotted along its entire domain.
沿其整个域未正确绘制。 It goes for x from -10 to approx 2.5 and y from 10 to approx 7.5.
x的范围从-10到约2.5,y的范围从10到约7.5。 What goes wrong here?
这里出什么问题了? I do not namely see it.
我没有看到它。
Furthermore I have a cosmetic question. 此外,我还有一个修饰性的问题。 I am trying to create a border around the whole plot on the axis and trying to get the ticks bars to point to the inside of the graph instead of outside.
我试图在轴上的整个图周围创建边框,并试图使刻度线指向图形的内部而不是外部。 In other words, I want to have it more looking like, a Matlab plot,
换句话说,我想让它看起来更像Matlab图
About cosmetic question: you need to set innerTickSize()
, outerTickSize()
and tickPadding()
on your axises with a negative numbers. 关于外观问题:您需要在
innerTickSize()
设置一个innerTickSize()
, outerTickSize()
和tickPadding()
。
About main question: as far as you have scales you should set line's x and y data as a result of scale functions. 关于主要问题:就比例尺而言,应使用比例尺功能设置行的x和y数据。
so your line code will look like this: 因此您的行代码将如下所示:
var line = d3.svg.line() .x(function(d) { return x(dx); }) .y(function(d) { return y(dy); }) .interpolate("linear");
Here is jsfiddle with updated sample http://jsfiddle.net/n3Lndkum/ 这是jsfiddle,带有更新的示例http://jsfiddle.net/n3Lndkum/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.