Step-by-step guide to generating an organizational chart with vue-google-charts

After following the instructions for using the vue-google-charts plugin here: https://www.npmjs.com/package/vue-google-charts, I wanted to create an organization chart as shown in the example provided by Google Charts here: https://developers.google.com/chart/interactive/docs/gallery/orgchart.

I realized that I needed to use the onChartReady() method, but I was unsure how to implement it specifically for organization charts.

<template >
  <div class="container">
    <GChart
      type="OrgChart"
      :data="chartData"
      @ready="onChartReady"
    />
  </div>
</template>

<script>
import { GChart } from 'vue-google-charts'


  export default {
    components: {
      GChart
    },
    data () {
      return {
              // Array will be automatically processed with visualization.arrayToDataTable function
        chartData: [
          [{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'},
           '', 'The President'],
          [{v:'Jim', f:'Jim<div style="color:red; font-style:italic">Vice President</div>'},
           'Mike', 'VP'],
          ['Alice', 'Mike', ''],
          ['Bob', 'Jim', 'Bob Sponge'],
          ['Carol', 'Bob', '']
        ],
        options: {allowHtml : true}
      }
    },
    methods: {
          onChartReady (chart, google) {
            var chart = new google.visualization.OrgChart();
            chart.draw(this.chartData, this.options)
          }
      }
  }

</script>

When attempting to run the code above, I encountered a blank webpage with an error stating "Unhandled promise rejection TypeError: "google.visualization[type] is not a constructor". It seems like I need to enter something into google.visualization.OrgChart(), but I'm not entirely sure what is missing based on the provided code.

Answer №1

If you're looking to implement Google Charts and the organization chart package, shoutout to WhiteHat for pointing out the benefits of using these packages.

Make sure to include :settings and incorporate the orgchart package along with a callback function that triggers drawChart(). Check out vue-google-charts for more details. Also, take a look at Google's documentation on Loading Libraries . Begin by utilizing the code snippet below:

<template >
  <div class="container">
    <div id="tree">
    <GChart
      :settings="{ packages: ['orgchart'], callback: ()=>{this.drawChart()} }"
      type="OrgChart"
      :data="chartData"

    />
    </div>
  </div>
</template>

<script>
import { GChart } from 'vue-google-charts'


  export default {
    components: {
      GChart
    },
    data () {
      return {
              // Array will be automatically processed with visualization.arrayToDataTable function
        chartData: null
      }
    },
    methods: {
          drawChart() {
            this.chartData = new google.visualization.DataTable()
            this.chartData.addColumn('string', 'Name')
            this.chartData.addColumn('string', 'Manager')
            this.chartData.addColumn('string', 'ToolTip')

            // For each orgchart box, provide the name, manager, and tooltip to show.
            this.chartData.addRows([
              [{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'},
              '', 'The President'],
              [{v:'Jim', f:'Jim<div style="color:red; font-style:italic">Vice President</div>'},
              'Mike', 'VP'],
              ['Alice', 'Mike', ''],
              ['Bob', 'Jim', 'Bob Sponge'],
              ['Carol', 'Bob', '']
            ])

                // Create the chart.
            var chart = new google.visualization.OrgChart(document.getElementById('tree'));
            // Draw the chart, setting the allowHtml option to true for the tooltips.
            chart.draw(this.chartData, {allowHtml:true});

          }
      },

  }

</script>

<style>
  table {
      border-collapse: inherit;
      border-spacing: 0;
  }
</style>

Answer №2

Struggling to set up the same Vue package I've been using, it took a lot of trial and error but finally figured out how to load most Charts efficiently...

Within the <template>

<GChart
  type="Table"
  :data="chartData"
  :options="chartOptions"
  :settings="{ packages: ['bar', 'corechart', 'table'] }"
  />

The key is specifying the Type of Chart you want in the `type` attribute (ColumnChart, LineChart, Table).

This approach simplifies things as all we need in Vue's data() is chartData: null where we can fetch data using axios.

This method seems to be the most efficient with minimal extra code needed.

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 is causing the element to disappear in this basic Angular Material Sidenav component when using css border-radius? Check out the demo to see the issue in action

I have a question regarding the Angular Material Sidenav component. I noticed that in the code below, when I increase the border-radius property to a certain value, the element seems to disappear. <mat-drawer-container class="example-container" ...

What is the best way to utilize clip() in html canvas when using text as a path?

I have written the code below, which is close to what I am trying to achieve but doesn't quite get there. My goal is to fill text in multiple colors, currently it is only in #FF00FF. Check out the Playground I suspect the issue lies in my lack of kn ...

Ways to modify the shown data in dojox DropDownSelect

I have a DropDownSelect element set up with the following HTML tags: <select id="someId" dojoType="dojox.form.DropDownSelect" > <option>Loading...</option> </select> Now, when the xhr load function is triggered, I want to replace ...

Tips for using conditional rendering with React and TypeScript

Issue with Conditional Rendering in TypeScript It seems like I might have encountered a problem with the way I declare my components. Take a look at this TypeScript snippet: import React, { FunctionComponent } from 'react'; export const Chapte ...

Update the scope parameter in an alternate perspective

I've been struggling with a particular issue for quite some time now and I seem to be stuck. Here's what I'm trying to achieve: I want to change the value of my scope in one view by clicking on a button in another view. For instance, if I&ap ...

Displaying Text on Mouseover with JQuery

I have created a unique set of graphs that unveil hidden text when hovered over with the mouse. To start, I carefully outlined the layout of my text: <article class="fundamental_metrics"> <h2>Fundamental Metrics</h2> <p id="nu ...

The 'function' is not defined in Edge and Explorer browsers

I am currently working on a web page in SharePoint and have encountered an issue where the onclick referenced function throws an error only in Internet Explorer 11 and Microsoft Edge. However, in Chrome, Firefox, and Opera, everything works perfectly fine. ...

Using a JavaScript class rather than an ID for toggling visibility

As a complete newbie to JavaScript, I've come across similar questions but I'm lost when it comes to coding - help needed! So here's the code I have so far, and I'm hoping someone can assist me. Currently, I have JavaScript set up to s ...

Scroll to the top of a new page with Material UI's table component

Feeling a little stuck here. I've tested various settings with scrollTop, but haven't had much luck. To clarify, I'm working with Material UI and their pagination features in conjunction with a table (you can find the documentation here). W ...

How to interact with objects/methods in React/Three.js from outside of useEffect

I'm looking to include controls for manipulating the rubiks object, which is created in useEffect. These controls consist of a series of buttons that should trigger functions within the cube class (to which rubiks belongs) stored in a separate compone ...

HTML stubbornly resists incorporating Javascript [UIWebView]

Currently, I am facing an issue while trying to animate a color property using jQuery 1.7.1 and the jquery color plugin downloaded from this link. I have ensured that all necessary files are included and part of the build target. Here is a simplified versi ...

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 ...

Unable to retrieve parameter while making a POST request

Need some help with attribute routing. I'm having trouble getting parameters from the HTTP body. The ConnectionID Class includes a property named CValue. $('#btn').click(function () { $.ajax({ type: "POST", url: "http:// ...

Angular Material 2: Error - Could not be located

I have successfully installed Angular CLI globally, followed by Angular Material 2. https://i.sstatic.net/xNQqD.png In the process, I configured angular-cli-build.js & system-config.ts as shown below. angular-cli-build.js // Angular-CLI build configura ...

Bidirectional Data Binding with Computed Setter in Vue.js

Exploring the concept of two-way data binding with a basic example: <template> <input v-model="fullname"/> <input v-model="first"/> <input v-model="last"/> </template> <script> var app = new Vue({ el: '#a ...

Selecting CSS paging in JQuery Jtable

I am currently working with Jquery JTable and encountering an issue with pagination. The numbers displayed on the selects for "Go to Page" and "Row Count" are not appearing correctly. Any suggestions on how I can resolve this? Thank you, Nk ...

Is there a method to deactivate multiple form elements simultaneously?

My goal is to deactivate a specific section of HTML form elements based on certain conditions. I initially tried using the following method: <fieldset disabled> <input value="one" /> <input value="two" /> </fie ...

Incorporate a captivating background image into your SVG design

Currently working with Snap.svg, my aim is to include a background image on a polygon shape. The specific polygon in question is outlined as follows: <svg id="test" height="600" width="600" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= ...

Creating dynamic toggle buttons in HTML using JavaScript and setting up their initial state

Currently, I have a div element: <div id="div1"> </div> I am attempting to create "toggle" buttons using JavaScript. These buttons can be created with the help of . <script> var count = 1; function foo() { var newBut ...

Determine the number of radio buttons selected in each group (At least 2 selections in total)

I am looking to validate 7 sets of Radio Buttons, ensuring that a minimum of 2 sets are selected. If not, an alert should be displayed. However, it's important to note that there are additional radio buttons on the page that are not part of this valid ...