Reputation: 752
I am newbie in java script and d3.js . I wanted to add images to corresponding points in the chart. I went through a lot of questions and articles, but I was not able to figure out that properly. The following is my code
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis--x path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 3px;
}
.circle {
fill: steelblue;
}
.axis--y path, .axis--y line {
fill: none;
stroke: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="http://localhost/d3.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 50, right: 50, bottom: 30, left: 80},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x= d3.scaleLinear().rangeRound([0, width]);
y = d3.scaleBand().range([height, 0]).padding(0.1);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("http://localhost/data1.tsv", function(d) {
d.frequency = +d.frequency;
return d;
}, function(error, data) {
if (error) throw error;
var line = d3.line()
.x(function(d) { return x(d.frequency); })
.y(function(d) { return y(d.letter)+30; })
.curve(d3.curveStepAfter);
x.domain([0, d3.max(data, function(d) { return d.frequency; })]);
y.domain(data.map(function(d) { return d.letter; }));
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("letter");
g.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
</script>
This image is the output of the above code:
I want to add icons or tiny image in the lines as in the link below
This image shows how I want to add icons to my chart:
A question similar to mine is How do I display an icon at a point on a line chart using d3.js , but that is not working for me, and I want to add my custom icons too.
Upvotes: 0
Views: 5808
Reputation: 517
I had a similar problem but was able to solve it.
Initially, I had a basic point style like this
svg
.append("g")
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) { return x(d.date) } )
.attr("cy", function(d) { return y(d.value) } )
.attr("r", 5)
.attr("fill", "#69b3a2")
and changed to this to have an image as a point
svg
.append('g')
.selectAll('dot')
.data(data)
.enter()
.append('svg:image')
.attr('xlink:href', function (d) {
return 'paht/to/image'
})
.attr('x', function (d) {
return x(d.date);
})
.attr('y', function (d) {
return y(d.value);
});
If you want to see the whole context, my chart was based on this example https://www.d3-graph-gallery.com/graph/connectedscatter_basic.html
Upvotes: 0
Reputation: 186
If you are looking to add images for tooltip like this example, replace circle by image by adding something like
focus.append('svg:image')
.attr({
'xlink:href': 'test.png',
x: 0,
y: 0,
width: 10,
height: 10
});
If looking to add images as data points, look at this example replacing dots by image as above code.
Upvotes: 3