how to take max value of bar in d3js(if dupblicates objects are there)

15 views
Skip to first unread message

Somepalli

unread,
Aug 30, 2017, 1:08:10 AM8/30/17
to d3-js
Hi  how to take max value of bar in d3js(if dupblicates objects are there). Text labels shows two lables. I need only one.
pls help


ex:      var data = [
              {l: 'a', v: 111},
             {l: 'b', v: 222},
             {l: 'b', v: 100},
             {l: 'c', v: 333},
              {l: 'c', v: 223}];
Capture1.PNG

steve rickus

unread,
Aug 30, 2017, 11:09:29 AM8/30/17
to d3-js
You can use the javascript reduce() and map() functions for things like this...

var data = [
    { l: 'a', v: 111 },
    { l: 'b', v: 222 },
    { l: 'b', v: 100 },
    { l: 'c', v: 333 },
    { l: 'c', v: 223 }
];

// build an object with max values for each key
var maxs = arr.reduce(function(acc, obj) {
   
if (obj.v > acc[obj.l]) {
        acc
[obj.l] = obj.v
   
};
   
return acc;
});

// rebuild the data array with only the max values
data = Object.keys(maxs).map(function(key) {
    return { l: key, v: maxs[key] };
});


--
Steve

Reply all
Reply to author
Forward
0 new messages