The basic Vue.js application is not displaying Highcharts properly. Instead, an empty div is being shown

I have a Vue js application that is not rendering a simple highcharts bar chart. Although I am able to console the data fetched from the database, I am struggling to display it in the chart.

As a newcomer to Vue js, I am unsure of what I might be missing in my code.

package.json

{
  "name": "my-dashboard",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^1.6.8",
    "core-js": "^3.8.3",
    "crypto-browserify": "^3.12.0",
    "dotenv": "^16.4.5",
    "highcharts": "^11.4.1",
    "highcharts-vue": "^2.0.1",
    "os-browserify": "^0.3.0",
    "path": "^0.12.7",
    "path-browserify": "^1.0.1",
    "stream-browserify": "^3.0.0",
    "vm-browserify": "^1.1.2",
    "vue": "^3.2.13"
  },
  "devDependencies": {
    "@babel/core": "^7.12.16",
    "@babel/eslint-parser": "^7.12.16",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-eslint": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "eslint": "^7.32.0",
    "eslint-plugin-vue": "^8.0.3"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "browser": true,
      "es2021": true
    },
    "extends": [
      "plugin:vue/vue3-essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "@babel/eslint-parser",
      "ecmaVersion": 2021
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead",
    "not ie 11"
  ]
}

componenets/BarChart.vue

<template>
  <div class="chart-container">
    <highcharts :options="chartOptions"></highcharts>
  </div>
</template>

<script>
import Highcharts from "highcharts";
import axios from "axios";

export default {
  name: "BarChart",
  components: {
    Highcharts,
  },
  data() {
    return {
      chartOptions: {
        chart: {
          type: "column", // Use "column" for bar chart
        },
        title: {
          text: "Crop Production by Country",
        },
        xAxis: {
          categories: ["USA", "China", "Brazil", "EU", "India", "Russia"],
          crosshair: true,
        },
        yAxis: {
          title: {
            text: "1000 metric tons (MT)",
          },
        },
        tooltip: {
          valueSuffix: " (1000 MT)",
        },
        series: [
          {
            name: "Corn",
            data: [406292, 260000, 107000, 68300, 27500, 14500],
          },
          {
            name: "Wheat",
            data: [51086, 136000, 5500, 141000, 107180, 77000],
          },
        ],
      },
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await axios.get(
          "http://localhost:3001/api/web?startDate=2023-04-01&endDate=2023-04-03"
        );
        const data = response.data;
        console.log(data);

        // Assuming data from API is properly formatted
        const categories = [
          ...new Set(data.map((item) => item.report_date)),
        ].sort();

        // Update chart options with fetched data
        this.chartOptions.xAxis.categories = categories;
        this.chartOptions.series[0].data = data.map((item) => item.value);

        // Set new chart options to trigger reactivity
        this.chartOptions = { ...this.chartOptions };
        console.log(this.chartOptions);
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    },
  },
};
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 400px; /* Set a desired height for the chart container */
}
</style>

App.vue

<template>
  <div id="app">
    <h1>Multiple Charts Example</h1>
    <BarChart />
  </div>
</template>

<script>
import BarChart from "./components/BarChart.vue";

