简体   繁体   中英

y axis set custom values in D3 Charts v4

I need to show y axis values as 60, 120, 180...or 200, 400, 600...

Currently my y-axis showing like 100, 200, 300..

my testing code - d3 graph gallery code..

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

 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

Try to use scalePoint instead of 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM