score:14
You could try like this
const xAxis = d3.axisTop(xScale)
.tickValues(data)
.tickFormat(d => (d.year +":"+ d.val));
score:-1
What I have been able to do now:
d3.axisTop(xScale)
.tickValues(data.map(d => d.year))
.tickFormat((d, i) => data[i].val);
Quite ugly but it works. If I get a better solution I will accept a different one.
score:2
Since you didn't show the scale in your question we don't know what type of scale you have and what are the domain values. But one thing is sure: you cannot pass the whole object to it. Therefore, your tickFormat
doesn't have access to one of the properties in each object.
A simple solution is using the index of the tick to get the other property, provided that you are displaying all the ticks (and that you are using an ordinal scale):
var axis = d3.axisBottom(scale)
.tickFormat(function(d, i) {
return d + ": " + data[i].val;
});
Here is a demo:
var svg = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 100);
var data = [{
year: 1999,
val: 5
}, {
year: 2002,
val: 8
}, {
year: 2005,
val: 1
}, {
year: 2008,
val: 4
}, {
year: 2011,
val: 9
}, {
year: 2014,
val: 2
}];
var scale = d3.scalePoint()
.range([20, 480])
.domain(data.map(function(d) {
return d.year
}));
var axis = d3.axisBottom(scale)
.tickFormat(function(d, i) {
return d + ": " + data[i].val;
});
var gX = svg.append("g")
.attr("transform", "translate(0,50)")
.call(axis);
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- d3.js, how can i create an axis with custom labels and customs ticks?
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- How can I get the D3.js axis ticks and positions as an array?
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- How to create left axis with variable tick interval in tick values and has same distance between tick value
- D3.js V4 : How to create X axis with time and %H:%M:%S format
- Howto set custom ticks and labels on Y axis in nvd3
- How to replace X axis numerical values(years) with custom labels in D3.js for this example?
- How to create a bar chart with custom string labels with d3 in angular?
- How can I connect a circle to a path with a line that is perpendicular to the x axis to create an "error line"?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How to create floor plans that work nice with mobile and desktop browsers?
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- How can I plot positive and negative values for both axes in a scatter plot with d3?
- How can I create a doughnut chart with rounded edges only on one end of each segment?
- Create bode plot with minor and major ticks d3js
- dimple.js How can I change the labels of a chart axis without changing the data?
- How to create Stacked Line Chart D3, Multiple Y Axis and common X Axis
- How can I remove non-integer ticks while zooming in with D3.js?
- how to draw nvd3 simple barchart with x and y axis in angularjs
- d3.js how to place custom labels on horizontal log scaled axis
- How to remove the comma in D3 axis ticks and remove the last tick?
- How can I add axis labels to a sankey diagram in d3?
- How can I append string to y-axis data with tick and d3.format?
- How to customize nvd3's tooltips and remove labels from x axis
- How to add svg rectangles as background behind axis tick labels in d3js if ticks are inside chart
- d3 bar chart with custom x axis and bar width
- How can i bind an json data with a key and Name for display
- How do I create a D3.js tree layout with custom child nodes?
More Query from same tag
- DOM manipulation outside of jquery
- D3 Pie Graph Text/Animation
- how to display the small value arcs in the d3.js pie chart?
- Labelling nodes in causes zooming glitch in D3JS
- d3 force directed graph moving away on the svg, separating into group of nodes
- d3js: supply a function to tickPadding
- How to Stack circles using d3js with lable and values
- Rendering a multi-line chart in DC.js
- nvd3 - How to redraw axis on windows resize
- Keys not binding correctly to elements in D3
- how to focus the clicked bar in C3.js bar graph?
- <base> tag breaks SVG marker-end rendering
- Line Chart with different line colors in d3
- Logarithmic charts in crossfilter
- How to have marker end enable to end of path in a d3 tree
- CloudFront Image error when used in D3
- D3 selection and entry
- Can you chain a function after a transition without it being a part of the transition?
- Generate multiple random paths and animate circles along paths
- How do I select one group of values from dataset to plot a linechart using D3.js and not the whole dataset
- how to set proper domain for date time scale in d3
- installing d3 using npm causes contextify errors
- D3 rect + color does not display
- D3 - Help Required in d3.nest
- How do I get Vaadin to load a JS file in UTF-8 using the JavaScript annotation?
- how to create dots(scatterplot) on a line in a d3 line chart
- D3js path update enter and exit
- D3 v4 force graph with images as nodes
- Can't load TSV when using D3.JS
- D3JS can't receive function?