Javascript D3JS— ;我的X轴网格线不是';他没有出现
我不知道为什么我的X轴显示得很好,但不是Y轴。有什么想法吗 我99%肯定这和Javascript D3JS— ;我的X轴网格线不是';他没有出现,javascript,css,svg,d3.js,Javascript,Css,Svg,D3.js,我不知道为什么我的X轴显示得很好,但不是Y轴。有什么想法吗 我99%肯定这和 .attr("transform", "translate(0," + height + ")") 因为每次我调整的时候,它都会显示X轴,只是在错误的空间 整个代码如下: <html> <head> <style> /* set the CSS */ body { font: 12px Arial;} path { stroke: steelblue;
.attr("transform", "translate(0," + height + ")")
因为每次我调整的时候,它都会显示X轴,只是在错误的空间
整个代码如下:
<html>
<head>
<style>
/* set the CSS */
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.grid .tick {
stroke: lightgrey;
opacity: 0.7;
}
.grid path {
stroke: lightgrey;
stroke-width: 0;
}
</style>
</head>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<h1>Temperature Chart Over Time</h1>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 800 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Display the date and time
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
// Set the X and Y axis ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the X and Y axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(10);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(10);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.measurementTime); })
.y(function(d) { return y(d.temperature); });
// Adds the svg canvas
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 + ")");
//Draw X axis grid tick marks
function make_x_axis() {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5)
}
//Draw Y axis grid tick marks
function make_y_axis() {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
}
//draw x axis grid lines
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat("")
)
//draw y axis grid lines
svg.append("g")
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat("")
)
// Get the data
d3.json("data.php", function(error, data) {
data.forEach(function(d) {
d.measurementTime = parseDate(d.measurementTime);
d.temperature = +d.temperature;
});
// Center the line on the graph
x.domain(d3.extent(data, function(d) { return d.measurementTime; }));
y.domain([60, d3.max(data, function(d) { return d.temperature; })+10]);
// Add the valueline path
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
</script>
</html>
/*设置CSS*/
正文{font:12px Arial;}
路径{
笔画:钢蓝;
笔画宽度:2;
填充:无;
}
.轴线路径,
.轴线{
填充:无;
笔画:灰色;
笔画宽度:1;
形状渲染:边缘清晰;
}
.grid.勾选{
笔画:浅灰色;
不透明度:0.7;
}
.网格路径{
笔画:浅灰色;
笔画宽度:0;
}
随时间变化的温度图
//设置画布/图形的尺寸
var margin={顶部:30,右侧:20,底部:30,左侧:50},
宽度=800-边距.左-边距.右,
高度=270-margin.top-margin.bottom;
//显示日期和时间
var parseDate=d3.time.format(“%Y-%m-%d%H:%m:%S”).parse;
//设置X轴和Y轴范围
var x=d3.time.scale().range([0,width]);
变量y=d3.scale.linear().range([height,0]);
//定义X轴和Y轴
var xAxis=d3.svg.axis().scale(x)
.方向(“底部”)。刻度(10);
var yAxis=d3.svg.axis().scale(y)
.方向(“左”)。刻度(10);
//界定界线
var valueline=d3.svg.line()
.x(函数(d){返回x(d.measurementTime);})
.y(函数(d){返回y(d.温度);});
//添加svg画布
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
//绘制X轴栅格记号
函数make_x_轴(){
返回d3.svg.axis()
.比例(x)
.orient(“底部”)
.滴答声(5)
}
//绘制Y轴栅格记号
函数make_y_轴(){
返回d3.svg.axis()
.比例(y)
.东方(“左”)
.滴答声(5)
}
//绘制x轴网格线
svg.append(“g”)
.attr(“类”、“网格”)
.attr(“变换”、“平移(0)”、“高度+”)
.调用(生成x轴()
.tickSize(-height,0,0)
.tick格式(“”)
)
//绘制y轴网格线
svg.append(“g”)
.attr(“类”、“网格”)
.调用(生成y轴()
.tickSize(-width,0,0)
.tick格式(“”)
)
//获取数据
json(“data.php”,函数(错误,数据){
data.forEach(函数(d){
d、 measurementTime=parseDate(d.measurementTime);
d、 温度=+d.温度;
});
//在图表上居中
x、 域(d3.extent(数据,函数(d){返回d.measurementTime;}));
y、 域([60,d3.max(数据,函数(d){返回d.temperature;})+10]);
//添加valueline路径
追加(“路径”)
.attr(“类”、“行”)
.attr(“d”,valueline(数据));
//添加X轴
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
//添加Y轴
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis);
});
我在一个破损的JSFIDLE上附加了一个链接,我不认为这是翻译
,因为如果删除整行代码并将其更改为东方('left')
,它会画出同样的东西。我认为你在寻找5条垂直线,但只有2条,对吗?@BuckDoyle是的,这正是问题所在。它在图表两端各有两行,中间没有一行。我自己也尝试过很多不同的方向。@oshikryu谢谢!这比在nano中乱搞要容易得多。您正在代码中创建两组轴。你说的是哪一个?您的数据是什么样子的?是否可能是您的域设置不正确?