alexc
alexc

Reputation: 1310

How to add circles onto line chart path d3.js

Here is my chart;

http://plnkr.co/edit/Cej2NcyUWysAsKiMAEXj?p=preview

I'm trying to add a circle on each data point along the line path, but can't seem to find a way to do it.

Here is the code I'm using to draw the circles/line for the graph;

  var selectLine = svg.selectAll(".line")
    .data([data])

  var selectCircle = svg.selectAll(".circle")
    .data([data])

  svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)

  svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)

  selectLine.enter().append("path")
    .attr("class", "line")
    .attr("d", line);

  selectCircle.enter().append("circle")
    .attr("class", "circle")
    .attr("r", 3.5)
    .attr("cx", function(d) {
      return x(new Date(2016, moment(d.date, 'MMMM').format('M') - 1, 1))
    })
    .attr("cy", function(d) {
      return y(d.close)
    })

Any help/advice is much appreciated!

Thanks

Upvotes: 2

Views: 9863

Answers (1)

Tim B
Tim B

Reputation: 1983

Change

var selectCircle = svg.selectAll(".circle")
.data([data])

to

var selectCircle = svg.selectAll(".circle")
.data(data)

Because you want each point to be treated as separate item

See http://plnkr.co/edit/NCQyDtykbxjlvK687WIu?p=preview

Upvotes: 5

Related Questions