I have come to realize that in my creation, my method does not function flawlessly as

I'm currently experimenting with creating a simple code using vue.js and d3.js. The goal is to plot some charts within the code, so I am attempting to allocate space for the charts by defining a method('add_svg') and executing the method in the created hook.

However, the method('add_svg') is not producing the desired outcome. To troubleshoot, I confirmed that the method was functioning correctly by including a console.log('Running') at the beginning of the method.

While I could see the message 'Running', the svg_space I intended to insert was not being added.

Interestingly, when I UPDATE charts using the same method in the computed hook, it works flawlessly. It seems to generate the necessary spaces multiple times in the computed hook, indicating that the method itself is functioning properly.

Why does this discrepancy exist?

Below is an excerpt from my code:

// CONSOLE.LOG DISPLAYS 'RUNNING' BUT SVG NOT APPENDED
created () {
  console.log("")
  this.add_svg() // append svg for chart
  this.plot([0, 0]) // basic plot with value = [0, 0]
},

...

// SAME METHOD YIELDS DESIRED RESULT WHEN RUN HERE.
// INITIALLY, I PREFER TO EXECUTE THE METHOD WITHIN created HOOK, NOT IN COMPUTED.
computed () {
  this.add_svg()
  this.plot(datum)
}

...

methods: {
  add_svg: function () {
    console.log("Adding some space for chart")
    d3.select('.chart_test2')
    .append('svg')
      .attr('class', 'svg_chart_test2')
      .attr('width', '10px')
      .attr('height', '10px')
  },
  other methods...
}

Answer №1

When working with Vue, it is essential to draw d3 charts within the mounted hook rather than the created hook. Trying to draw a chart before the page is fully rendered will result in an error, which is why the mounted hook is the appropriate place for this task.

mounted () {
  this.createChartSVG() // create SVG element for the chart
  this.renderChart([0, 0]) // render basic plot with values [0, 0]
}

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

What steps are needed to set up an SSL-secured reverse proxy on Nginx using Let's Encrypt?

I am currently using a RestAPI that is running on Node.js and it's being proxied through Nginx. The configuration for Nginx, with some parts hidden for privacy, looks like this: server { listen 80; server_name mywebsitestuff.com www.myweb ...

Unable to invoke a function from a separate file (using node.js with express)

I have a basic express app and I want to organize my socket listening functionality into separate files within a sockets directory (each file corresponding to a controller - separation of concerns). In app.js, here is what I am attempting: io.on('co ...

How can I center one middle position for my inputs and have the span align to the left?

How can I center my input fields with all the spans aligned to the left? I would like the inputs to be centered on the page, with supporting spans aligned to the left or right of the input lane. I am utilizing some Bootstrap 4 classes for this layout. C ...

Interact with socket.io and access variables with express-session

I am currently developing a chat feature for the customers of a website using express.js and socket.io. I am facing an issue in reading session values on the socket. Here is a snippet of my code. Any assistance would be greatly appreciated :) var express ...

Ways to obtain reactive values in Vue are diverse

There are numerous ways to obtain reactive values, and it is causing some confusion for me. Can someone explain the distinctions between prop.value, unref(prop), and toValue(prop)? Additionally, under what circumstances should each method be utilized? ...

Encountering a 500 error while trying to access a different file using the .get()

My current project involves setting up basic Express routing. However, every time I try to fetch data in order to link to a new page on my website, I encounter a 500 error. The main file managing the routing is Index.js. The content of Index.js: var expr ...

Issue with web scraping using fetch - promise remains unresolved

Despite mimicking all the request headers that Chrome sends, I am still encountering a pending promise that never resolves when trying to fetch a particular website. Below is my current code and headers: const fetch = require('node-fetch'); (a ...

Achieve consistent column heights with flexbox styling

I have a solution for achieving equal height columns using the CSS property display:table. Here is an example: .row { display: table; width: 100%; } .col{ display: table-cell; } However, in my case, I am using flexbox and the row class has ...

Challenges Arising from Using Jade Extends

Recently delving into Jade for a new project and finding it to be quite impressive so far. However, I've encountered a little snag with the Extends feature. The block I created doesn't seem to display when rendering layout.jade. layout.jade: di ...

The post() method in Express JS is functioning flawlessly in Firebase cloud function after deployment, however, it seems to encounter issues when running on a

https://i.stack.imgur.com/bIbOD.pngI am facing an issue with my Express JS application. Despite having both post() and get() requests, the post() request is not working on my local machine. It keeps throwing a 404 error with the message "Cannot POST / ...

Develop a revolutionary web tool integrating Node.js, MongoDb, and D3.js for unparalleled efficiency and functionality

I am exploring the creation of a web application that will showcase data gathered from various websites. To achieve this, my plan involves automating the process of data collection through web scraping. After collecting the data from these sites, I will fo ...

Is it feasible to obtain multiple tag-name indexes using JavaScript?

Exploring the table search function provided by W3Schools has brought up an interesting question in my mind. Is it feasible to simultaneously retrieve multiple indexes using getElementsByTagName and conduct a search across the entire table instead of just ...

Retrieving data from a script within a React component

How can I access a variable from a script inside a React component? I am performing device detection on Node and sending the resulting object to the client (React) within index.ejs. <script type="text/javascript">window.deviceType = <%- deviceT ...

Oops, looks like there's been an issue with the webpack build. It seems we're

I encountered an issue while building and running the development server using Webpack. My project is based on Vue.js, and I utilized vue-cli to generate it. Jest is used for testing, and running npm test poses no problems. However, when I run npm run bui ...

Error Encountered During Global Installation of NodeJS

I'm attempting to create a Node module that, when installed globally with the -g flag, can be run with a single command from the terminal. Although the tutorials I've followed suggest it should be straightforward, I seem to be missing something. ...

Creating a text container in PHP for displaying information

As someone who is new to PHP and HTML, I'm curious about the CSS statements needed to create a text box that will display a value from one of my PHP functions. Right now, the function retrieves data from an SQL server and returns it, but I would like ...

Is there a way to ensure that the border of a textarea matches the border of a text input?

My goal is to make the textarea fields in my form have the same appearance as the text input fields, regardless of the browser being used. I am interested in achieving this without imposing a customized style, but instead by leveraging the standard style w ...

Template for Joomla with a white stripe running along the right side

When I resize my browser to be half the width of my screen at www.thames.ikas.sk, a white stripe appears on the right side. Why is this happening? Why aren't my html and body elements taking up the full width of the browser? This issue doesn't oc ...

How can I send two responses in a single POST request using node.js?

Below is my router setup for handling responses: questionRouter.post('/questionsReply', (req, res) => { twilioResp(req, res); var newResponse = new Response(req.body); newResponse.save((err, data) => { if (err) return handleDBError(er ...

Creating tooltips using React and Tailwind CSS

I'm currently working on creating tooltips for icons in my header. I have created a component that combines the icon and tooltip into one div, with Tailwind CSS styles applied. JSX Component: const HeaderIcon = ({ icon, redirect = window.location.pat ...