Ways to include dots in a D3 chart?

I'm striving to create a graph that includes lines and dots.

Although I have successfully drawn lines and dots with zoom and brush functionalities, the issue arises when I try to zoom in or out - the dots do not adjust accordingly.

Being relatively new to D3 graphs, I have used the following code to plot the dots:

    g.selectAll(".dot")
      .data(dots)
      .enter()
      .append("circle")
      .attr("class", "dot")
      .attr("r", 3.5)
      .attr("cx", function(d) { return x(new Date(d.date)); })
      .attr("cy", function(d) { return y(d.price); })
      .on("mouseover", function(d){
        return tooltip.style("visibility", "visible").html("Expected value is: "+d.expected_value + "<br/>"  + "value : "+d.close +"<br/>"  + "deviation is: "+d.deviation_expected)
      })
      .on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px");})
      .on("mouseout", function(){return tooltip.style("visibility", "hidden");});

You can find my progress on this jsfiddle link.

Your assistance would be greatly appreciated.

Answer №1

Before delving into visualization, it's crucial to understand some basic features. When dealing with data sets on a graph, such as in the fiddle example, you'll need to plot them across the X and Y axes (e.g., data points or dots). Keep in mind that each set of data has its own domain, so grasp the concept of Domain and range.

d3.scaleLinear() <>

This function creates a continuous scale with a unit domain [0, 1] and a unit range [0, 1], along with the default interpolator and no clamping enabled. Linear scales are ideal for representing continuous quantitative data as they maintain proportional differences. The relationship between a range value y and a domain value x can be expressed as: y = mx + b.

d3.scaleTime() <>

Here, a time scale is constructed with the domain [2000-01-01, 2000-01-02], a unit range [0, 1], the default interpolator, and no clamping enabled.

  • Understand how domains and ranges map to individual data series.
  • Explore concepts like brush and zoom and their inverse mapping to the scales.
  • Practice redrawing plots based on new scales.

** The following code snippet demonstrates the implementation of brushing without considering the scaling for dots. To master this logic, refer to the documentation. Brush Event Example

// JavaScript code showcasing data and dot objects
var data = [{
    "date": "2017-04-22T11:45:00.000Z",
    "total": 731.6047915220261,
    ...
    },
  // Additional data objects here...
];

var dots = [
  {
    "date": "2017-04-22T11:45:00.000Z",
    "total": 731.6047915220261,
    ...
    },
  // More dot objects go here...
];


// Code continuation not displayed for brevity

// End of JavaScript block
.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 2px;
}

.heading {
  padding-top: 30px;
}

.buttonWidth {
  height: 34px;
  width: 50px;
}

.actiontable {
  width: 36rem;
}

.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}
// Additional CSS styles may continue below...

// End of CSS block
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="960" height="500"></svg>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding Data to a Database Using Ajax with PHP

I am trying to use Ajax to insert data into a database, but I keep encountering this error: Uncaught ReferenceError: insertData is not defined at HTMLInputElement.onclick (Modal.php:105) Error screenshot: The HTML and JS for this functionality are in ...

how to handle form submission using JavaScript's return method

As a developer, I have created a single page that fetches data from a registration page. On this page, each data row has an "add" and "unfriend" button, with the latter initially disabled. When a user clicks the "add" button, a prompt box appears asking ...

Enable users to modify the URL in the window.open() function

