gpt4 book ai didi

javascript - 如何在 d3 js 条形图中查看轴标签和值?

转载 作者:行者123 更新时间:2023-11-30 20:38:36 24 4
gpt4 key购买 nike

大家好,我一直在关注一些关于在 D3.js 中制作条形图的 youtube 教程,当我的输出变得困惑时,我似乎搞砸了一些事情

这是我的 html 文件

<!DOCTYPE html>
<html>

<head>
<!-- meta -->
<meta charset="utf-8">

<title>My Data Record</title>

<!-- CSS stylesheet -->
<link rel="stylesheet" type="text/css" href="stylesheet.css">

<!-- D3.js CDN source -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>

</head>

<body>

<!-- Title -->
<h1 style="text-align:center;">Monthly Dispensed Amount</h1>

<!-- Your D3 code for bar graph -->
<script type="text/javascript" src="gdpBarGraph.js"></script>


</body>

</html>

这是我的 javascript 文件

var margin = {top: 20, right: 10, bottom: 100, left:50},
width = 700 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;

var svg = d3.select("body")
.append("svg")
.attr ({
"width": width + margin.right + margin.left,
"height": height + margin.top + margin.bottom
})
.append("g")
.attr("transform","translate(" + margin.left + "," + margin.right + ")");


// defining x and y scales
var xScale = d3.scale.ordinal()
.rangeRoundBands([0,width], 0.2, 0.2);

var yScale = d3.scale.linear()
.range([height, 0]);

// defining x axis and y axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");

d3.csv("newcashdata.csv", function(error,data) {
if(error) console.log("Error: data could not be loaded!");

data.forEach(function(d) {
d.date = d.date;
d.amount = +d.amount;
console.log(d.amount);
});

// sort the values to show at which date the cash collection was the highest
data.sort(function(a,b) {
return b.amount - a.amount;
});

// Specify the domains of the x and y scales
xScale.domain(data.map(function(d) { return d.date; }) );
yScale.domain([0, d3.max(data, function(d) { return d.amount; } ) ]);

svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("height", 0)
.attr("y", height)
.transition().duration(3000)
.delay( function(d,i) { return i * 200; })

.attr({
"x": function(d) { return xScale(d.date); },
"y": function(d) { return yScale(d.amount); },
"width": xScale.rangeBand(),
"height": function(d) { return height - yScale(d.amount); }
})
.style("fill", function(d,i) { return 'rgb(20, 20, ' + ((i * 30) + 100) + ')'});


svg.selectAll('text')
.data(data)
.enter()
.append('text')



.text(function(d){
return d.amount;
})
.attr({
"x": function(d){ return xScale(d.date) + xScale.rangeBand()/2; },
"y": function(d){ return yScale(d.amount)+ 12; },
"font-family": 'sans-serif',
"font-size": '13px',
"font-weight": 'bold',
"fill": 'white',
"text-anchor": 'middle'
});

// Drawing x axis and positioning the label
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("dx", "-.8em")
.attr("dy", ".25em")
.attr("transform", "rotate(-60)" )
.style("text-anchor", "end")
.attr("font-size", "10px");


// Drawing y Axis and positioning the label
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("dy", "-2em")
.style("text-anchor", "middle")
.text("Amount Dispensed");
});

最后是我的样式表:

svg {
margin-left: auto;
margin-right: auto;
display: block;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.axis text{
font: Times;
font-size: 20px;
font-weight: bold;
}

这是我得到的输出: enter image description here

显然我把我的 Y 轴标签“Amount Dispensed”搞得一团糟,我想不出一种方法来改变它,因为我在样式表中的字体大小或我的一些其他错误代码,任何帮助将不胜感激。

编辑:这是我的csv文件

enter image description here

这是右轴更改后的输出y 轴标签确实恢复了,但是单个条上的数字似乎太大而无法描述是否有办法缩短它们,例如 950000 到 950K 等等 enter image description here

最佳答案

Here is a full fiddle

我将您的 margin.left 从 50 增加到 75。我还修改了您的 yAxis 创建以修复刻度数字格式 s will change numbers into their corresponding prefix (7000 到 7k 等)

var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left").tickFormat(d3.format("s"));

我使用的数据只是随机创建的,因为问题只是轴格式。

我还将您添加到 yAxis 的标签从 y: -2em 移动到 y: -3em

关于javascript - 如何在 d3 js 条形图中查看轴标签和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540526/

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