Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add a barLabel for composite stack bar charts? #1172

Closed
cheanney opened this issue Jun 27, 2016 · 4 comments
Closed

how to add a barLabel for composite stack bar charts? #1172

cheanney opened this issue Jun 27, 2016 · 4 comments

Comments

@cheanney
Copy link

Hi,

I have a composite stack bar chart. I want to show the count for each bar when a legend is mouseover. Now when I mouse over a legend, the bars are highlighted. I also want to add the barLabel for the highlighted layer.
Here is how the chart looks like:
screen shot 2016-06-27 at 10 42 59 am

Here is my code:
`

  1. var composite = dc.compositeChart('#timepoint-chart');
    
  2. var scat = dc.barChart(composite)
    
  3.      .gap (65)
    
  4.      .group(scatLevelByTimePoint, 'full scat').valueAccessor(function (d) {
    
  5.          return d.value.s_full;
    
  6.      })
    
  7.      .stack(scatLevelByTimePoint, 'partial scat', function (d) {
    
  8.          return d.value.s_partial;
    
  9.      })
    
  10.         .stack(scatLevelByTimePoint, 'empty scat', function (d) {
    
  11.             return d.value.s_empty;
    
  12.         })
    
  13.         .title(function(d) {
    
  14.         if (this.layer !== 'N' && this.layer !== 'Y'){
    
  15.             var type = this.layer.split(" ")[0];
    
  16.             var name = this.layer.split(" ")[1];
    
  17.             var c;
    
  18.             if (name == 'scat'){
    
  19.                 c = 's_';
    
  20.                 return ['full '+name+': ' + d.value['s_full'],'partial '+name+': ' + d.value['s_partial'], 'empty '+name+': ' + d.value['s_empty']].join('\n');
    
  21.             }
    
  22.             else if (name == 'citrate'){
    
  23.                 c  = 'c_';
    
  24.                 return ['full '+name+': ' + d.value['c_full'],'partial '+name+': '  + d.value['c_partial'],'empty '+name+': ' + d.value['c_empty']].join('\n');
    
  25.             }
    
  26.         }
    
  27.         else{
    
  28.             if (this.layer == 'Y' || this.layer == 'N')
    
  29.                 return ['Y: ' + d.value['y_pax'],'N: ' + d.value['n_pax']].join('\n');
    
  30.             else
    
  31.                 return this.layer + ": "+d.value;
    
  32.         }
    
  33.     })
    
  34.         .colors(d3.scale.ordinal().range(["#495090", "#4d5eef", "#8b95ec"]));
    
  35.         //.colors(d3.scale.ordinal().range(colorbrewer.RdPu[9]))
    
  36. var citrate = dc.barChart(composite)
    
  37.         .gap (65)
    
  38.         .group(citrateLevelByTimePoint, 'full citrate').valueAccessor(function (d) {
    
  39.             return d.value.c_full;
    
  40.         })
    
  41.         .stack(citrateLevelByTimePoint, 'partial citrate', function (d) {
    
  42.             return d.value.c_partial;
    
  43.         })
    
  44.         .stack(citrateLevelByTimePoint, 'empty citrate', function (d) {
    
  45.             return d.value.c_empty;
    
  46.         })
    
  47.         .title(function(d) {
    
  48.         if (this.layer !== 'N' && this.layer !== 'Y'){
    
  49.             var type = this.layer.split(" ")[0];
    
  50.             var name = this.layer.split(" ")[1];
    
  51.             var c;
    
  52.             if (name == 'scat'){
    
  53.                 c = 's_';
    
  54.                 return ['full '+name+': ' + d.value['s_full'],'partial '+name+': ' + d.value['s_partial'], 'empty '+name+': ' + d.value['s_empty']].join('\n');
    
  55.             }
    
  56.             else if (name == 'citrate'){
    
  57.                 c  = 'c_';
    
  58.                 return ['full '+name+': ' + d.value['c_full'],'partial '+name+': '  + d.value['c_partial'],'empty '+name+': ' + d.value['c_empty']].join('\n');
    
  59.             }
    
  60.         }
    
  61.         else{
    
  62.             if (this.layer == 'Y' || this.layer == 'N')
    
  63.                 return ['Y: ' + d.value['y_pax'],'N: ' + d.value['n_pax']].join('\n');
    
  64.             else
    
  65.                 return this.layer + ": "+d.value;
    
  66.         }
    
  67.     })
    
  68.         .renderLabel(true)
    
  69.         .label(function(d){
    
  70.             return d.value;
    
  71.         })
    
  72.         .colors(d3.scale.ordinal().range(colorbrewer.RdYlBu[9]));
    
  73.         //.colors(d3.scale.ordinal().range(["#2c6852", " #51c097", "#6cfac6"]))
    
  74.         //.colors(d3.scale.category20())
    
  75.         ;
    
  76. var pax = dc.barChart(composite)
    
  77.     .gap (65)
    
  78.     .colors(d3.scale.ordinal().range(colorbrewer.BrBG[6]))
    
  79.     .group(paxByTimePoint, 'N').valueAccessor(function (d) {
    
  80.         return d.value.n_pax;
    
  81.     })
    
  82.     .title(function(d) {
    
  83.         if (this.layer !== 'N' && this.layer !== 'Y'){
    
  84.             var type = this.layer.split(" ")[0];
    
  85.             var name = this.layer.split(" ")[1];
    
  86.             var c;
    
  87.             if (name == 'scat'){
    
  88.                 c = 's_';
    
  89.                 return ['full '+name+': ' + d.value['s_full'],'partial '+name+': ' + d.value['s_partial'], 'empty '+name+': ' + d.value['s_empty']].join('\n');
    
  90.             }
    
  91.             else if (name == 'citrate'){
    
  92.                 c  = 'c_';
    
  93.                 return ['full '+name+': ' + d.value['c_full'],'partial '+name+': '  + d.value['c_partial'],'empty '+name+': ' + d.value['c_empty']].join('\n');
    
  94.             }
    
  95.         }
    
  96.         else{
    
  97.             if (this.layer == 'Y' || this.layer == 'N')
    
  98.                 return ['Y: ' + d.value['y_pax'],'N: ' + d.value['n_pax']].join('\n');
    
  99.             else
    
  100.                return this.layer + ": "+d.value;
    
  101.        }
    
  102.    })
    
  103.    .stack(paxByTimePoint, 'Y', function (d) {
    
  104.        return d.value.y_pax;
    
  105.    });
    
  106. var patient = dc.barChart(composite)
    
  107.    .gap(65)
    
  108.    .renderLabel(true)
    
  109.    .group(timePointGroup, 'patient',function(d){
    
  110.        return d.value;
    
  111.    });
    
  112. composite
    
  113.    .width(1000) 
    
  114.    .height(500)
    
  115.    .renderLabel(true)
    
  116.    .label(function(d){
    
  117.        console.log('d = '+d);
    
  118.        var keys = Object.keys(d.value);
    
  119.        var total = 0;
    
  120.        for (i=0;i<keys.length;i++){
    
  121.            total+=d.value[keys[i]];
    
  122.        }
    
  123.        return total;
    
  124.    })
    
  125.    .margins({top: 50, right: 20, bottom: 50, left: 40})
    
  126.    .dimension(timePointDimension)
    
  127.     .legend(dc.legend().x(800).y(0).itemHeight(13).gap(5))
    
  128.     .xAxisLabel('Time Point')
    
  129.     .yAxisLabel('Count')
    
  130.     .ordering(function(d) { return +d.key; })
    
  131.     .x(d3.scale.ordinal())
    
  132.     .y(d3.scale.linear().domain([0, 300]))
    
  133.     .shareTitle(false)
    
  134.     .xUnits(dc.units.ordinal)
    
  135.     .group(scatLevelByTimePoint)
    
  136.     .compose([
    
  137.             scat,
    
  138.             citrate,
    
  139.             pax,
    
  140.             //patient
    
  141.    ])
    
  142.    .on('renderlet',function (chart) {
    
  143.        chart.selectAll("g.sub._0  .stack._0  rect.bar").attr("transform", "translate(" + -8 + ", 0)");
    
  144.        chart.selectAll("g.sub._0  .stack._1  rect.bar").attr("transform", "translate(" + -8 + ", 0)");
    
  145.        chart.selectAll("g.sub._0  .stack._2  rect.bar").attr("transform", "translate(" + -8 + ", 0)");
    
  146.        //citrate
    
  147.        chart.selectAll("g.sub._1  .stack._0  rect.bar").attr("transform", "translate(" + 6 + ", 0)");
    
  148.        chart.selectAll("g.sub._1  .stack._1  rect.bar").attr("transform", "translate(" + 6 + ", 0)");
    
  149.        chart.selectAll("g.sub._1  .stack._2  rect.bar").attr("transform", "translate(" + 6 + ", 0)");
    
  150.        chart.selectAll("g.sub._2  .stack._0  rect.bar").attr("transform", "translate(" + 20 + ", 0)");
    
  151.        chart.selectAll("g.sub._2  .stack._1  rect.bar").attr("transform", "translate(" + 20 + ", 0)");
    
  152.        chart.selectAll("g.sub._3  .stack._0  rect.bar").attr("visibility", "hidden");
    
  153.        chart.selectAll("g.sub._3  .stack._0  .barLabel").attr("visibility", "hidden");
    
  154.        var legends = chart.selectAll("g.dc-legend .dc-legend-item");
    
  155.        var labels = chart.selectAll("g .barLabel");
    
  156.        console.log('size = '+labels[0].length);
    
  157.        console.log('labels = '+JSON.stringify(labels,null,2));
    
  158.        legends
    
  159.        .on("mouseover.foo", function(d){
    
  160.            var layername = d.name;
    
  161.            var highligtedBars = chart.selectAll("rect.bar.highlight");
    
  162.            var length = highligtedBars.length;
    
  163.            console.log('size = '+length);
    
  164.            highligtedBars[0].forEach(function (s, i) {
    
  165.                console.log('data = '+JSON.stringify(s.**data**, null, 2));
    
  166.                var layer = s.**data**.layer;
    
  167.                if (layer == layername){
    
  168.                    labels[0].text( function (s) { return "( " + s.x + ", " + s.y +" )"; });
    
  169.                }
    
  170.            });
    
  171.            //chart.selectAll("g.sub._3  .stack._0  .barLabel").attr("visibility", "show");
    
  172.            //}else{
    
  173.            //  highligtedBars.text( function (d) { return "( " + d.x + ", " + d.y +" )"; });
    
  174.            //}
    
  175.        });
    
  176.        legends
    
  177.            .on("mouseout.foo", function(d){
    
  178.                console.log('d = '+JSON.stringify(d));
    
  179.                //chart.selectAll("rect.bar bartext").text("");
    
  180.                labels.text( function (d) { return ""; });
    
  181.            });
    
  182.    });
    

`

