Skip to content

Instantly share code, notes, and snippets.

@lhstock
Last active July 31, 2019 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lhstock/8394a9bef240321c34f3c13576d50ee5 to your computer and use it in GitHub Desktop.
Save lhstock/8394a9bef240321c34f3c13576d50ee5 to your computer and use it in GitHub Desktop.
scalePoint()
license: mit
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
</body>
<script>
var width = 500,height = 500,padding = {left:20,right:20};
var data = ["tang","song","yuan","ming","qing"];
var axis_length = width - padding.left - padding.right;
//d3.scaleImplicit = 200;
var svg = d3.select("body").append("svg")
.attr("width",width)
.attr("height",height)
var scale = d3.scalePoint()
.domain(data)
// .range([0,axis_length-2])
// .round(true)
.rangeRound([0,axis_length])
.padding(.1)
.align(0.1);
var axis = d3.axisBottom()
.scale(scale)
.ticks(5)
svg.append("g").call(axis).attr("transform","translate("+ (padding.left) +","+(height - 100)+")");
</script>
</html>
@Manisha1205
Copy link

i have implemented same code with tick count 3 like this : var axis = d3.axisBottom().scale(scale).ticks(3), but its not reflecting , its still has 5 ticks on browser view, can u please brief me about it

@lhstock
Copy link
Author

lhstock commented Jul 30, 2019

i'm sorry. this's an error. "ticks " does not apply to d3.scalePoint().
image
You can assign data to control the quantity
var scale = d3.scalePoint() .domain(data)
or
Maybe you should choose the right scale type

@Manisha1205
Copy link

Manisha1205 commented Jul 31, 2019 via email

@lhstock
Copy link
Author

lhstock commented Jul 31, 2019

let data = ['longString1',longString2',longString3',longString4',longString5']
let filterData = data.filter((d,i)=> i %2 === 0)
let scale = d3.scalePoint()
.domain(data)
.rangeRound([0,axis_length])
let scale = d3.scalePoint()
.domain(data)
. rangeRound([0,axios_length])
.tickValues(filterData) // see this code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment