score:2
You can style the axes as shown below.
var xAxis = g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
xAxis.select("path") //Axis
.style("stroke","white");
xAxis.selectAll("line") //ticks
.style("stroke","white");
var yAxis = g.append("g")
.call(d3.axisLeft(y));
yAxis.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 20)
.attr("text-anchor", "end")
.attr("font-size", "1.2em")
.text("Price ($)");
yAxis.select("path") //Axis
.style("stroke","white");
yAxis.selectAll("line") //ticks
.style("stroke","white");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="960" height="500"></svg>
</head>
<style>
body {
text-align: center;
margin-top: 5em;
background-color: #74b9ff;
}
h1 {
color: snow;
}
</style>
<body>
<h1>Bitcoin Prices in U.S. Dollars</h1>
<script>
var url = "https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=200&aggregate=3&e=CCCAGG";
d3.json(url).get(function(error, d) {
var data = d.Data;
data.forEach(function(d) {
d.time = new Date(d.time * 1000)
});
if (error) throw error;
var svg = d3.select("svg"),
margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleTime()
.range([0, width])
var y = d3.scaleLinear()
.range([height, 0]);
var line = d3.line()
.x(function(d) {
return x(d.time);
})
.y(function(d) {
return y(d.close);
});
x.domain(d3.extent(data, function(d) {
return d.time;
}));
y.domain(d3.extent(data, function(d) {
return d.close;
}));
var xAxis = g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.attr("stroke-width", 2)
.attr("fill", "none")
.style("font-size", ".8em")
xAxis.select("path") //Axis
.style("stroke", "white");
xAxis.selectAll("line") //ticks
.style("stroke", "white");
var yAxis = g.append("g")
.call(d3.axisLeft(y))
.attr("stroke-width", 2)
.style("font-size", ".8em");
yAxis.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 20)
.attr("text-anchor", "end")
.attr("font-size", "1.2em")
.text("Price ($)");
yAxis.select("path") //Axis
.style("stroke", "white");
yAxis.selectAll("line") //ticks
.style("stroke", "white");
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#ffeaa7")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 2)
.attr("d", line);
});
</script>
</body>
</html>
score:-1
While Gilsha's answer seems like best practice and allows for more direct control over styling, the answer got from that was to use selectAll()
to select and style each "path" (axes), "line" (ticks), and "text" (display of price and date).
Including the following in the end of the function d3.json(url).get(function(error, d)
styles everything as white:
g.selectAll("path") //Axes
.style("stroke","white");
g.selectAll("line") //Ticks
.style("stroke","white");
g.selectAll("text") //Text displaying date and price
.attr("fill", "white");
Note that this broad-stroke approach includes the path for the plotted data line as well. If you need to distinguish between them, you can define a variable for that and style it independently as Gilsha did for the axes.
Source: stackoverflow.com
Related Query
- How do you change color of lines drawn in d3 with svg?
- How to update the fill color on existing svg elements with d3.js?
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- How to select and deselect a svg line and change its color on selection and deselection using d3.js and javascript , jquery?
- How to draw single Pixel lines with d3.js / SVG
- How to create SVG with grid lines using D3JS
- How to display country name labels on an SVG map drawn with d3?
- How do you update an existing svg element with the d3v5 join syntax
- How can you change the height of an x-axis with d3.js
- How to change color of multiple SVG nodes from onClick
- How do I prevent clipping of an SVG drawn with the D3 library (in Javascript)?
- how to change the color of circle to back with original color on clicking of another circle in D3 Js chart
- How do I remove all children elements from a node and then apply them again with different color and size?
- How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?
- How do I show a bootstrap tooltip with an SVG object?
- How to select a d3 svg path with a particular ID
- D3 how to change the width and length of SVG
- How to update an svg path with d3.js
- how do you set a fixed link distance with d3 v4
- Fill SVG element with a repeating linear gradient color
- How do you make an SVG element mouse event bubble up through another element?
- How to change the color of two circles upon overlap?
- How to change a circle into a square with d3.js
- How to change line color in d3js according to axis value?
- How to show a tooltip with value when mouseover a svg line graph using d3.js?
- How can I set the each line color or width in SVG path
- how do you draw linear line in scatter plot with d3.js
- How do you change the last tick value in D3.js?
- How do I change color of a bar in Vega-lite Bar Chart?
- How to move elements along with svg group
More Query from same tag
- How to programatically add new children to a deeply nested object
- D3: update data with multiple elements in a group
- How do I group the nodes in a force directed graph
- Setting ticks on a time scale during zoom
- d3: confusion about selectAll() when creating new elements
- Trying to link object and text when dragging in v4
- Show a D3 SVG Within a Tooltip
- How to fill area with our choice colour in plotly area grap
- GeoJSON with lower precision
- Why do transitions flicker/stutter when applied in a separate function (D3)
- Template level reactivity in Meteor
- d3.js doesn't work for some reason
- data uri as background image of svg not converted to canvas on safari live site
- Import a iPython Custom Widget Function (File Structure?)
- Labelling nodes in causes zooming glitch in D3JS
- How do I update this data without redrawing datapoints that are already there?
- d3 vs. svg When Dealing with Element
- How to re-position datapoints in d3?
- How can I draw simple lines (flows) between arcs in D3.js?
- Alternating axis line colors in D3
- Creating a border around your D3 graph
- Interaction with multiple charts in a page
- Dom-to-image SVG image elements
- IE9 detects z-index events different than Chrome/Firefox - how to fix?
- d3js transforming nested group images
- add text inside a rect in force layout graph d3.js
- d3 skips first index in an array when appending circles
- Zooming lines in d3js
- How to dynamically update a d3.js force layout graph?
- Structuring Plain Text to JSON