Click here to Skip to main content
14,578,769 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am new d3.js .I am trying to run code at this link.For running this code, I placed that code in index.js file.
d3 = require("d3@5")
height = 500
margin = ({top: 10, right: 10, bottom: 20, left: 40})
data = Object.assign(d3.csvParse(await FileAttachment("data.csv").text(), d3.autoType), {y: "Population"})
groupKey = data.columns[0]
keys = data.columns.slice(1)
x0 = d3.scaleBand()
    .domain( => d[groupKey]))
    .rangeRound([margin.left, width - margin.right])
x1 = d3.scaleBand()
    .rangeRound([0, x0.bandwidth()])
y = d3.scaleLinear()
    .domain([0, d3.max(data, d => d3.max(keys, key => d[key]))]).nice()
    .rangeRound([height - margin.bottom,])
color = d3.scaleOrdinal()
    .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"])
xAxis = g => g
    .attr("transform", `translate(0,${height - margin.bottom})`)
    .call(g =>".domain").remove())
yAxis = g => g
    .attr("transform", `translate(${margin.left},0)`)
    .call(d3.axisLeft(y).ticks(null, "s"))
    .call(g =>".domain").remove())
    .call(g =>".tick:last-of-type text").clone()
        .attr("x", 3)
        .attr("text-anchor", "start")
        .attr("font-weight", "bold")
legend = svg => {
  const g = svg
      .attr("transform", `translate(${width},0)`)
      .attr("text-anchor", "end")
      .attr("font-family", "sans-serif")
      .attr("font-size", 10)
      .attr("transform", (d, i) => `translate(0,${i * 20})`);

      .attr("x", -19)
      .attr("width", 19)
      .attr("height", 19)
      .attr("fill", color);

      .attr("x", -24)
      .attr("y", 9.5)
      .attr("dy", "0.35em")
      .text(d => d);
chart = {
    const svg =, height));

        .attr("transform", d => `translate(${x0(d[groupKey])},0)`)
        .data(d => => ({key, value: d[key]})))
        .attr("x", d => x1(d.key))
        .attr("y", d => y(d.value))
        .attr("width", x1.bandwidth())
        .attr("height", d => y(0) - y(d.value))
        .attr("fill", d => color(d.key));




    return svg.node();

After that i called index.js file from html script

    <title>Visualization 2 (Multiline graph)</title>
    <link rel="stylesheet" href="styles.css">
    <script src=""></script>
<svg width="960" height="500"></svg>
<script src="index.js"></script>

I created the data.csv file from array
State,Under 5 Years,5 to 13 Years,14 to 17 Years,18 to 24 Years,25 to 44 Years,45 to 64 Years,65 Years and Over
FL,1140516,1938695, 925060,1607297,4782119,4746856,3187797

This code is not giving the required output that is this graph.
How to get the required output.

Update: Code is giving me following error in vizhub
SyntaxError: Unexpected keyword 'const' (57:4)
/index.js (line 57)
53 :       .attr("dy", "0.35em")
54 :       .text(d => d);
55 : }
56 : chart = {
57 :     const svg =, height));

What I have tried:

Here is vizhub link to code
Updated 31-May-20 1:32am
Richard MacCutchan 31-May-20 7:58am
Remove the word 'const' in line 57. Once you have done that try again ands see if it works. If it does not work the please use the Improve question link above, and add proper details of what fails, what output you expect to see, and what you actually do see.
Member 8840306 31-May-20 8:45am
removing 'const' keyword gives another error on same line
svg =, height));// SyntaxError: Unexpected token
Richard MacCutchan 31-May-20 8:50am
Sorry, but we cannot guess which word it is complaining about. Maybe you should check the documentation for the correct syntax for these commands.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100