0

I am trying to create a bubble chart using DC.js

The sample data I have is:

State_Name  Area    Cities    Villages   Population
A           200      60          1050      10000
B           300      80          1100      15678
C           500      20          2220      97767
D           600      90          3400      50000

So, I want to show total three Bubbles on a bubbleChart. The first bubble should represent total Area and rest two should represent total Cities and total Villages respectively. I have made a barChart for States_Names vs Population where xAxis represent States_name and yAxis represent Population. Now I want whenever I click on one or more bars or you can say States_name I should get the sum of Area, Cities and Villages for all those selected States_name. These sum values for Area, Cities and Villages will represent three bubbles on bubbleChart respectively.

For example: if I click on two bars from barChart which represent State_Name = A and B respectively, I should get three bubbles on bubbleChart. On hovering these three bubbles I should get values total Area = 200+300= 500, Total Cities = 60+80 = 140, total Villages = 1050+1100 = 2150 respectively.

For Positioning of bubbles: I can fix their yAxis position. And, their xAxis position and radius should depend on the value that bubble has. For above example radius of bubble representing total Villages should be larger than the radius of other two bubbles. Similarly, for xAxis position bubble representing total Cities will be on left side of other two bubbles.

I tried code from here: http://jsfiddle.net/gordonwoodhull/37uET/6/ but getting the error: "group.top is not a function". It seems that code works only for rowChart.

var data = [
        {type: 'foo', a: 10, b: 9, c: 11, d: 2},
        {type: 'bar', a: 1, b: 4, c: 2, d: 3},
        {type: 'foo', a: 0, b: 2, c: 1, d: 22},
        {type: 'bar', a: 11, b: 5, c: 6, d: 5}];
    var ndx1 = crossfilter(data);

    function regroup(dim, cols) {
        var _groupAll = dim.groupAll().reduce(
            function(p, v) { // add
                cols.forEach(function(c) {
                    p[c] += v[c];
                });
                return p;
            },
            function(p, v) { // remove
                cols.forEach(function(c) {
                    p[c] -= v[c];
                });
                return p;
            },
            function() { // init
                var p = {};
                cols.forEach(function(c) {
                    p[c] = 0;
                });
                return p;
            });
        return {
            all: function() {
                // or _.pairs, anything to turn the object into an array
                return d3.map(_groupAll.value()).entries();
            }
        };
    }

    var dim = ndx1.dimension(function(r) { return r.a; });
    var sidewaysGroup = regroup(dim, ['a', 'b', 'c', 'd']);

    var typeDim = ndx.dimension(function(r) { return r.type; });

    var sidewaysRow = dc.bubbleChart('#state-donations')
            .width(350).height(300)
            .dimension(dim)
            .group(sidewaysGroup)
            .elasticX(true)
            .maxBubbleRelativeSize(0.3)
                        .x(d3.scale.linear().domain([0, 10]))
                        .y(d3.scale.linear().domain([0, 10]))
                        .r(d3.scale.linear().domain([0, 400]))
                        .elasticY(true)
                        .yAxisPadding(100)
                        .elasticX(true)
                        .xAxisPadding(500);

please help!

Divyansh
  • 1
  • 2
  • You should add more detail and maybe a picture of what you're trying to do, and some code you tried. It doesn't sound like the same kind of bubble chart that dc.js offers - I don't see X and Y coordinates here, maybe you are looking for a [bubble cloud](http://intellipharm.github.io/dc-addons/#bubble-cloud)? – Gordon Jul 11 '17 at 14:56
  • @Gordon I have attached the code I had tried. – Divyansh Jul 11 '17 at 16:19
  • You still need to spell out your question better - now I understand that you want to display a bubble for each column (which requires rotating the data), but most people couldn't guess that. Also I still don't understand how you plan to apply this to a bubble chart, which normally has X and Y coordinates for each bubble. Asking good questions is hard! – Gordon Jul 11 '17 at 16:34
  • Hi @Gordon , I have edited my question for better understanding. I hope now it is clear to you. – Divyansh Jul 11 '17 at 17:48
  • Okay, how are the bubbles arranged in the chart, what are their positions? I still think you may be thinking about a bubble cloud rather than a bubble chart. – Gordon Jul 11 '17 at 17:52
  • @Gordon well, I can fix their `yAxis` position. And, their `xAxis` position and `radius` should depend on the value that bubble has. **For above example radius of bubble representing total `Villages` should be larger than the radius of other two bubbles. Similarly, for `xAxis` position bubble representing total `Cities` will be on left side of other two bubbles.** – Divyansh Jul 11 '17 at 18:10
  • Okay, include this in the question and I'll vote to reopen. – Gordon Jul 11 '17 at 18:38
  • @gordan I have included that in the question as you mentioned. – Divyansh Jul 12 '17 at 06:53
  • Okay, I've voted to reopen, let's see if others follow. What if two columns have the same value, e.g. what if total cities ends up the same as total area? Then the bubbles will be on top of each other. – Gordon Jul 12 '17 at 12:12
  • Well, this question is still on hold, but I think [this answer](https://stackoverflow.com/questions/24737277/dc-js-how-to-create-a-row-chart-from-multiple-columns#24747518) covers most of what you're looking for. Note that clicking on the bubbles won't filter anything, because the rows of your data contribute to all bubbles. – Gordon Jul 14 '17 at 13:31

0 Answers0