Conceal Segment within Google Doughnut Graph

I have implemented a Google Donut Chart.

Occasionally, I encounter the following data:

{
    DATA_1: 10,
    DATA_2: 15,
    INVALID_DATA: 10000000 (Large Number)
}

When this occurs, my valid data appears as a very thin slice or is not visible in the chart.

Is there a feature in Google Charts that allows me to hide a specific slice to enhance the visibility of other slices?

I would like the valid data to be displayed as a percentage alongside the INVALID_DATA, while simply hiding the INVALID_DATA Slice.

Answer №1

There are no direct options available on the chart itself to hide a slice, but it can be achieved using a DataView.

However, when hiding a slice, it may affect the relative size of the remaining slices compared to the hidden one.

In the example below, a column is added to calculate the percentage without considering the hidden slice.

The option pieSliceText: 'value' is then utilized to display the true percentage of each slice.

A DataView is employed to conceal the original value column and the row containing the larger slice.

google.charts.load('current', {
callback: function () {
var data = google.visualization.arrayToDataTable([
['Data Type', 'Value'],
['DATA_1', 10],
['DATA_2', 15],
['INVALID_DATA', 10000000]
]);

var options = {
pieHole: 0.4,
pieSliceText: 'value',
theme: 'maximized',
height: 262,
width: 262,
};

// Calculating total sum
var dataGroup = google.visualization.data.group(
data,
[{column: 0, type: 'string', modifier: function () {return '';}}],
[{column: 1, type: 'number', aggregation: google.visualization.data.sum}]
);

var hideRows = [];
data.addColumn({type: 'number', label: '%'});
for (var i = 0; i < data.getNumberOfRows(); i++) {
// Setting % value
data.setValue(i, 2, data.getValue(i, 1) / dataGroup.getValue(0, 1));

// Hide the big number
if (data.getValue(i, 2) > .99) {
hideRows.push(i);
}
}

var numberFormat = new google.visualization.NumberFormat({
pattern: '#,##0.00000 %'
});
numberFormat.format(data, 2);

var dataView = new google.visualization.DataView(data);
dataView.hideColumns([1]);
dataView.hideRows(hideRows);

var pieChart = new google.visualization.PieChart(document.getElementById('pieChart_div'));
pieChart.draw(dataView, options);

var tableChart = new google.visualization.Table(document.getElementById('tableChart_div'));
tableChart.draw(data);
},
packages: ['corechart', 'table']
});
div {
padding: 2px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="pieChart_div"></div>
<div id="tableChart_div"></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

Utilizing objects as tags within the Form Tags component in Bootstrap Vue

Let's break down the following code snippet from the documentation: <template> <div> <b-form-tags v-model="value" no-outer-focus class="mb-2"> <template v-slot="{ tags, inputAttrs, inputHandlers, addTag, removeTag }"> ...

How should I integrate my JS authentication function in Rshiny to enable the app to utilize the outcome?

Currently, I have an Rshiny application set to be published on the server but in order to ensure that a specific user has access, we require an API authentication token. The process of authentication is handled within JS tags outside of the application, wh ...

Is it possible to target only those divs within a class that wrap to a new line?

I currently have a series of 7 to 12 divs with the same styling, all floated to the left. Is there a CSS selector that can target the ones flowing onto a second row? I doubt this is achievable with standard CSS, so I'm curious if anyone has any jQuery ...

Issue with Firefox browser: Arrow keys do not function properly for input tags in Twitter Bootstrap drop down menu

Requirement I need to include a login form inside the dropdown menu with username and password fields. Everything is working fine, except for one issue: Issue When typing, I am unable to use the arrow keys (up/down) in Firefox. This functionality works ...

Waiting for multiple asynchronous calls in Node.js is a common challenge that many developers

I'm facing a dilemma trying to execute multiple MongoDB queries before rendering a Jade template. I am struggling to find a way to ensure that all the Mongo Queries are completed before proceeding with rendering the template. exports.init = funct ...

Modifying worldwide variables within an ajax request

After spending considerable time attempting to achieve the desired outcome, I am faced with a challenge. My goal is to append the object from the initial ajax call after the second ajax call. However, it appears that the for loop is altering the value to ...

Error: React 404 - Unable to make a POST request with

I am new to utilizing Axios and Node.js as a backend technology. I recently inherited some React code for a login page and I'm struggling to understand why I'm unable to send a POST request to the backend. Below is my .env file: REACT_APP_BACKEN ...

Connect my asp.net grid view to JavaScript using AJAX

I am seeking guidance on how to bind my gridview from JavaScript in an ASP.NET web forms application. My objective is to click a linkbutton within my gridview and have it trigger a modalpopup that displays another gridview. Here are snippets of my code s ...

Crafting a clever smart banner using jquery.smartbanner for Android Firefox users

In order to display smartbanners on my mobile website, I utilize jquery.smartbanner. This implementation has been successful for ios, windows phone, and the default android browser. However, when testing on Firefox for android, the smartbanner is not visib ...

Utilizing columns within the 'segment' element in Semantic-ui to enhance layout design

I am looking to divide the ui-segment into 3 columns and display the ui-statistics evenly. Below is the code I have used in an attempt to achieve this: <div class="ui container"> <div class="ui segment"> <h3 class="ui header"> ...

Is it possible to provide unrestricted support for an infinite number of parameters in the typing of the extend function from Lodash

I am utilizing the "extend" function from lodash to combine the objects in the arguments as follows: import { extend } from 'lodash'; const foo1 = { item: 1 }; const foo2 = { item: 1 }; const foo3 = { item: 1 }; const foo4 = { item: 1 }; const f ...

What is the best way to incorporate variables into strings using JavaScript?

Can someone help me achieve the following task: var positionX = 400px; $(".element").css("transform", "translate(0, positionX)"); Your assistance is greatly appreciated! ...

I encountered an issue when trying to include the dotenv file, receiving the following error message: [TypeError: Network request failed]

babel.config.js File plugins: [ ["module:react-native-dotenv", { "envName": "APP_ENV", "moduleName": "@env", "path": ".env", "blocklist": null, "allowlist": null, "blacklist": null, // DEPRECATED "whitelist": ...

What is the process of choosing a language (English/French) within a pop-up?

<body style="text-align:center"> <h2>Popup</h2> <div class="popup" onclick="myFunction()">Interact with me to show/hide the popup! <span class="popuptext" id="myPopup">A Simple Popup!</span> </div> <script& ...

Unable to save input from <from> in the React state object

I'm currently working on a project in reactjs where I need to store user input in the react state object. I followed an example from reactjs.com, but it seems like the input is not being stored in the state object as expected. class CreateMovieForm ex ...

Array indexes can be negative

My approach to storing objects for a grid involves using a two-dimensional array. In this case, the grid is hexagonal, making it more convenient to utilize a coordinate system centered at (0, 0) and ranging from -r to r. The following source suggests that ...

Sending back numerous information in the catch block

Recently, I was testing out the fetch API and encountered an issue with logging a fetch error. I tried catching the error and logging it within the catch block. Strangely, even when I added additional console.log statements in the catch block, they would ...

Issue: React does not allow objects as children for rendering. (You have provided an object with keys {user})

Currently in the process of implementing the authentication flow for my app utilizing the context API const AuthContext = createContext({}); export const AuthProvider = ({ children }) => { return ( <AuthContext.Provider value={{ user: " ...

Creating static HTML files for non-static pages using Next.js SSR/ISR

While troubleshooting an issue with a specific page, I noticed that a static HTML file was created for a non-static page using Next.js. Is this expected? The page, which we will refer to as "page1," does not include the functions getStaticPaths() or getSta ...

Objects hidden in the darkness, utilizing the traditional MeshPhongMaterial

Currently struggling with my shadows issue. Here is a snapshot of the problem I am encountering: https://i.sstatic.net/odnuE.png https://i.sstatic.net/RZitM.png The presence of those lines puzzles me, as the scene should be evenly illuminated! The shado ...