What is the best way to vanish the circles using keyboard commands?

After reviewing the example at http://bl.ocks.org/d3noob/10633704, my goal is to create an input field on my keyboard where I can enter a number, and then use array.slice() to make circles disappear based on that number. Unfortunately, my implementation did not work as expected. In my code, I have generated circles based on the values of the array days. Within the HTML section, I have created a button for inputting a number. The desired functionality is to shorten the array days using the value entered in the input field (referenced within the brackets of the slice() function) so that circles associated with those values will disappear. However, there seems to be an error in my code. Can someone please assist? I am utilizing D3 to address this issue. Thanks

<!DOCTYPE html>
<meta charset="utf-8">
<title>Input (number) test</title>

<p>
  <label for="nValue" 
         style="display: inline-block; width: 120px; text-align: right">
         angle = <span id="nValue-value"></span>
  </label>


  <input type="number" min="0" max="360" step="4" value="0" id="nValue">
</p>

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 600;
var height = 300;

var svg = d3.select("body")
      .append("svg")
      .attr("width", width)    
      .attr("height", height); 



  var days = [7, 12, 20, 31, 40, 50];
  console.log(days);




  var circle = svg.selectAll("circle")
            .data(days)
            .enter().append("circle")
            .attr("cy", 60)
            .attr("cx", function(d, i) { return i * 100 + 40; })
            .attr("r", function(d) { return Math.sqrt(d); });



  d3.select("#nValue").on("input", function() {
  update(+this.value);
});


// Initial update value 
update(0);




function update(nValue) {

days.slice(nValue);
}

Answer №1

It took me some time to grasp the concept you're aiming for here, and I may still not fully comprehend it.

The Issue

From what I gather, you are making changes to an array of data (using a select menu in this scenario), but these modifications don't seem to reflect in your visualization. Essentially, as "the array days is getting shorter ... let circles based on the value[s] of the array disappear."

Updating the Visualization

To update the visualization, you must bind the new data to your selection. Then, you can eliminate unnecessary elements in the visualization, introduce new ones (not pertinent to this query), or adjust existing elements. Simply altering the data array will not automatically update the visualization. To make the visualization incorporate the new information, you need to bind that data to the selection:

circle.data(data);

Subsequently, you can discard the old items:

circle.exit().remove();

You can also tweak properties of the existing items:

