ERROR702: The requested action cannot be completed as the object does not have support for the specified property or method 'attr'

An error occurred while trying to execute the following code...

The code snippet provided is for creating a vertical bar chart using D3. However, when executed, it results in an error and the chart is not displayed as expected.

// Data for generating the vertical bar chart
var chartData = [40, 60, 80, 100, 70, 120, 100, 60, 70, 150, 120, 140];

// Dimensions of the svg element
var height = 200,
    width = 720,

// Width of each bar and offset between bars
    barWidth = 40,
    barOffset = 20;


d3.select('#bar-chart').append('svg')
  .attr('width', width)
  .attr('height', height)
  .style('background', '#dff0d8')
  .selectAll('rect').data(chartData)
  .enter().append('rect')
    .style({'fill': '#3c763d', 'stroke': '#d6e9c6', 'stroke-width': '5'})
    .attr('width', barWidth)
    .attr('height', function (data) {
        return data;
    })
    .attr('x', function (data, i) {
        return i * (barWidth + barOffset);
    })
    .attr('y', function (data) {
        return height - data;
    });
<!DOCTYPE HTML>
    <html>
    <head>
    <title>Vertical Bar Chart
    </title>
    <script src="https://d3js.org/d3.v5.min.js"></script>
    </head>
    <body>
        <h1>Vertical Bar Chart Using D3</h1>
     <div id="bar-chart"></div>  
     </body>
     </html>

Answer №1

Try moving the chain call for the style attribute function to the end, based on the d3-selection d3.style() documentation

The .style() function returns the value of the style property with the specified name for the specific node. If there is an inline style with the specified name, it returns that value; otherwise, it returns the computed property value. See also selection.style.

Since .style() does not return a D3 object, chaining it won't work in this case.

Here is how you can fix this:

// Data powering the bar chart - array of numeric values
var chartData = [40, 60, 80, 100, 70, 120, 100, 60, 70, 150, 120, 140];

// Size of the overall svg element
var height = 200,
    width = 720,

// Width of each bar and offset between bars
    barWidth = 40,
    barOffset = 20;


d3.select('#bar-chart').append('svg')
  .attr('width', width)
  .attr('height', height)
  .style('background', '#dff0d8')
  .selectAll('rect').data(chartData)
  .enter().append('rect')
    .attr('x', function (data, i) {
        return i * (barWidth + barOffset);
    })
    .attr('y', function (data) {
        return height - data;
    })
    .attr('width', barWidth)
    .attr('height', function (data) {
        return data;
    })
    .style({'fill': '#3c763d', 'stroke': '#d6e9c6', 'stroke-width': '5'})
    
<!DOCTYPE HTML>
    <html>
    <head>
    <title>Vertical bar chart
    </title>
    <script src="https://d3js.org/d3.v5.min.js"></script>
    </head>
    <body>
        <h1>Vertical bar chart using D3</h1>
     <div id="bar-chart"></div>  
     </body>
     </html>

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

Animate a three.js object moving towards the front of the camera

Currently, I have an object that is smoothly tweening towards the front of the camera by using WestLangleys solution from a different question: var pLocal = new THREE.Vector3( 0, 0, -10 ); var pWorld = pLocal.applyMatrix4( camera.matrixWorld ); ...

The error message that popped up on my console read: "Uncaught (in promise) DOMException: Failed to load due to the absence of a compatible source."

Whenever I attempt to create a JavaScript function that allows users to preview their files before submission, I encounter this error: Uncaught (in promise) DOMException: Failed to load because no supported source was found. My goal is to develop a JavaScr ...

Operating Node SerialPort on a CentOS 6 Linux environment

Hi everyone, I'm a beginner in node.js and I've recently developed an application using node, express, socket, and serialport. I have a weighing scale connected to serialport (COM2) which works perfectly on my Windows localhost. However, when I t ...

The CSS styling of Vuetify TreeView does not support text wrapping

I'm having trouble getting the long text in this CodePen snippet to break and wrap properly. It extends off screen, rendering the append button unclickable. I've experimented with various CSS rules but haven't found a solution yet. Check ou ...

Fixing the issue of a div exceeding the height of its parent div in Tailwindcss

