4

Currently there is no example on the internet for a bubble chart having a legend, using dc.js, with the dc.legend() function.

that.sessions_scatterplot
        .width(830)
        .height(350)
        .transitionDuration(1000)
        .margins({top: 100, right: 10, bottom: 30, left: 25})
        .dimension(that.dim_Source)
        .group(that.fact_Source_Sessions)
        .ordinalColors(["#70c5a4","#8cd4f2","#fdc976","#9d97c8"])
        .colorAccessor(function(d){
            return d.value.src_category;
        })
        .keyAccessor(function (d) {
            var x = d.value.avg_time/d.value.count;
            return x;
        })
        .valueAccessor(function (d) {
            var y = d.value.hundred_bounce_rate/d.value.count;
            return y;
        })
        .radiusValueAccessor(function (d) {
            var r = Math.round(d.value.sessions/d.value.count);
            return r;
        })
        .title(function(p){
            return [p.key,
               "Average Time On Page: " + (p.value.avg_time/p.value.count).toFixed(2),
               "100 - Bounce Rate: " + (p.value.hundred_bounce_rate/p.value.count).toFixed(2),
               "Sessions / Page Views: " + (p.value.sessions/p.value.count).toFixed(2)]
               .join("\n");
        })
        .renderTitle(true)
        .maxBubbleRelativeSize(0.03)
        .x(d3.scale.linear().domain(that.avg_time_extent).range([0,that.width]))
        .y(d3.scale.linear().domain(that.hundred_bounce_rate_extent).range([0,that.height]))
        .r(d3.scale.linear().domain(that.sessions_pg_views_extent))
        .minRadiusWithLabel(30)
        .yAxisPadding(0.5)
        .xAxisPadding(500)
        .xAxisLabel('Average Time On Page')
        .yAxisLabel('100 - Bounce Rate')
        .legend(dc.legend().x(200).y(30).gap(5).horizontal(true).itemWidth(100).legendWidth(200));

There MUST be someway to use legends in the Bubble Chart.
Assumptions :
1) Have used top-margins.
2) Used a colorAccessor() function to color the bubbles using a group.
3) A 'g' element is getting appended to the 'svg', with the 'dc-legend' class being assigned to the 'g' element. But there are no child elements to the legend.

Gordon
  • 19,811
  • 4
  • 36
  • 74
Isha
  • 61
  • 6
  • I believe this is not supported by dcjs directly. You will have to hack https://github.com/dc-js/dc.js/blob/master/src/bubble-chart.js and take some inspiration from https://github.com/dc-js/dc.js/blob/master/src/composite-chart.js#L363 or other examples which have legend support – Navyseal Dec 05 '14 at 06:13
  • See this answer - it's not too difficult to hack it in: https://stackoverflow.com/a/50532425/676195 – Gordon May 25 '18 at 15:22

1 Answers1

2

It was talked about on github here: https://github.com/dc-js/dc.js/issues/485.

However, to my best knowledge I believe you will have to hack it if you want a legend in this case.

Ben Leitner
  • 1,532
  • 1
  • 12
  • 33