D3 Scatter Plot

Nothing displays, not even the title. I can’t figure out what’s wrong. The code is below.

<body>
  <h5 id="title">
  </h5>
  <div class="scatter-plot">
  </div>
  <div class="tooltip">
  </div>
  <legend class="legend">
    <div class="legend-title">
    </div>
    <div class="color-code">
    </div>
    <div class="country-code">
    </div>
    <div class="nation">
    </div>
  </legend>
  <script src="./script.js ">
  </script>
  <script src="https://cdn.jsdelivr.net/npm/d3@7">
  </script>
</body>
* {
  margin: 0;
  padding: 0;
}

body {
  font-family: sans-serif;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h1 {
  font-size: 6;
}

.dot:hover {
  fill: white;
}

.scatter-plot {
  position: absolute;
}

#tooltip {
  position: absolute;
  width: 160px;
  height: 60px;
  padding: 0.5em;
  font: 1rem;
  border-radius: 4px;
  pointer-events: none;
  color: black;
  background: lightblue;
  display: flex;
  align-items: center;
  justify-content: center;
}
let json = [];
const w = 420;
const h = 400;
const pad = 50;

d3
  .json(
"https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json"
)
  .then((data) => { // *
    const {time, place, seconds, name, year, nationality, doping, URL} = dataset;

    const title = 
document.getElementById("title"); // *
    title.innerText = "Cyclist Data";

    const mxYear = d3.max(dataset, (d) => year);
    const mnYear = d3.min(dataset, (d) => year);

    const xScale = d3
      .scaleTime()
      .domain([mnYear, mxYear])
      .range([pad, w - pad]);

    const mxTime= d3.max(dataset, (d) => new Time(year, 1, 1, 0, 0, seconds, 0).getMinutes());

    const yScale = d3
      .scaleLinear()
      .domain([0, mxTime])
      .range([h - pad, pad]);

    const svg = d3
      .select(".scatter-plot")
      .append("svg")
      .attr("width", w)
      .attr("height", h)
      .style("position", "absolute")
      .style("left", -190)
      .attr("x", w)
      .attr("y", h);

    const tooltip = d3
     .select(".tooltip")
     .attr("id", "tooltip")
     .style("position", "absolute")
     .style("visibility", "hidden");

    svg
      .selectAll("circle")
      .append("g")
      .data(dataset)
      .enter()
      .append("circle")
      .attr("class", "dot")
      .attr("cx", (d, i) => xScale(year))
      .attr("cy", (d, i) => yScale(h - time))
      .attr("r", 5)
      .attr("x-value", (d) => year)
      .attr("y-value", (d) => new Time(year, 1, 1, 0, 0, seconds, 0).getMinutes())
      .attr("fill", "green")

      .on("mouseover", (event, d) => {
        tooltip
          .attr("x-value", year)
          .attr("y-value", new Time(year, 1, 1, 0, 0, seconds, 0).getMinutes())
          .transition()
          .duration(200)
          .style("visibility", "visible")
          .text(
            "Name:\t" + name +
            "Year:\t" + year + 
            "\nTime:\t" + time +
            "\nPlace:\t" + place +
            "\nSeconds\t" + seconds +
            "\nNationality:\t" + nationality +
            "\nDoping:\t" + doping +
            "URL:\t" + URL + "\n")
           .style("left", 70 + "px")
           .style("top", 150 + "px");
      })

      .on("mouseout", () => {
        tooltip
          .transition()
          .style("visibility", "hidden");
      });

  const xAxis = d3.axisBottom(xScale);

  svg
    .append("g")
    .attr("id", "x-axis")
    .attr("transform", `translate(0, ${h - pad})`)
    .call(xAxis);

  const yAxis = d3.axisLeft(yScale);

  svg
    .append("g")
    .attr("id", "y-axis")
    .attr("transform", `translate(${pad}, 0)`)
    .call(yAxis);
});

Can you link to our codepen?

And link to the problem/requirements?

I corrected my post. I posted the wrong program by mistake, initially. Sorry. The correct post is above.

Can you link to our codepen?

And link to the problem/requirements? I’d like to understand what you’re trying to do.

I have a copy of the instructions:

Visualize Data with a Scatterplot Graph

Objective: Build an app that is functionally similar to this: https://scatterplot-graph.freecodecamp.rocks.

Fulfill the below user stories and get all of the tests to pass. Use whichever libraries or APIs you need. Give it your own personal style.

You can use HTML, JavaScript, CSS, and the D3 svg-based visualization library. The tests require axes to be generated using the D3 axis property, which automatically generates ticks along the axis. These ticks are required for passing the D3 tests because their positions are used to determine alignment of graphed elements. You will find information about generating axes at d3-axis | D3 by Observable. Required DOM elements are queried on the moment of each test. If you use a frontend framework (like Vue for example), the test results may be inaccurate for dynamic content. We hope to accommodate them eventually, but these frameworks are not currently supported for D3 projects.

User Story #1: I can see a title element that has a corresponding id=“title”.

User Story #2: I can see an x-axis that has a corresponding id=“x-axis”.

User Story #3: I can see a y-axis that has a corresponding id=“y-axis”.

User Story #4: I can see dots, that each have a class of dot, which represent the data being plotted.

User Story #5: Each dot should have the properties data-xvalue and data-yvalue containing their corresponding x and y values.

