How to get the colour of stacked bar chart segment

Share RGraph:   To help my Google visibility (it can't get much worse!), if you like and use RGraph I'd appreciate it if you could link to me

« Back to message list

Enter your email address to get email updates on this topic. You can stop receiving updates by clicking the link in the update email messages.

Posted by Ananth on 20th December 2016
Hi,

i have click function where it will alert a message with bar value, can we get the color of the bar or can we assign some ID to that bar (id for each color in stacked bar)

.grow().on('click', function (e, shape) {

                 var obj = e.target.__object__

                 var index = shape['index_adjusted'];
                 var dataset = shape['dataset'];
                 var value = obj.original_data[dataset][index];
                 $a(value);
Posted by Richard on 20th December 2016
Hi there,

You're close - simply use the index_adjusted with the colors array that you can get using the .get() function:

www.rgraph.net/fiddle/view.html/getting-colour-of-stacked-segment-from-a-click




Richard
Posted by Ananth on 20th December 2016
Hi Richard,

thank you for your prompt response, this is very good support! i have ever seen.

can we get x value as well in this?
Posted by Richard on 20th December 2016
Hi there,

There's no X scale on a Bar chart - and therefore no X value. But you can get the relevant label by using the dataset property:

www.rgraph.net/fiddle/view.html/getting-colour-of-stacked-segment-from-a-click


Richard
Posted by Ananth on 21st December 2016
Hi

in this JSON field one will be on X axis as label how can we read this in dataset, also do we have any debug tool to get the list of dataset arrays?

[{
    "FIELD1": "Richard",
    "FIELD2": 8,
    "FIELD3": 4,
    "FIELD4": 7,
    "FIELD5": 6,
    "FIELD6": 5,
    "FIELD7": 3,
    "FIELD8": 4
   }, {
    "FIELD1": "Dave",
    "FIELD2": 7,
    "FIELD3": 4,
    "FIELD4": 6,
    "FIELD5": 9,
    "FIELD6": 5,
    "FIELD7": 8,
    "FIELD8": 7
   }, {
    "FIELD1": "Luis",
    "FIELD2": 4,
    "FIELD3": 3,
    "FIELD4": 5,
    "FIELD5": 2,
    "FIELD6": 6,
    "FIELD7": 5,
    "FIELD8": 4
   }, {
    "FIELD1": "Kevin",
    "FIELD2": 4,
    "FIELD3": 2,
    "FIELD4": 8,
    "FIELD5": 9,
    "FIELD6": 6,
    "FIELD7": 7,
    "FIELD8": 3
   }, {
    "FIELD1": "Joel",
    "FIELD2": 4,
    "FIELD3": 5,
    "FIELD4": 1,
    "FIELD5": 3,
    "FIELD6": 5,
    "FIELD7": 8,
    "FIELD8": 6
   }, {
    "FIELD1": "Pete",
    "FIELD2": 4,
    "FIELD3": 5,
    "FIELD4": 6,
    "FIELD5": 3,
    "FIELD6": 5,
    "FIELD7": 8,
    "FIELD8": 6
   }
]
Posted by Richard on 21st December 2016
Hi there,

Looking at that you might be able to do this (assume that the above structure is held in the "data" variable.

var label = data[dataset].FIELD1;




Richard
Posted by Ananth on 21st December 2016
Hi Richard,

thank you for the details, i was using

var labels = bar.get('labels');
$a(labels[dataset]);

not sure if this will make any difference in performance etc.
Posted by Richard on 21st December 2016
Hi there,

Probably not.




Richard
Posted by Enter your name... on 21st December 2016
Thank you! Appreciate your help!

Add a reply




« Back to message list