Can more than one label be assigned to a single bar in a Google column chart?

Is it feasible to include an extra label on every column chart bar without affecting the appearance of the bar itself?

This is how my current chart is arranged:

this.populationChart.myChartObject.data = {
    'cols': [
        { id: 's', label: 'Stage', type: 'string' },
        { id: 'v', label: 'Value', type: 'number' },
        { id: 'p', label: 'Percent', type: 'number' },
        { role: 'style', type: 'string' }
    ], 'rows': [
        // data rows here
    ]
};

calcPercent = (numerator: number, denominator: number): number => {
    if (denominator === 0) {
        return 0;
    };
    if (denominator !== 0) {
        return (Math.round((numerator / denominator) * 100));
    };
};

Currently, each stage shows two bars - one for the value label and one for the percentage label.

What I am aiming for is to have each bar's height determined by the value label. When hovering over a bar, I want the popup to display the value label and its value, as well as the percentage label.

In essence, I wish for the bar's visual representation to be based solely on the value label, while both labels are shown when moused over. Is this achievable?

Answer №1

Let's take a look at this example:

   let information = google.visualization.arrayToDataTable([
     ['Element', 'Density', { role: 'style' }, { role: 'annotation' } ],
     ['Copper', 8.94, '#b87333', 'Cu' ],
     ['Silver', 10.49, 'silver', 'Ag' ],
     ['Gold', 19.30, 'gold', 'Au' ],
     ['Platinum', 21.45, 'color: #e5e4e2', 'Pt' ]
  ]);

Now, here are the required edits for the labels to be updated:

  <script type="text/javascript" 
