Altering the color scheme of a specific column within a stacked bar chart using C3.js

I'm currently facing a challenge with highlighting specific values in a c3.js stacked bar chart. While I was able to change the color of an individual bar in a non-stacked bar following this example, I'm struggling to determine how to identify the indexes of values within a single stack. Here's my current JS code:

var chart = c3.generate({
  bindto: '#chart1',
  data: {
    x: 'x',
    columns: [
      ['x', "1ST", "2ND", "3RD", "4TH"],
      ['A', 6, 8, 2, 9],
      ['B', 3, 4, 1, 6],
      ['C', 4, 4, 2, 4]

    ],
    type: 'bar',
    groups: [
      ['A', 'B', 'C']
    ],
    colors: {
      A: '#666666',
      B: '#cccccc',
      C: '#428bca',
    },
    order: 'asc'
  },
  axis: {
    x: {
      type: 'category'
    }
  }
});

The approach taken in the example is:

color: function (color, f) {
   return f.index === 4 ? "#dd0" : "#ddd";
}

I am still working on this and you can track my progress on JSFIDDLE. My question is how can I obtain the indexes of the three values within a single stack and then use them to customize their colors? For instance, if I want to highlight "2ND" by changing its color to a shade of red?

Answer №1

Referencing the code snippet from this source -->

    color : function (color, d) {
        return d.index && d.index === 2 ? "#dd0" : color;
    },

https://jsfiddle.net/kmjpg30c/3/

d.index provides the index of the stack - note that there is no index 4 in the example since indexing starts at 0, ranging from [0,1,2,3]

To check against the index and apply a new color if it matches or return the originally passed color.

The d.index test is included as other routines may pass in just a string indicating the dataset ("A", "B", "C" in the example), therefore checking for the necessary fields in d first.

If highlighting a specific part of the stack, also consider using the .id field e.g.

d.index === 2 && d.id === "A"

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

Add an item to an array and then transform the array into a JSON format

I have a situation where I am creating an object and pushing it into an array. After that, I convert it into JSON format. When I display the dataCharts using an alert, it is returned in this form: [{"AllLinks":"Link9","LinkURL":"url1"},{"AllLinks":"Link6" ...

The deep reactivity feature in Vue3 is functioning well, however, an error is being

I am currently using the composition API to fetch data from Firestore. While the render view is working fine, I am encountering some errors in the console and facing issues with Vue Router functionality. This might be due to deep reactivity in Vue. Here is ...

When refreshing the page, the authentication token set in the Vuex store using axios in Nuxt.js/Vue.js gets reset

Here is the code snippet I am using to manage login, logout, user retrieval, and token setting for all axios requests as an auth header. While this code works perfectly during client-side rendering - such as logging in, storing the token in cookies, etc. ...

Unordered calling of functions in JavaScript - is it possible?

I'm currently working on a project that involves extracting data from an SQL database and converting the output of a query (which is a number) into a corresponding color, which is then passed to a JavaScript variable. Essentially, I am using ajax to ...

Transitioning from the Play mode to the Pause mode upon pressing the Set button

Is there a way to make the button switch from play to pause once the 'Set' button is clicked? Clicking on 'Set' should change the SVG to display the pause button icon. Snippet of Code: https://jsfiddle.net/192h0w85/195/ (function ...

Using jQuery to dynamically add a value to a comment string

Is there a way to dynamically include tomorrow's start and end times in the message for the setupOrderingNotAvailable function if today's end time has passed? The current message states that online ordering will be available again tomorrow from 1 ...

Ensure that the initial section of the page spans the full height of the browser, while all subsequent sections have a

I have a website composed of various blocks with different heights, all extending to full width. My goal is to make the first block the full height and width of the browser window, while keeping the other blocks at a set height as seen on this site: After ...

Exploring the world of variable scopes in Node.js

I'm new to exploring the world of node.js, particularly trying to grasp the concept of asynchronous processing loops. Let's say I have an array called var counter = []; declared at the beginning of my server.js script. Next, I have a POST handl ...

What is the best way to search for documents that have all conditions met within a sub-array entry?

My rolesandresponsibilities collection document is displayed below: [{ "_id": { "$oid": "58b6c380734d1d48176c9e69" }, "role": "Admin", "resource": [ { "id": "blog", "permissions": [ " ...

Obtaining a byte array using the file input method

var profileImage = fileInputInByteArray; $.ajax({ url: 'abc.com/', type: 'POST', dataType: 'json', data: { // Other data ProfileImage: profileimage // Other data }, success: { } }) // Code in Web ...

Automatically insert nested object keys and values from jQuery into their respective div elements

Below is a sample object that I am working with: "experience": { "1": { "jobtitle": "job_title", "companyname": "company_name", "companytown": "company_town", "companycountry": "company_country", "summary": "Sum ...

Ensure that the height of the flex body is set to 100% within the ReactNative View

Within my React Native app, I have constructed the following view: <ScrollView style={{ width: 250, height: '100%', backgroundColor: '#000000' }}> <SafeAreaView style={{ flex: 1, flexW ...

Renaming properties in an AngularJS model

After receiving the data in a structured format, my task is to present it on a graph using radio buttons. Each radio button should display the corresponding category name, but I actually need each button to show a custom label instead of the original categ ...

Is it possible to employ a method to eliminate a specific valuable element 'this' from an array?

I am working on a task management app that allows users to create a To-Do list and remove specific items from the list. Currently, I am facing an issue with using 'localStorage' to save the list when the page is refreshed. The problem arises when ...

If the width of the table is set to 100% in the CSS, the legend in the Flot chart will automatically shift to the

When the CSS for the table is set to { width:100%}, the Flot chart's legend moves to the left side. Is there any way to maintain the table { width:100%} while also preventing this shift, considering that the CSS is applied site-wide? Here is a jsfid ...

What is the appropriate response to send to the user in a web application?

I am currently developing a web application that utilizes AngularJS for the frontend, a REST API, and MongoDB as the backend, all powered by Node.js. Background to the challenge: One key requirement is to authenticate users using token-based authenticati ...

Tips on how to update a table following each button click during an ajax request

I am encountering an issue while trying to display data in a table by clicking on a search button. The problem arises when there is no data between the specified "Fromdate - Todate" range; the error message appears correctly. However, even after entering t ...

Having trouble establishing a new local Windows directory structure with Selenium

After following the guidelines provided here and here, I am striving to ensure that the directory where my results reports are stored is consistently available for each user. new File(sampleFolder).mkdir(); The sampleFolder path displayed in the Eclipse ...

Is it feasible to merge Apollo queries within the context of Nuxt?

Incorporating nuxt and apollo together using the https://github.com/nuxt-community/apollo-module module has been a successful venture. A GraphQL query was crafted and tested in GraphiQL to obtain information about a specific page along with general SEO de ...

Accessing the req object in Node.js using Express

Currently, I am attempting to redefine the function send in order to include an express-session value with each response. Unfortunately, I seem to be encountering issues accessing the variable req within the definition of send. 01| let app = express(); 02| ...