export default {
  name: "App",
  components: {
    BarChart,
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

This code snippet only displays "Multiple Charts Example" without rendering the actual charts. What could be causing this issue?

Please advise on how to resolve this problem.

Answer №1

The Highcharts library you are using is not meant to be treated as a component. When you include app.use(HighchartsVue) in your installation process, it automatically globally registers the <highcharts> component for you. By manually importing and registering Highcharts in your component file, you are actually causing errors.

To fix this issue, delete the following code from your BarChart.vue file:

import Highcharts from "highcharts";
components: {
  Highcharts,
},

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

unable to send response from the controller to ajax request

In the world of Phalcon framework, I am faced with a challenge on my blog. I want to dynamically append comments using AJAX, but the real issue lies within my controller query. I am struggling to grasp how to return only the comment data to the view with A ...

Using selected option from dropdown as a value for a hyperlink

I'm attempting to transfer the chosen value from a dropdown menu to my link. This way, when I click on the specific link, it should trigger the corresponding action based on the ID selected from the dropdown. The alert shows the value, but now I need ...

What is the method for retrieving highlighted text using JavaScript?

I need assistance with a DIV field that contains lyrics. I am looking for a way to detect when a user drags and selects a portion of the lyrics. Here is an example: In the provided example, the user has dragged and selected the part with a blue background ...

I encountered an issue during test payment where the error message "The class 'Stripe' was not found" appeared

Upon clicking the submit payment button, I encountered the following error message: Fatal error: Class 'Stripe' not found in /opt/lampp/htdocs/stripe-php/stripe_api.php on line 10 Despite including the Stripe.php file in my code using require ...

Tips for Testing an Ajax jQuery Function Within the document.ready Event

I am in the process of developing a script that utilizes $.ajax to make requests for a json api. My goal is to create unit tests that can verify the results received from the ajax request. For instance, I expect the returned JSON object to contain "items" ...

Is there a way for me to monitor the ngSrc attribute of an image element and retrieve the updated width and height within my personalized directive?

Here is a snippet from index.html: <img ng-src="{{ImageURL}}" my-image/> This is from app.js: var app = angular.module('plunker', []); app.controller('MyCtl', function($scope) { $scope.ImageURL = ""; $scope.ImgWidth = 0; ...

The required version of @angular/compiler-cli must be at least 2.3.1. The current version appears to be "undefined"

While attempting to run ng serve, I encounter the following error: The required version of @angular/compiler-cli is 2.3.1 or higher. The current version is "undefined". This is how my package.json file looks: "dependencies": { "@angular/common": "2. ...

deliver alerts to the user on asp.net

In my attempt to send notifications using ASP.NET, I encountered an issue. When an admin logs in and clicks on "add message", the intention is for this message to be sent to a user. Upon the user logging in, a pop-up notification should appear. I have tr ...

Utilizing Axios for communication between backend and frontend

My development setup includes reactjs for the frontend and mongoDB for the backend database. Below is the backend react code: const express = require("express"); const jwt = require("jsonwebtoken"); const bcrypt = require("bcrypt"); const { User, Verifica ...

Dynamic parameters can be passed in Rails using the link_to method

Feeling a bit stuck, maybe this is just a simple question. I have a sort button that I need to use to organize my list of questions. To do this, I create a link_to like this: <%= link_to xx_path(:is_sort => true, :remote=> true , :method=> :p ...

The issue of assets not being resolved in Nx React is causing frustration

Currently, I am involved in a nx monorepo project that consists of several libraries and two applications. Directory Structures apps \--api \--funnel (react with webpack) \--api-e2e \--funnel-e2e libs \--funnel &bsol ...

Can TypeScript and JavaScript be integrated into a single React application?

I recently developed an app using JS react, and now I have a TSX file that I want to incorporate into my project. How should I proceed? Can I import the TSX file and interact with it within a JSX file, or do I need to convert my entire app to TSX for eve ...

Leveraging jQuery to extract the value from a concealed form field

Seeking assistance with a jQuery issue... I am attempting to use jQuery to retrieve the values of hidden fields in a form. The problem I am facing is that there are multiple forms displayed on the same page (result set items for updating), and the jQuery ...

Display the keys of a nested array in Vue.js when the structure is unknown

Here is a representation of a dynamic array I have: nodes: [ { n1: "Foods", }, { n4: "Drinks", b7: [ { a2: "Beers", a4: [ ...

Insert the entered value into the table

I'm struggling to extract the content from an input textfield and insert the value into a table row. However, every time someone submits something, the oldest post shifts down by one row. Below is my current attempt, but I'm feeling quite lost at ...

Generate an li element that is interactive, containing both text and a span element

I am dealing with a JSON array that looks like this: var teamDetails=[ { "pType" : "Search Engines", "count" : 5}, { "pType" : "Content Server", "count" : 1}, { "pType" : "Search Engines", "count" : 1}, { "pType" : "Business", "count" : 1,}, { "pTyp ...

Double Trouble: KnockoutJS causing an ajax form submission glitch

Dealing with a form that is persistently submitting twice can be quite frustrating. I've gone through various jQuery-related queries on Stack Overflow, but haven't come across a definitive solution yet. Additionally, scouring Google groups for an ...

Problem with AJAX functionality, functioning properly on all browsers except for Internet Explorer

I have noticed that this code works perfectly in Chrome and Firefox, but unfortunately not in IE. This is expected due to browser compatibility issues. Can anyone identify any problems with this code that would make it ineffective in IE? var waittime=400 ...

What is the reason for having two plugin declarations within the galleriffic.js file?

I am currently working on enhancing the functionality of galleriffic.js by implementing a feature that will update a <div> element with text content as images are being changed. However, I am facing some challenges understanding the code. What perpl ...

Angular2: The index.html file is not being displayed, it simply shows the message "loading..."

Having some trouble binding the file to the "my-app" selector. Check out the plnkr link: http://plnkr.co/edit/2L5jSRK3adPZWTUwWdcP?p=preview UPDATE: It seems like the plunker can display content if I place the template code inside the <my-app> tags ...