User Story #6: The data-xvalue and data-yvalue of each dot should be within the range of the actual data and in the correct data format. For data-xvalue, integers (full years) or Date objects are acceptable for test evaluation. For data-yvalue (minutes), use Date objects.

User Story #7: The data-xvalue and its corresponding dot should align with the corresponding point/value on the x-axis.

User Story #8: The data-yvalue and its corresponding dot should align with the corresponding point/value on the y-axis.

User Story #9: I can see multiple tick labels on the y-axis with %M:%S time format.

User Story #10: I can see multiple tick labels on the x-axis that show the year.

User Story #11: I can see that the range of the x-axis labels are within the range of the actual x-axis data.

User Story #12: I can see that the range of the y-axis labels are within the range of the actual y-axis data.

User Story #13: I can see a legend containing descriptive text that has id=“legend”.

User Story #14: I can mouse over an area and see a tooltip with a corresponding id=“tooltip” which displays more information about the area.

User Story #15: My tooltip should have a data-year property that corresponds to the data-xvalue of the active area.

Here is the dataset you will need to complete this project: https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json

You can build your project by using this CodePen template and clicking Save to create your own pen. Or you can use this CDN link to run the tests in any environment you like: https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js

Once you’re done, submit the URL to your working project with all its tests passing.

Solution Link
ex: https://codepen.io/camperbot/full/oNvPqqo

Open the browser console and read the error messages.


You have data not dataset

Not sure what the Time() constructor is supposed to be, except for Date(), maybe?

How did you write this code when nothing was working from the start? Just as a reminder, do not copy code, it isn’t allowed for the certificate projects.


The CodePen you linked to is missing the code and lib.

Thank you. The information helps.

How did I write the code when nothing works? I just wrote it. I followed a basic framework for the code when I wrote it. I thought this was standard.

I wasn’t aware, when there is much posting of project code by copy and paste in other posts, that this wasn’t allowed.

Sorry. I will keep this in mind. Thanks again.

There is nothing standard about writing a hundred lines of code without seeing any output. Why would you continue to write code when nothing you had written produced any output?

How are you supposed to know what you have written is even remotely correct?

I just meant, if you copied the code, that isn’t allowed.

What I meant is that I was incorrect to post the code. I should instead post a link to the Codepen. I understand.

I found out that there is no display if there is even one thing that is wrong. There are no error messages in the console. No feedback. This happened when using d3 formatting commands, and if an undeclared variable is referenced.

Is it possible for Codepen to give better error feedback. As it is, I wouldn’t immediately know that the code is wrong and would have to execute individual modules to find out what is wrong.

Thank you. The post was helpful.

Currently, my x-axis improperly displays years. My y-axis is displaying integers for the scale when I want the numbers rounded to two decimal places. How can I fix this if the d3 formatting commands don’t work?

My codepen link is https://codepen.io/richardvannie/pen/ZEgrbdq. Thank you.

I notice you are using scaleOrdinal() here:

const xScale = d3
    .scaleOrdinal()
    .domain([mnYear - 1, mxYear])
    .range([pad, w - pad]);

But it might be more appropriate to use a time scale:

https://d3js.org/d3-scale/time

Same with the y axis since it’s also time related.

The x-axis is

.994 .996 .998 :02 .002 .004 .006 .008 .010 .012 .014 with a time scale.

The domain is mnYear to mxYear, which is 1994 through 2015. The y-axis doesn’t appear. What is going on?

My Codepen link is https://codepen.io/richardvannie/pen/ZEgrbdq

Thank you.

The axes don’t appear. Can you help? Thank you. My Codepen link is https://codepen.io/richardvannie/pen/ZEgrbdq

Where is all your HTML, it looks like you wrote the CSS without it.

Is JavaScript supposed to insert the proper head items? They are omitted in the guided lessons. I inserted them myself. They might be duplicated. Same results.

I didn’t read the instruction but most of what I’m referring to is in html like .hover would be in a class etc… inside a div for example inside the body. If you analyze js there a strange pop from codepen that I’ve never seen before, anyway I take it d3 is what you are working with I’m still kind of new to it. Good luck

I passed the d3 Bar Chart tests with a headless HTML file after I was informed that Codepen automatically inserts the head.

Yes, I had some strange pops with d3, when d3 blanks the screen if there is even the smallest error and gives no feedback about what’s wrong. There are no error messages in the console and alert and console.log are suppressed.

What I did was comment out the sections I wasn’t working on.

Then I coded an alert starting at the top pf the block after the first instruction.

If the alert doesn’t display, the instruction is bad.

If the alert displays, the instruction is good. I then move the alert under the next instruction and repeat until the end.

Then I repeat with each block of code.

This is very time-consuming still because of the nature of the d3 I am using. I am still in the process of doing this.

I hope the information is helpful to anyone reading this message.

My Codepen is updated. The link is https://codepen.io/richardvannie/pen/ZEgrbdq. I will greatly appreciate help with having the chart display. Thank you.

I started a new thread because I updated and revamped the code; and because I was only getting very unhelpful replies in my other posts. I will appreciate it if people are helpful. This unhelpfulness goes against the principles of this forum.

Thanks again.

Can you post the link to this challenge?

Please look. I did give a Codepen link. I am looking for helpful replies. Thank you.

This is the link to the challenge