One issue I'm encountering is with my card element, which has a fixed height of h-80 in Tailwind. Typically, I use this card within a grid. However, I recently added a div inside the card that is larger than its parent div, and I want it to scroll ve ...

It appears that JavaScript has the capability to automatically remove event listeners from textareas

Greetings to all members of the community, I have developed a web application (see code below) that, when double-clicked, inserts a text area into the frame. Upon double-clicking the text area, it changes its color to green, and with another double-click ...

Is it considered acceptable to conduct an https redirect from an http connection?

My web application utilizes node.js, the socket.io library, and a postgresql database while being hosted on heroku. When accessing the site with "http://X", I encounter an error related to the socket.io library that is not present when using "https://X". T ...

What is the easiest way to pass a chosen value to PHP?

My goal is to dynamically display photos based on the selected album without refreshing the entire page. Here is my current script: <script type="text/javascript"> function replaceContent(divName, contentS) { document.g ...

Troubleshooting error message "Unexpected field" when uploading images using Multer in NodeJS

I am encountering an "Error: Unexpected field" issue while attempting to process and store uploaded images using NgFileUpload in my Angular code. Below is the code snippet: HTML: <div ngf-drop="upload($files)" ngf-select="upload($files)" ng-model="fil ...

Ways to retrieve a service variable within a delegated function (callback)

I am currently using a service that has a variable which needs to be updated by the service itself. However, I am facing an issue where I cannot access the variable in anonymous or delegated functions. (function() { 'use strict'; angular ...

Tailored alignment of checkboxes and labels

I've designed a custom checkbox where the actual checkbox is hidden and a label is styled to look like a checkbox. Everything works fine, however, when I add a label, it doesn't align properly. Here's the link to the fiddle: http://jsfiddle ...

Converting the source to your image assets in Angular: A step-by-step guide

I am looking to update the source code. Here is an example: <div *ngFor="let item of meal.allergenList" class="btn btn-primary"> <img [src]="item" alt=""> </div> I want to make the following co ...

Using Discord.js to retrieve user input and generate a customized message: How to do it?

I'm currently working on developing a Discord bot using javascript. I want the bot to be able to retrieve user input, specifically a person's name or mention, and then output it as a message. For example, if I type !say @Dobie, I would like the ...

Can you display the menu and then conceal it by clicking elsewhere?

I am facing a unique situation where the body tag does not visually wrap all content. In this scenario, I have 3 TR's that each contain 2 TD's - one for information and one for a yellow div. By clicking on the Info TD, a div opens (positioned a ...

Node.js Express not inserting data with Mongoose when using form data

For the past two weeks, I have been struggling to post data to insert into a database using form-data. It consistently shows a 400 bad request error. Below is my code for server.js: require('./db.js') let express = require('express') ...

The HTML element received focus from either a mouse click or keyboard input

Is there a way to determine if an HTML element (such as a select tag) has received focus via mouse-click, keyboard input, or a JavaScript function? <select onfocus="foo(event)"></select> <script> function foo(e) { if (e.??? == &apos ...

What is the best method for determining the central position of a .dae file and adjusting its placement?

As I work with numerous 3D models, I have noticed that many of them are not centered properly. Is there a method to determine the dimensions (length for x, width for z, height for y) of a model and divide it by two in order to accurately position the model ...

"Converting an HTML file to a Word file using PHP - a step-by-step

Help needed with converting an HTML file to MS Word using PHP code. Below is the code I am using: include("connection.php"); extract($_REQUEST); ob_start(); include("counties/$county_name/$file"); $html_content = ob_get_contents(); ob_end_clean(); header ...

Browsersync and Gulp Continuously Refreshing CSS

My Gulp and Browsersync setup is causing the CSS in the Style.css file to reset every time I run npm start. While the changes to index.html persist and I can successfully push the initial CSS changes to Github, I am puzzled why the CSS additions keep reset ...

Using AngularJS to create a select dropdown menu with nested objects as the options

Currently, I am working on developing an AngularJS application with a complex data structure. The data source consists of an array of individuals with their languages and skill levels. My goal is to be able to filter these individuals based on language sk ...