In the code snippet below, a new window opens when a user clicks on a button: $('.loginButton').click(function () { var strString = $("#medlineSearch").val() var strSearch = "http://google.com&query="; var url = strSearch + strSt ...

Error in Babel-Loader when executing npm run build

Not being a webpack expert, I decided to upgrade from V3 to V4. After fixing the depreciation errors in my webpack config, I hit a roadblock with a syntax error from babel-loader: Module build failed (from ./node_modules/babel-loader/lib/index.js): ...

Selecting options in table is disrupted by filtering in ng-repeat

My table showcases selectable information, featuring parent rows and child rows. I am seeking a solution where only the parent rows are selectable if they have no children; otherwise, only the child rows should be clickable. Essentially, it's a selec ...

When the 'keyup' event is detected, trigger the function only on keyup

Looking for assistance in setting this to only trigger on keyup events. Can anyone provide guidance? $(function() { $('#acf-field_5a32085c7df98-field_5a3208f87df99').on('keyup', function() { $('#link-headline-fb').text($( ...

TypeError: Unable to access the 'classify' property of an object that has not been defined (please save the ml5.js model first)

In my React app, I have set up ml5.js to train a model by clicking on one button and make predictions with another. However, I encounter an error when trying to test the model for the second time: TypeError: Cannot read property 'classify' of und ...

Unable to activate function when closing Vuetify v-alert

Is there a way to trigger a function when the Vuetify v-alert is closed? I have explored the documentation but haven't found any information on this specific functionality. In the codepen example, the dismissible attribute allows for closing the alert ...

How to implement a form in PHP that doesn't refresh the page upon submission

I am having an issue with this ajax code. I know it works, but for some reason it's not submitting. <script type="text/javascript"> $(function(){ $('input[type=submit]').click(function(){ $.ajax({ type: "POST", ...

Differences between Jquery's Find Method and ID Attribute

In my search for efficiency, I am currently exploring ways to quickly access an element. Which method is faster: $('body').find('#elemID'); vs. var id = $('#elemID'); ...

Issue with React Testing Library: Attempting to access the 'contents' property of an undefined value in Redux

Hello, I'm new to writing test cases using React Testing Library. Below is the code of my component: import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; ...

How can I best fill the HTML using React?

After attempting to follow various React tutorials, I utilized an API to fetch my data. Unfortunately, the method I used doesn't seem to be very efficient and the code examples I found didn't work for me. I am feeling quite lost on how to proper ...

What is causing the question mark symbol to appear at the beginning of my ajax response?

Below is my JavaScript code: $('#tags').select2({ tags: true, tokenSeparators: [','], createSearchChoice: function (term) { return { id: $.trim(term), text: $.trim(term) + ' (new tag)&ap ...

Can Google Charts columns be customized with different colors?

Is it possible to modify the colors of both columns in Google's material column chart? Here is the code I am using for options: var options = { chart: { title: 'Event Posting', subtitle: 'Kairos event p ...

What is the best method to make the first input field the focus in BootStrap?

Is there a way to prioritize the focus on the initial input element in an HTML form without specifying an ID? How to set the focus to the first input element in an HTML form independent from the id? I'm working with BootStrap and ASP.NET MVC4. De ...

How does express.route determine the route?

I have recently started learning about Node.js (specifically with Express.js) and React.js. As a result, I have some questions regarding Express Router. Let me share a portion of my code with you: server.js const app = express(); const apiRouter = requi ...

Vue js parent-child communication using event bus fails to function

Is there any way to successfully communicate between non parent child components in vue js document? I followed the instructions in the vue document, but unfortunately, my code did not work as expected. Below is a snippet of my code: The structure of the ...

The MaterialUI table pagination feature is experiencing an issue where the "Next" button is

Every time I attempt to search for a record with a lower value, such as 6, 14, 19, or 20, the Next button does not become active. However, when dealing with larger records, it functions perfectly fine. I am uncertain about what mistake I might be making. ...

Filter a nested array in AngularJS and retrieve the parent object as the result

I've recently started experimenting with AngularJS to create checkbox filters for my wine store. Here is an example of an item in my store: { "id": 17, "name": "Ermelinda Freitas Reserva", "tag_ids": [40, 12, 56, 6, 60], " ...

The contrastText property of the MUI React Theme palette is not functioning properly

I am working with MUI React to design a menu and I have utilized the AppBar component. I would like to customize it in the following way: brown.myBrown = '#544846'; const brownTheme = createTheme({ palette: { primary: { ma ...