score:6
Any possible answer for your question is dangerously opinion-based, because it depends on exactly what you want: do you want to treat time as time? Or do you want to treat time as just separate, individual events?
There are actually three options for you here:
- Using an ordinal scale (band, point...): in this case, each data entry will be just a categorical variable. The space between the ticks in the axis will be the same.
- Using a linear scale: this allows different distances for the ticks in the axis. However, the values won't be time, you'll have to create a math to set the values (the easiest one is probably calculating the number of seconds).
- Using a time scale: this has an advantage, which is the fact that you'll treat time as time. The main disadvantage, however, is exactly the previous statement: time is a complicated beast, you'll have leap years, daylight savings, time zone differences etc...
Since the linear scale is quite simple to create (and the ordinal scale is even more simple), here is an answer showing how to do it with a time scale.
Suppose you have this data array:
var data = ["12:32", "21:05", "24:56", "36:30", "45:14", "71:11"];
The first step is parsing the dates:
var specifier = "%M:%S";
var parsedData = data.map(function(d) {
return d3.timeParse(specifier)(d)
});
And then creating the scale:
var scale = d3.scaleTime()
.domain(d3.extent(parsedData));
For this to work, we'll have to set the tickValues
using our parsed data array, and formatting the ticks for minutes and seconds:
var axis = d3.axisBottom(scale)
.tickValues(parsedData)
.tickFormat(function(d){
return d3.timeFormat(specifier)(d)
});
Here is the result:
var data = ["12:32", "21:05", "24:56", "36:30", "45:14", "71:11"];
var specifier = "%M:%S";
var svg = d3.select("svg");
var parsedData = data.map(function(d) {
return d3.timeParse(specifier)(d)
});
var scale = d3.scaleTime()
.domain(d3.extent(parsedData))
.range([30, 470]);
var axis = d3.axisBottom(scale)
.tickValues(parsedData)
.tickFormat(function(d) {
return d3.timeFormat(specifier)(d)
});
var gX = svg.append("g")
.attr("transform", "translate(0,50)")
.call(axis)
<script src="https://d3js.org/d3.v5.min.js"></script>
<svg width="500" height="100"></svg>
As you can see, the ticks are correctly spaced here. But we have a problem: those ticks are actual dates (if you inspect the code, you'll see that the year is 1900). Because of that, the last tick, which should be 71 minutes and 11 seconds, is shown as 11:11
, because it is the eleventh minute of the next hour.
A simple solutions is referencing the original array of strings:
.tickFormat(function(d,i) {
return data[i]
});
And here is the result:
var data = ["12:32", "21:05", "24:56", "36:30", "45:14", "71:11"];
var specifier = "%M:%S";
var svg = d3.select("svg");
var parsedData = data.map(function(d) {
return d3.timeParse(specifier)(d)
});
var scale = d3.scaleTime()
.domain(d3.extent(parsedData))
.range([30, 470]);
var axis = d3.axisBottom(scale)
.tickValues(parsedData)
.tickFormat(function(d, i) {
return data[i]
});
var gX = svg.append("g")
.attr("transform", "translate(0,50)")
.call(axis)
<script src="https://d3js.org/d3.v5.min.js"></script>
<svg width="500" height="100"></svg>
Source: stackoverflow.com
Related Query
- Formating minutes and seconds on a D3 axis
- Converting seconds to minutes and formatting axes
- How to make hour and minutes as x axis in nvd3.js graph
- How can I get the D3.js axis ticks and positions as an array?
- D3: Is it possible to zoom+pan one axis and only pan the other?
- d3.js & nvd3.js axis and label precision formatting
- How do I make a custom axis formatter for hours & minutes in d3.js?
- how to set the domain and scale on an axis on a nvd3.js multiBarChart
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- D3 Y Axis Scaling and Path Transition
- Ticks only at the start and end of an axis
- d3 axis label cut off in chrome and firefox
- Data points and ticks in the scaleBand axis are not aligned
- d3 bar chart y axis ticks and grid lines above max value
- d3js axis dates start and end value only
- Append Value to Rickshaw Graph Axis and what is ticksTreatment and Preserve
- c3 graph in a dark background; how to change axis and tick value color
- Title and Axis labels
- Make a chart with area charts having both positive and negative y axis in dc or d3.js
- D3.js - Line Graph: Area path goes over x and y axis on zoom
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- Multiples with x/y axis and mouseover example - line path shows wrong values
- D3: How to remove tick marks at the top and bottom of Y axis
- D3 Chart- place space between tick values and axis path lines
- How to create Stacked Line Chart D3, Multiple Y Axis and common X Axis
- D3 hide overflow of the chart area, when Y axis has a min and max
- How to set domain to avoid overlapping dots and axis in d3 plot?
- Multiple Y axis and paths in different scales
- d3 change and update axis domain scatterplot
- how to draw nvd3 simple barchart with x and y axis in angularjs
More Query from same tag
- dc.js/d3.js Is there a way to get all the selected slices,paths, etc on a pie/row chart?
- D3 easing function for animation between points
- d3.scale.log cannot read property log of undefined
- d3js - Sortable Group Bar Chart
- Generate arrows in d3js
- Concentric arcs using D3.js
- Transitioning the clipPath when the data is updated
- Interrupt scrolling transitions in D3.js
- No route matches [GET] "/flare.json"
- How to zoom the path in d3.js
- D3 chart Y axis line is not visible in some resolutions
- Karma/Jasmine unit test for D3 svg element
- How to display rectangular div with data on mouse over event of any node in d3 js tree?
- D3 - Single and Multi Line chart tooltips
- d3v4 - 'transition' of undefined
- r2d3 + Shiny: can't render a local image in D3, despite being able to render one using an URL
- refactoring d3 js code from v5.16 to v6.6.2, d3.event breaking change
- What is the data type of the 'data' argument in d3.request.get?
- Y Axis not displaying for scatter plot with d3 v4
- Angular + D3 - Get scope values to show in template from click event
- C3 xFormat is not working for timestamp in seconds
- d3 v4 wrap text field (not axis)
- drawing a time axis - d3.js
- d3js: Unable to place horizontally bars within the axis
- d3js negative and positive barplot looses its alignment when more bars added
- How do you change the default axis labels in D3
- d3 - table, each char in the display string end up in a separate "td"
- Some bars are black when trying to use individual colors
- How to destroy multiple C3 graphs that were built from the same original function
- D3.js: How to select data from graph's brushed area?