[英]y axis set custom values in D3 Charts v4
I need to show y axis values as 60, 120, 180...or 200, 400, 600...我需要将 y 轴值显示为 60、120、180...或 200、400、600...
Currently my y-axis showing like 100, 200, 300..目前我的 y 轴显示为 100、200、300..
my testing code - d3 graph gallery code..我的测试代码 - d3 图形库代码..
HTML HTML
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
Javascript Javascript
var y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.n; })])
.domain([0, 100000])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
Sample code link - https://www.d3-graph-gallery.com/graph/line_several_group.html示例代码链接 - https://www.d3-graph-gallery.com/graph/line_several_group.html
Try to use scalePoint instead of scaleLinear:尝试使用 scalePoint 而不是 scaleLinear:
var width = 800, height = 800;
var data = [60, 120, 180, 240, 300, 360];
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var x = d3.scalePoint()
.domain([60, 120, 180, 240, 300]) // This is what is written on the Axis: from 0 to 100
.range([50, 400]); // This is where the axis is placed: from 100 px to 800px
var y = d3.scalePoint()
.domain([60, 120, 180, 240, 300]) // This is what is written on the Axis
.range([50, 400]); // This is where the axis is placed: from 50 px to 400px
// Draw the axis svg.append("g").attr("transform", "translate(0,50)") // This controls the vertical position of the Axis.call(d3.axisBottom(x)); // 绘制轴 svg.append("g").attr("transform", "translate(0,50)") // 这控制 Axis.call(d3.axisBottom(x)) 的垂直 position;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.