var margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
};
var width = 600 - margin.left - margin.right;
var height = 270 - margin.top - margin.bottom;
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 valueline = d3.svg.line()
.x(function (d) {
return x(d.IP);
})
.y(function (d) {
return y(d.count);
});
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 = [{
"IP": "192.168.12.1",
"count": "100"
}, {
"IP": "76.09.45.34",
"count": "200"
}, {
"IP": "34.91.23.76",
"count": "300"
}, {
"IP": "192.168.19.32",
"count": "400"
}, {
"IP": "192.168.10.89",
"count": "500"
}, {
"IP": "192.168.12.98",
"count": "600"
}];
data.forEach(function (d) {
d.IP = d.IP;
d.count = +d.count;
});
x.domain(d3.extent(data, function (d) {
return d.IP;
}));
y.domain([0, d3.max(data, function (d) {
return d.count;
})]);
svg.append("path")
.attr("d", valueline(data));
svg.append("g")
.attr("class", "x axis")
.data(data)
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.data(data);
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;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
</body>
</html>
请帮助我。我被困。我不知道如何使用这些代码获取x axis
和 line Graph 。我是D3
的新手,所以我很困惑。每次我进行更多更改时,轴都会熄灭。这些是我到目前为止尝试的最后一个代码,但我只得到y axis
及其标签,但没有x axis
,并且该行也没有被绘制。请帮助我。预先感谢。
而不是预期仅整数范围的Linear scale
,因此您需要将ordinal scale
用于X轴。
var x = d3.scale.ordinal().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);
x.domain(data.map(function(d) {
return d.IP;
}))
.rangeRoundBands([0, width], 0.1);
y.domain([0, d3.max(data, function (d) {
return d.count;
})]);
这是您的工作代码。
您必须使用x轴的顺序尺度。
序数尺度具有离散域,例如一组名称或 类别。
参考:https://github.com/d3/d3/wiki/ordinal-scales
这是工作代码段:
var margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
};
var width = 600 - margin.left - margin.right;
var height = 270 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangePoints([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 valueline = d3.svg.line()
.x(function(d) {
return x(d.IP);
})
.y(function(d) {
return y(d.count);
});
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 = [{
"IP": "192.168.12.1",
"count": "100"
}, {
"IP": "76.09.45.34",
"count": "200"
}, {
"IP": "34.91.23.76",
"count": "300"
}, {
"IP": "192.168.19.32",
"count": "400"
}, {
"IP": "192.168.10.89",
"count": "500"
}, {
"IP": "192.168.12.98",
"count": "600"
}];
data.forEach(function(d) {
d.IP = d.IP;
d.count = +d.count;
});
x.domain(data.map(function(d) {
return d.IP
}));
y.domain([0, d3.max(data, function(d) {
return d.count;
})]);
svg.append("path")
.attr("d", valueline(data));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
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;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
</body>
</html>