Issue with multi line graph using D3 V6


I’m having a problem with D3. Here’s what i would like to achieve : a multi line graph by cities (“ville”) with colours corresponding to categories.
I finally get a graph, tooltip and colours, but they don’t correspond to the data.
I believe the problem comes from my dataNest array but after multiple hours, trying to fix the array, the range and the domain of colours, i couldn’t find the right one.

Thanks for your answers

Here’s a sample of my data.csv file :
Arbonne la Forêt,2015,NaN,#65509C
Arbonne la Forêt,2016,NaN,#65509C
Arbonne la Forêt,2017,NaN,#65509C
Arbonne la Forêt,2018,9.13,#65509C
Arbonne la Forêt,2019,5.21,#65509C

And here’s my code :

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

body {
  font-family: "PT Sans";

path {
  stroke-width: 10px;
  fill: none;
  overflow: visible;
  opacity: 0.5;
.axis path,
.axis line {
  fill: none;
  /*stroke: grey;*/
  stroke-width: 0.2px;
  shape-rendering: crispEdges;

 .tooltip {
  position: absolute;
  text-align: center;
  /*display: none;*/
  padding: 5px;
  background: #50C688;
  opacity: 0.6;
  border: 0px;
  border-radius: 7px;
  pointer-events: none;


    <div id="container" height="500" width="960" , style="border:none"></>
    <!--<g id="body"></g>-->



<!-- load the d3.js library --> 

<script src=""></script>


// set the dimensions and margins of the graph 
var margin = {top: 20, right: 20, bottom: 30, left: 50}, 
width = 960 - margin.left - margin.right, 
height = 500 - - margin.bottom;

// parse the date / time 
var parseTime = d3.timeParse("%Y");

// set the ranges 
var x = d3.scaleTime().range([0, width]); 
var y = d3.scaleLinear().range([height, 0]);

// define the line

var tauxRefus = d3.line()
  //line with missing data 
  .defined(d => !isNaN(d.taux))
  .x(d => x(d.annee))
  .y(d => y(d.taux));

// define the container width and height

var svg ="#container")
    .attr("width", width + margin.left +margin.right)
    .attr("height", height + + margin.bottom) 
      "translate(" + margin.left + "," + + ")");

// define the tooltip 

var tooltip ="#container").append("div")
    .attr("class", "tooltip")
    .style("opacity", 0);

// Get the data 

d3.csv("SMICTOM_VilleAnneeTaux.csv").then(function(data) {
  data.forEach(function(d) {
  d.annee = parseTime(d.annee);
  d.taux = +d.taux;
  d.ville = d.ville;
  d.couleur = d.codeHexa;

// Scale the range of the data 

x.domain(d3.extent(data, function(d) { return d.annee; }));
y.domain([0, d3.max(data, function(d) { return d.taux; })]).nice();

// Grouper les entrées par nom de ville 

var dataNest = Array.from(, d => d.ville), ([ville, taux]) => ({ville, taux}))

  var couleur = d3.scaleOrdinal()
  .range(, function(d) {return d.taux.couleur}))
  .domain(, function(d) {return d.ville; }));

 // Boucle à travers chaque clef 

dataNest.forEach(function(d, i) {
    .attr("class", "line")
    .style("stroke", function() {
      return d.couleur = couleur(i)

    .attr("d", tauxRefus(d.taux))
            .on("mouseover", function(event, d) {
            tooltip.html("Ville : " + dataNest[i].ville + "<br>" + "Couleur : " + data[i].couleur)
              .style("left", event.clientX + "px")
              .style("top", event.clientY + 25 + "px")
              .style("display", "inline")
    "opacity", 1).style("stroke-width", 5)
        .on("mousemove", function(event, d) {
            tooltip.html("Ville : " + dataNest[i].ville + "<br>" + "Année : " + data[i].couleur)
              .style("left", event.clientX + "px")
              .style("top", event.clientY + 25 + "px")
              .style("display", "inline")
    "opacity", 1)
        .on("mouseleave", function(event, d) {
          .style("display", "none")
          .style("opacity", 0.8)"opacity", 0.5)
        .style("stroke-width", 1)

// Add the X and Y axis

  .attr("class", "axis")
  .attr("transform", "translate(0," + height + ")")
  .style("font-family", "PT Sans")
  .call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0));

  .attr("class", "axis")
  .style("font-family", "PT Sans")
  .call(g =>".domain").remove())

// line with missing data

  .attr("stroke", "grey")
  .attr("d", tauxRefus);




PS : i’m a kind of a noob in D3 but working hard to fix it.

I finally got a chance to look at this. First, I rearranged your code so that the css, html, and js were in separate files and reordered some of your html. When I tried to load the page, I hit a CORS error (locally) on your d3.csv() call in the browser console, since fetch wants a http/https address. I worked around that.

I can only get a graph that looks like the data if I set do the stroke part like

dataNest.forEach(function(d, i) {
    .attr("class", "line")
    .style("stroke", "#65590C")

I get a line plot that looks like the data, but I don’t know if it’s what you want.

You use forEach to iterate over your data, but d3 will handle that for you by using calls like

    const graph = svg.selectAll('rect')

which will then place the current data item in any calls to functions to set the stroke, color, etc.


Thanks so much for your time and your answer. I’ve just seen it.
Here’s what i’ve succeded to achieve until now (Et si on améliorait le recyclage ?).
Colors are taken from the data, but they don’t fit to dataNest. So, i’m gonna try your suggestion : if my understanding is correct, i should use
and replace forEach to select all path and apply my attributes (colors from the data).
I’ll follow that lead and come back with some answers.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.