Exploring the values of data in c3js through data events

Is there a way to extract values from the Company data array within the onclick event? Currently, using the api functions only allows access to the Users array.

var chartCompany = c3.generate({
    bindto: '#users-chart',        
    data: {
        x: 'Company',
        url: '/ajax_call',
        mimeType: 'json',
        type: 'bar',
        axes: {
            Company: 'x'
        },
        onclick: function (d, i) { console.log(chartCompany.data()); }
    },
    axis: {
        x: {
            type: 'category',
             show: false
        },
    }
});

The json response received from the server is as follows:

{
 "Company": ["Company 1", "Company 2", "Company 2"],
 "Users"  : [10, 20, 30]
}

Any assistance or ideas on this matter would be greatly valued.

[Edit 1] Further details on my inquiry: When clicking on a bar representing Users, I aim to retrieve the associated Company information.

[Edit 2] Here's a functional static example: http://jsfiddle.net/et37a9t2/

Answer №1

After experimenting with various methods, I stumbled upon a hidden gem within the chart object itself - a property called categories. This property is only populated when the x-axis is declared as type category, like in my particular scenario. To extract data from the x-axis, simply use the .categories() function.

        ...
        },
        onclick: function (d, i) { console.log(chartCompany.categories()[d.index]); }
    },
    ...

By clicking on a bar in the chart, you can easily retrieve the corresponding category information.

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

Merge rxjs streams, identify modifications, and yield a single result

In the context of using Angular with .net Core WebApi, let's consider the development of a time management application designed to monitor task durations. The user initiates a task on the front end which triggers a timer displaying elapsed time. The ...

Container-wrapper unable to contain images, overflowing despite using overflow:hidden property

I am currently working on a unique section for a website that includes a .container-wrapper element consisting of text and multiple images. The container is set to have overflow: hidden; in order to prevent any overflow issues. However, the images within t ...

Is there a way to verify the phone number input field on my registration form along with the country code using geolocation

I'm currently working on a registration form that includes an input field for telephone number. I'd like to implement a feature where, upon filling out the form, the telephone input field automatically displays the country code by default. Would ...

AngularJS 1.x directive experiencing unresponsive controller

Currently, I am attempting to organize my menu using a directive. Here is the structure I have set up: function Menu(){ self = this; self.tab = ''; self.selectedTab = function(tab){ self.tab=tab; console.log('updating tab&a ...

Dynamically attach rows to a table in Angular by triggering a TypeScript method with a button click

I need help creating a button that will add rows to a table dynamically when pressed. However, I am encountering an error when trying to call the function in TypeScript (save_row()). How can I successfully call the function in TypeScript and dynamically a ...

The $formatters directive plays a role in modifying the ngModel as it is

I am facing an issue with using $formatters. My objective is to conceal the phone number, while displaying only the last 4 characters. It should not display anything if left blank. If text is entered, the model is impacted by the mask and the hidden phone ...

Utilizing Django for parsing JSON files

I am currently working with a json file generated by scrapy in the following format: [ { "area_of_interest": [ "Pharmaceutical" ], "department": [ "RETAIL PHARMACY: APOTHECARY" ], "duti ...

Is it possible for me to include a static HTML/Javascript/jQuery file in WordPress?

My extensive HTML file contains Javascript, jQuery, c3.js, style.css, and normalize.css. I am wondering if I can include this file as a static HTML page within Wordpress. Say, for instance, the file is called calculator.html, and I want it to be accessib ...

Unveiling ContentVeil.js and the Complications of Forced Synchronous Layouts

My php page is experiencing delays of 3-10 seconds after loading, making it impossible to scroll or close the tab. This issue occurs in both Chrome and IE, with the loading gif continuing to loop. In the Chrome Timeline (link: https://i.sstatic.net/MxuAl. ...

Custom JSON.NET Resolver for Resolving Sub-Property Names

I am facing an issue with my API that retrieves a JSON object from a MongoDB where one of the properties can contain any valid JSON data and has dynamic property names. The challenge is to serialize this particular property exactly as it is stored in the d ...

Utilizing ES6 promises in node.js to send a null response

I'm looking for assistance on how to execute a query using ES6 native promises in node.js. The code I have below is what I've been working with: let arr= []; conn.query('select * from table1', (err, b) => { for (let i = 0; i ...

The div is populated after the onload event is finished

Currently, my onload event is functioning correctly. However, I am encountering an issue where the second part of the onload event calls another function before the first part is finished. The initial part inserts information into an input field, while th ...

Generating columns dynamically in Angular Material 2

Exploring the functionalities of Angular Material ng-table, I am interested in dynamically generating columns. The code snippet provided below demonstrates a table template with statically defined columns. My goal is to replace these column definitions wit ...

Python struggles to iterate and find an existing JSON entry

Currently in the process of learning python and working on creating a master list for IPFS hash descriptions. Each time I generate a new IPFS hash, I plan to add it to this list along with a brief description. This will allow me to easily search for specif ...

Using maxCDN to deliver static files within a Node application

Our current project is built using keystone and nunjucks, with all paths to static files following the format /stc/img/someimage.jpg. I am looking for a way to serve these files through middleware in our node server from maxCDN. Is there a solution that ...

Generate a pair of dates and organize them in chronological order

Working on an application where the database response lacks a direct timestamp, causing data to render differently each day and requiring me to use .reverse() to fix my charts. I aim to implement a permanent solution using sort(), but struggling due to the ...

receiving error notification from struct that conforms to Codable

Deciphering a JSON structure is currently my task, and in case the decoding process fails, it indicates that one of the fields is absent from the server's response. My aim is to inform the user about this missing field. Here is the structure being de ...

Unable to produce audio from files

Attempting to incorporate sound files into my project using https://github.com/joshwcomeau/redux-sounds but encountering difficulties in getting it to function. Below is the code snippet I utilized for setup. Unsure if webpack is loading the files correctl ...

Stop the jQuery custom slide animation when there are no more items to display

I have designed a unique slider for users to view the work process https://i.sstatic.net/FLYne.png When a user clicks the button, the slider will move left or right depending on the button clicked. However, if the user clicks multiple times, the slider ma ...

Enhance your viewing experience with the Zoom feature that activates when

I recently noticed on that I am able to zoom/enlarge a photo by clicking on it. Is there a way for me to incorporate this feature without purchasing the entire theme? While I understand the theme is designed for purchase, I only need this specific functi ...