src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript">
    google.charts.load("current", {packages:['corechart']});
    google.charts.setOnLoadCallback(drawChart);
    function drawChart() {
      let information = google.visualization.arrayToDataTable([
        ["Element", "Density", { role: "style" } ],
        ["Copper", 8.94, "#b87333"],
        ["Silver", 10.49, "silver"],
        ["Gold", 19.30, "gold"],
        ["Platinum", 21.45, "color: #e5e4e2"]
      ]);

      let view = new google.visualization.DataView(information);
      view.setColumns([0, 1,
                       { calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation" },
                       2]);

      let options = {
        title: "Density of Precious Metals, in g/cm^3",
        width: 600,
        height: 400,
        bar: {groupWidth: "95%"},
        legend: { position: "none" },
      };
      let chart = new google.visualization.ColumnChart(document.getElementById("columnchart_values"));
      chart.draw(view, options);
  }
  </script>
<div id="columnchart_values" style="width: 900px; height: 300px;"></div>

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 the best way to determine if an array of objects is present in the state of a React

Currently, I am in the process of developing a dynamic form using React JS. One aspect of my form involves an input field with a limit of 10 characters. To enhance the cleanliness of my code, I have constructed an object and utilized mapping to streamline ...

Toggle the visibility of fields using dynamic identifiers (NodeJS, Mongoose, Express, JavaScript)

I am using a forEach loop to display menus for various restaurants when the restaurant icon is clicked. The number of restaurants/menus and their icons are unknown, each created with a dynamic ID such as menu-0, menu-1, menu-2, and so on. When one restaur ...

Use Angular.js to perform navigation after clicking the "Ok" button on a confirmation box

I encountered a problem with my requirement. I need a confirm box to appear when the user attempts to navigate to the next state/page. Only if the user clicks on the "Ok" button should it proceed to the next state; otherwise, it should stay as it is. Below ...

Angular 13: Issue with Http Interceptor Not Completing Request

In my app, I have implemented a HtppInterceptor to manage a progress bar that starts and stops for most Http requests. However, I encountered an issue with certain api calls where the HttpHandler never finalizes, causing the progress bar to keep running in ...

Exploring the concept of kleisli composition in TypeScript by combining Promise monad with functional programming techniques using fp-ts

Is there a way to combine two kleisli arrows (functions) f: A -> Promise B and g: B -> Promise C into h: A -> Promise C using the library fp-ts? Having experience with Haskell, I would formulate it as: How can I achieve the equivalent of the > ...

Some filters in Bootstrap table not displaying properly

I've been struggling to showcase this bootstrap table with the data-filter-control attribute, but I'm unable to achieve it successfully. The filters are failing in two main ways: The filter is not showing all the categories from that column The ...

Exploring Objects without the need for loops

Currently, I am focusing on optimizing the performance of the following objects: var scheduleFee = { poor = {level1:25,level2:25,level3:25} , good = {level1:15,level2:20,level3:25} , vgood = {level1:10,le ...

Attain worldwide reach

I'm currently facing a Scope issue that has been quite challenging to resolve. The saying goes, "a picture is worth a thousand words," and in this case, it couldn't be more true. Whenever the OK or REJ buttons trigger the reject() function, passi ...

The functionality is verified in Postman, however, it is not functioning properly when accessed from my client's end

I am working on a project where I have a button in my client's application that is supposed to delete a document from a MongoDB collection based on its ID. Here is the backend code for this functionality: index.js: router.post('/deletetask' ...

What is the reason for parsing JavaScript code when the page is first loaded?

I recently placed an Adsense (for content) code on my website in order to show a single ad: <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"> </script> <ins class="adsbygoogle" style="display:inline-block;width ...

Unspecified ID of the most recently added record

Can anyone help me retrieve the last inserted id? $stmt = $db->query("SELECT LAST_INSERT_ID();"); $row = $stmt->fetch(); echo $row['id']; JavaScript: success: function(data) { console.log(data); } The result shows as undefined! ...

Creating a rotating HTML5 video and exporting the result as a canvas image

I'm currently using the jpeg_camera library to display an HTML5 Video on my website (https://github.com/amw/jpeg_camera) Here's how the video output looks in my HTML: <video autoplay="" src="blob:http://localhost:49209/41c42143-5d0e-4525-8fe ...

Several buttons, each requiring to show distinct text

Currently, I am attempting to enhance an existing project that was graciously created for me. I must admit, I am quite new to this, so please be patient with me! In my project, there are 9 buttons, each triggering the display of a different image upon bei ...

Check the functionality of a React functional component by conducting unit tests on its functions

Is there a way to properly unit test a function within a Functional component in React? Since wrapper.instance() will return null for functional components, what is the best approach to ensure this function is included in testing to achieve maximum coverag ...

Using {children} in NextJS & Typescript for layout components

I am looking to develop a component for my primary admin interface which will act as a wrapper for the individual screens. Here is the JavaScript code I have: import Header from '../Header' function TopNavbarLayout({ children }) { return ...

Encountered a React TypeScript issue stating that the type '{ ... }' cannot be assigned to the type 'IntrinsicAttributes & IntrinsicClassAttributes<...>'

Embarking on a new journey with a react typescript project, I encountered this puzzling error: Failed to compile. /Users/simon/Code/web/react-news-col/src/MainNewsFeed.tsx TypeScript error in /Users/simon/Code/web/react-news-col/src/MainNewsFeed.tsx(27,35 ...

Using Angular UI TinyMCE to import a Word file

Can I import a local word document into the text area generated by the Angular UI TinyMCE directive? ...

Prevent animations on child elements with Vue.js

I am dealing with a scenario where I want to remove the fade transition on a child div within a <transition> component. The reason for nesting it is to prevent layout flickering, which can be demonstrated in a fiddle if necessary. In the fiddle belo ...

Having trouble with updating AngularJS?

I am facing an issue while updating the guitar object in my AngularJS application. The operation is performed using the updateGuitar controller with ngMock backend. After updating the guitar object, the put request is processed by the carService. However, ...

When defining properties/data in Vue mixins, the properties/data of the mixin are not accessible

A vue mixin is being used to store information (referred as `world` in the example below) that needs to be accessed in multiple vue components without having to import it every time. Check out the code snippet: <template> <ol> <li> ...