There was an error: Unable to access the 'axis' property because it is undefined

This is the code snippet I am working with:

<html> 
 <head> 
    <meta charset="utf-8">         
    <title>a picture</title> 
    <style>
        .axis path,
        .axis line{
            fill: none;
            stroke: black;
            shape-rendering: crispEdges;
        }

        .axis text{
            font-family: sans-serif;
            font-size: 11px;
        }
    </style>
 </head> 

    <body>
        <script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
        <script>
            var width=300; 
            var height=300; 

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

            var dataset = [ 2.5 , 2.1 , 1.7 , 1.3 , 0.9 ];  

            var linear = d3.scaleLinear()     
                           .domain([0, d3.max(dataset)])
                           .range([0, 250]);

            var rectHeight=25;  

            svg.selectAll("rect")
               .data(dataset)
               .enter()
               .append("rect")
               .attr("x",20)
               .attr("y",function(d,i){
                    return i * rectHeight;
            })
               .attr("width",function(d){
                    return linear(d);
            })
               .attr("height",rectHeight-2)
               .attr("fill","steelblue");

            var axis = d3.svg.axis()
                         .scale(linear)      
                         .orient("bottom")          
                         .ticks(7);         

            svg.append("g")
               .attr("class","axis")
               .attr("transform","translate(20,130)")
               .call(axis);

        </script>

    </body> 
</html>

I encountered an issue when using this code - specifically, the error message "plot:53 Uncaught TypeError: Cannot read property 'axis' of undefined." After looking into similar problems, it seems like there may have been an update causing this. If you have any insights or suggestions on how to address this issue, I would greatly appreciate your help. Thank you!

Answer №1

You have updated to d3 version 4.x. with this latest version, the axis generator function has undergone changes.

The correct variable to use is:

 var axis = d3.axisBottom(linear)
    .ticks(7);   

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

CSS Class ID selection for selectpicker

I have two classes: selectpicker with different IDs: selected_Test_Platforms and selected_SIL_relevant I am trying to assign a preselection from an array called "availableTestPlatforms" to the selected_Test_Platforms. Currently, when I use the selector $( ...

Excluding certain images from a JavaScript Photobox animation within a div - a step-by-step guide

Currently, I am using photobox to showcase all the images in a div as part of a beautiful gallery display. However, there is one image that I do not want to be included in this gallery - a PDF icon which, when clicked, should download a PDF file. The issue ...

Mongoose (mongodb) node returning null JSON object when using Model.find()

Below is the code snippet in question: app.get('/all', function(req,res) { Party.find({},[],function(p) { console.log(p); }); res.redirect('/'); }); The goal was to retrieve all collections from the database, but it returns ...

Challenges with launching a fresh React App

After installing Nodejs(v 10.16.0 LTS), I used Windows Powershell to execute the following commands: npx create-react-app my-app cd my-app npm start However, I am encountering an issue where my code works properly only when the PowerShell ...

AJAX: JSON response is not valid

When using ajax to fetch data from my JSON file located at http:xxx.myserver.com/myfolder/example.json $.ajax({ url:"http:xxx.myserver.com/myfolder/example.json", dataType:"json", success:function(data, status) { alert("success"+data+" ...

Execute action based on local state when component is unmounted in React

I have encountered an issue with a component's internal state not being properly propagated throughout the application, specifically under certain events. const FunComponent = (initialDescription, onSave) => { const [description, setDescription] ...

Parsing JavaScript JSON using PHP's json_decode method is a powerful feature

In my current scenario, I am encountering situations where I extract a JSON string from an HTML page and pass it to the `json_decode` function. Sometimes it succeeds, but other times `json_decode` returns null. How can I enhance my solution to ensure that ...

encountered an error when attempting to utilize the Google Index API in JavaScript

Greetings everyone, I am interested in creating a script that updates the new URLs of my website using the Google Indexing API from http://localhost:8000/test.html. I have added http://localhost:8000 to Authorized JavaScript origins in the client ID, but I ...

Encountered an issue while attempting to write to SQLite, error code 6 is

I'm working on a project that involves writing to a SQLite Database from a cross-platform app built with AngularJS, Monaca, and Onsen UI. Within my app, there's a view where users input their username and password. I store these details in a Ser ...

Why is it that I am not receiving JSON data in my Angular application?

I am currently working on a class within a webapi public class ResponseObject { public int Success { get; set; } public string Message { get; set; } public object Data { get; set; } } Within my ASP.NetCore, I have the following method: publi ...

Failure in querying with Mongo's $lookup results in an empty array

My $lookup operation on schemas is always returning an empty array. What could be causing this issue? Result Collection const resultSchema = new mongoose.Schema({ trial: { type: mongoose.Schema.Types.ObjectId, ref: 'Trial', requir ...

Updating a single document in Node JS using Express is a simple and straightforward process

I'm having trouble understanding why my documents aren't updating. When I retrieve any record and load it into the form using http://localhost:3000/update2?buyerID=2299, the field doesn't update when I make changes and click submit. It rema ...

Retrieve dashboard configurations in AngularJS by making an $http request prior to initiating the dashboard controller

I am currently immersing myself in Angular and tackling a complex dashboard framework all built with Angular. Prior to loading the controllers, I need to fetch various dashboard settings from the server using $HTTP. These settings play a crucial role in de ...

The value of an AngularJS model object cannot be altered with a dynamic key

app.controller('indexController', ['$scope', '$location', 'authService', function ($scope, $location, authService) { var vm = this; vm.$onInit = function () { vm.active = { "home": true, ...

JavaScript and jQuery radio button matrix group implementation

I'm feeling completely lost when it comes to solving this problem. My task is to create a matrix of radio buttons with columns labeled 1 to 3 and rows labeled A to C. A B C 1 (o) (o) (o) 2 (o) (o) (o) 3 (o) (o) (o) <tabl ...

Tips for creating multiple popups using a single line of JavaScript code

I am new to JavaScript and I am attempting to create a popup. However, I am facing an issue in opening two divs with a single line of JavaScript code. Only one div opens while the other remains closed despite trying various solutions found on this website. ...

Failed AJAX Request - Google Chrome Experiences Issue

I have been working on a website that loads HTML content from a template page and then pulls in data from an XML file. This process is initiated in the document.ready function as shown below: $.ajax({ type : "GET", url : "t ...

Merging RXJS observable outputs into a single result

In my database, I have two nodes set up: users: {user1: {uid: 'user1', name: "John"}, user2: {uid: 'user2', name: "Mario"}} homework: {user1: {homeworkAnswer: "Sample answer"}} Some users may or may not have homework assigned to them. ...

VUEJS - Link the output from my function to my sub-template

I am looking for a way to link the output of my function with the child template. methods object methods: { filterOptions(checkedValues: any) { let filtered = this.cards.filter(card => { return card.profile .map(prof ...

Is it possible to streamline multiple $.fn. calls using javascript or jQuery?

I recently started using datatables and was advised to include the following code: $.fn.dataTableExt.oStdClasses.sWrapper = 'no-margin last-child'; $.fn.dataTableExt.oStdClasses.sInfo = 'message no-margin'; $.fn.dataTableExt.oStdClasse ...