circle.attr('cx',function(d,i) {...

Your update function should at least handle updating the data and removing unneeded elements.

Modifying the Array

In the following code snippet, I'm appending both a select menu and circles using d3 based on the data in the array. Selecting an item from the menu will remove a circle:

(JavaScript code sample)

(Explanation continued with code examples) ...

Solution Approach

Rather than depending on the increment of an item in the array (which alters each time an element before another is removed), consider incorporating an id property in your data structure.

This necessitates restructuring your data slightly. For instance:

var data = [ {id:1,value:1},{id2....

Since the id property remains constant, it serves as a better attribute for setting attributes. Refer to the subsequent sample below:

(More JavaScript code)

Answer №2

Consider updating your function with the following code snippet:

function modifyValue(newData) {

 numbers = numbers.splice(newData);

}

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

Incorporating a new dropdown menu above the content of a pre-existing jQuery accordion dropdown, causing the content to shift downwards

Seeking a way to overlay a second-level dropdown menu on top of the content beneath a jQuery accordion-style dropdown. Typically, the jQuery accordion pushes down the content below, but I want the second-level dropdown to remain unaffected. Is there a solu ...

What are some ways to troubleshoot the UI of a Nativescript app?

As a newcomer to NativeScript technology, I often encounter challenges while developing applications. Whether it's troubleshooting why a textview is not displaying properly, identifying layout overlaps, or detecting other distortions in the UI, debugg ...

Display the output returned from the AJAX request (HTML and/or JavaScript)

How can I effectively render content from an ajax call when the response may include html and/or javascript? I used to rely on document.write() for synchronous calls, but now that I need to use asynchronous calls, I have to find a different approach to dis ...

Retrieve elements within the array ranging from index 1 to 5 using Javascript

How can I log items from index 1 to 5 in the current array by using a loop? let cars = ["AUDI","BMW","LEXUS","VOLKSWAGEN","FERRARY","PORSCHE"] for (let i = 0; i < cars.length; i++) { if (i >= 1 && i <= 5) { console.log("The current ...

Creating clones of <li> elements using JQuery and appending them to the end of a <ul> list

I have a regular ul li list and I am looking to add an additional li based on the first one in the list. Here is the HTML: <div id="gallery"> <ul> <li>Point Nr 1<p>lol</p></li> <li>Point Nr 2</li> <li> ...

Prevent storing each result value in a separate array when utilizing preg_match in PHP

Just starting out with PHP and attempting to extract data between two tags from an HTML file using preg_match. I managed to retrieve the data, but the issue is that each value is ending up in separate arrays as shown below: Array ( [0] => Bachelor ...

What is the best way to create a button with this functionality?

In the form that I have created, it is opened in a bootstrap modal style. This form contains a button that, when clicked, triggers an alert box to appear. The code snippet used for this functionality is as follows: echo "<script>"; echo "alert(&apos ...

Click event triggered the function, however it was unable to modify the variable in $scope

<!doctype html> <html ng-app> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> <script src="script.js"></script> </head> <body> <ul c ...

Changing synchronous functions to asynchronous

Attempting to transform synchronous calls into asynchronous ones using when...done. Despite being new at this, after extensive reading on the topic for the past two days, my code should work as intended. However, while it does function, it doesn't exe ...

Utilizing Javascript to interact with a different webpage

Although my knowledge of javascript is quite limited, I have a strong interest in creating a script that requires data from another webpage. Is there a javascript alternative to urllib2 that can handle simple GET requests without the need for cookie stor ...

Working with C functions and function prototypes that involve a two-dimensional array parameter

void create_game_map (int *game_map); <--- function prototype int rows, cols; <-- global variables some main { // Prompt user for number of rows and cols int game_map[rows][cols]; // Create game map here because another function will use it ...

Capturing the value of a child element using an Angular directive

When I include scope: {finishcallback: "&"} in my directive, the binding of values with $scope.minutes = 1 to ng-bind="minutes" stops working. I am currently working on creating a countdown timer using Angular directives. However, I am facin ...

Disabling the global ajax datatype causes issues with Rails remote links

I have successfully developed a rails application that submits a form remotely and then displays form validation errors if the validation process fails. The form itself is working perfectly. However, I've come across an issue where the way I've ...

JavaScript Countdown Timer Programming

I've scoured countless resources, but most of them only provide code for counting down to a specific day. I'm reaching out in the hopes that someone can assist me by crafting some JavaScript for the following HTML structure. This section of my w ...

Grouping results by field data is a common practice in MYSQL and PHP

My task involves generating reports based on data extracted from a log within the database structured as follows: id |    student    |    type     |    marks 1       23494   &n ...

Link your 3D force graph node by attaching it

How can a link be attached to the nodes in 3d-force-graph (https://github.com/vasturiano/3d-force-graph) so that clicking on a node redirects the user to a specific or customized URL? ...

Is it possible to use array_combine with an unequal number of elements? Are there any alternative methods using a

My PHP code fetches names and emails, then runs them in a foreach loop to send emails to respondents and input their names and emails into a MySQL database. The issue I'm facing is that I can select all respondents without a problem, but I encounter ...

Issues arise in the alignment of the tab order when utilizing a combination of the stripe card

Experiencing an issue with tab index not working properly when using Vue.js and having a stripe card-element inside the Vue main element. Below is the code snippet: <html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js ...

Saving the object array to a file

Encountering an issue while trying to write an object array into a file in JAVA specifically with integer values. Collections.shuffle(list); r = (Object[])list.toArray(); Attempted to write the object array "r" to a file using the following code. fos = ...

Issue with Google Adsense - adsbygoogle.push() error: Pages can only support one AdSense head tag. The additional tag will be disregarded

After adding my script tag to the navbar component, I encountered an error on my site during the next js build: Google Adsense error -adsbygoogle.push() error: Only one AdSense head tag supported per page. The second tag is ignored <Head> ...