gpt4 book ai didi

javascript - 个性化 d3 x 轴标签

转载 作者:行者123 更新时间:2023-11-30 15:13:44 26 4
gpt4 key购买 nike

我正在制作折线图,经过一些谷歌搜索后我想出了这个:

function maxValue(input) {
var current = 0;

for (var i = 0; i < input.length; i++) {
if (input[i] > current) {
current = input[i];
}
}

return current;
}

var m = [80, 80, 80, 80]; // margins
var w = 900 - m[1] - m[3]; // width
var h = 600 - m[0] - m[2]; // height

var data = [1, 6, 5, 12, 30, 45, 50, 48, 60, 69, 62, 80];

var months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];

var x = d3.scale.linear().domain([1, 12]).range([0, w]);
var y = d3.scale.linear().domain([0, maxValue(data)]).range([h, 0]);

var line = d3.svg.line()
.x(function(d,i) {
return x(i + 1);
})
.y(function(d) {
return y(d);
})

var graph = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");

var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true);
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);


var yAxisLeft = d3.svg.axis().scale(y).ticks(3).orient("left");
graph.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(-25,0)")
.call(yAxisLeft);

graph.append("svg:path").attr("d", line(data));

graph.append("text")
.attr("x", w/2)
.attr("y", -20)
.style("text-anchor", "middle")
.style("font-size", "18")
.text("Total Hits in the Last Year");

如您所见,我为一年中的月份制作了一个数组,并希望 x 轴显示月份而不是数值。但我不知道它在哪里说要制作标签。任何人都知道如何提供帮助?谢谢!

附言“svg:svg”与“svg”有何不同?

编辑:

function maxValue(input) {
var current = 0;

for (var i = 0; i < input.length; i++) {
if (input[i] > current) {
current = input[i];
}
}

return current;
}

var m = [80, 80, 80, 80]; // margins
var w = 900 - m[1] - m[3]; // width
var h = 600 - m[0] - m[2]; // height

var data = [1, 6, 5, 12, 30, 45, 50, 48, 60, 69, 62, 80];

var months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];

var x = d3.scale.linear().domain([1, 12]).range([0, w]);
var y = d3.scale.linear().domain([0, maxValue(data)]).range([h, 0]);

var line = d3.svg.line()
.x(function(d,i) {
return x(i + 1);
})
.y(function(d) {
return y(d);
})

var graph = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");

var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true);
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);


var yAxisLeft = d3.svg.axis().scale(y).ticks(3).orient("left");
graph.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(-25,0)")
.call(yAxisLeft);

graph.append("svg:path").attr("d", line(data));

graph.append("text")
.attr("x", w/2)
.attr("y", -20)
.style("text-anchor", "middle")
.style("font-size", "18")
.text("Total Hits in the Last Year");
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}

.axis {
shape-rendering: crispEdges;
}

.x.axis line {
stroke: lightgrey;
}

.x.axis .minor {
stroke-opacity: .5;
}

.x.axis path {
display: none;
}

.y.axis line, .y.axis path {
fill: none;
stroke: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<html>
<head>
<title>Simple Line Graph</title>
</head>
<body>

<div id="graph" class="aGraph" style="position:absolute;top:0px;left:0; float:left;"></div>

</body>
</html>

最佳答案

您可以使用 xAxis 的 tickFormat 函数从数组中获取月份名称:

var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true).tickFormat(function(d) {
return months[d-1];
});

关于javascript - 个性化 d3 x 轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44765784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com