@gordonwoodhull
Copy link
Contributor

This doesn't strike me as a feature request, more a one-off feature you can add with a renderlet, or maybe a tooltip.

Sorry, but the issue tracker is for reporting bugs and making enhancement requests, not for general support questions. Please ask on Stack Overflow with the dc.js tag, or on the user group, and we will be glad to help you there.

I am closing this.

(P.S. It pains me to see people going through these contortions for a grouped bar chart - could you please try PR #1043?)

@cheanney
Copy link
Author

Thank you!!

Sorry for posting at the wrong place.

Thanks,

Anney

On Wed, Jun 29, 2016 at 6:19 PM, Gordon Woodhull [email protected]
wrote:

This doesn't strike me as a feature request, more a one-off feature you
can add with a renderlet, or maybe a tooltip
http://stackoverflow.com/questions/24664079/d3-tooltips-for-multiple-linked-dc-js-charts/24702554#24702554
.

Sorry, but the issue tracker is for reporting bugs and making enhancement
requests, not for general support questions. Please ask on Stack Overflow
http://stackoverflow.com with the dc.js tag, or on the user group
https://groups.google.com/forum/?fromgroups=#!forum/dc-js-user-group,
and we will be glad to help you there.

I am closing this.

(P.S. It pains me to see people going through these contortions for a
grouped bar chart - could you please try PR #1043
#1043?)


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#1172 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AIYz3VFE1tPYQ9NyTwf_h6b_TVpmQv32ks5qQu9tgaJpZM4I_Ko6
.

@wni5378
Copy link

wni5378 commented May 7, 2021

This doesn't strike me as a feature request, more a one-off feature you can add with a renderlet, or maybe a tooltip.

Sorry, but the issue tracker is for reporting bugs and making enhancement requests, not for general support questions. Please ask on Stack Overflow with the dc.js tag, or on the user group, and we will be glad to help you there.

I am closing this.

(P.S. It pains me to see people going through these contortions for a grouped bar chart - could you please try PR #1043?)

Well there are some things that aren't obvious. It's not easy to navigate this library without having to endure 'contortions'.

@gordonwoodhull
Copy link
Contributor

gordonwoodhull commented May 7, 2021

It's not easy to navigate this library without having to endure 'contortions'.

Yep that’s true! This library evolved and was not originally designed to do all it does.

If you’re looking for a self-consistent, well-thought-out charting library with many of the same features, I recommend checking out Vega Lite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants