8

I have a scatter plot charge where i want to change the size of the dots to make them look like bubble. Can someone show me how to change size of the bubble?

Here is my code:

var chart = c3.generate({
    data: {
        xs: {
            IBM: 'ibm_x',
            Microsoft: 'microsoft_x',
        },

        columns: [
            ["ibm_x", 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.4, 3.0, 3.0, 4.0, 4.4, 3.9, 3.5, 3.8, 3.8, 3.4, 3.7, 3.6, 3.3, 3.4, 3.0, 3.4, 3.5, 3.4, 3.2, 3.1, 3.4, 4.1, 4.2, 3.1, 3.2, 3.5, 3.6, 3.0, 3.4, 3.5, 2.3, 3.2, 3.5, 3.8, 3.0, 3.8, 3.2, 3.7, 3.3],
            ["microsoft_x", 3.2, 3.2, 3.1, 2.3, 2.8, 2.8, 3.3, 2.4, 2.9, 2.7, 2.0, 3.0, 2.2, 2.9, 2.9, 3.1, 3.0, 2.7, 2.2, 2.5, 3.2, 2.8, 2.5, 2.8, 2.9, 3.0, 2.8, 3.0, 2.9, 2.6, 2.4, 2.4, 2.7, 2.7, 3.0, 3.4, 3.1, 2.3, 3.0, 2.5, 2.6, 3.0, 2.6, 2.3, 2.7, 3.0, 2.9, 2.9, 2.5, 2.8],
            ["IBM", 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.4, 0.2, 0.5, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.3, 0.2, 0.6, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2],
            ["Microsoft", 1.4, 1.5, 1.5, 1.3, 1.5, 1.3, 1.6, 1.0, 1.3, 1.4, 1.0, 1.5, 1.0, 1.4, 1.3, 1.4, 1.5, 1.0, 1.5, 1.1, 1.8, 1.3, 1.5, 1.2, 1.3, 1.4, 1.4, 1.7, 1.5, 1.0, 1.1, 1.0, 1.2, 1.6, 1.5, 1.6, 1.5, 1.3, 1.3, 1.3, 1.2, 1.4, 1.2, 1.0, 1.3, 1.2, 1.3, 1.3, 1.1, 1.3],
        ],
        type: 'scatter'
    },
             color: {
        pattern: ['#4C7A9F', '#E25459']
    },

});

setTimeout(function () {
    chart.load({
        columns: [
            ["IBM", 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.4, 0.2, 0.5, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.3, 0.2, 0.6, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2],
        ]
    });
}, 1000);

setTimeout(function () {
    chart.load({
        columns: [
            ["IBM", 10.2, 10.2, 10.2, 10.2, 10.2, 11.4, 10.3, 10.2, 10.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.4, 0.2, 0.5, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.3, 0.2, 0.6, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2],
        ]
    });
}, 2000);

            setTimeout(function () {
    chart.load({
        columns: [
            ["Microsoft", 10.2, 10.2, 10.2, 10.2, 10.2, 11.4, 10.3, 10.2, 10.2, 10.1, 10.2, 10.2, 10.1, 0.1, 0.2, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.4, 0.2, 0.5, 0.2, 0.2, 0.4, 0.2, 0.2, 0.2, 0.2, 0.4, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.3, 0.2, 0.6, 0.4, 0.3, 0.2, 0.2, 0.2, 0.2],
        ]
    });
}, 3000);

This is my FIDDLE. I am basically trying to get my graph to look like this

user1010101
  • 2,062
  • 7
  • 47
  • 76
  • A bubble chart needs three values for each element: x, y, and value. The third one is the one used to set the relative size (radius, area...). I don't see that type of structure in your data. – Cool Blue Jun 08 '15 at 21:50

2 Answers2

19

With c3, if you just want to increase the point size uniformly, just add a property for points and specify the radius

point: {
    r: 15
},

Fiddle - http://jsfiddle.net/3jfe75r8/


If you want to make the point size dependent on one of the dimensions you can replace the hardcoded 15 by a function. For instance

point: {
    r: function(d) { 
       return d.value * 5;
    }
},

Notice that I multiplied by a scaling factor of 5, so that the points would be large enough. If your data values are large enough, you actually don't need this scaling factor.


If you want to make the point size dependent on both the the dimensions you would do this

point: {
    r: function(d) { 
       return (d.x + d.value) * 5;
    }
},

Or whatever expression that strikes your fancy.


If you want to scale the size by one of the dimension, you can define your own scaling function and use that. For instance

var rs = d3.scale.linear()
          .domain([0.01, 5])
          .range([5, 50]);

and then

point: {
    r: function(d) {
        return rs(d.value)
    }
},

Which would scale the radius linearly from 5 to 50 for values from 0.01 to 5.


You could also replace 0.01 and 5 by the actual min and max values from your dataset (use d3.min(< < array > >) and d3.max(< < array > >)). If you data is multidimensional, you'll have to flatten it into an array.

If your data changes dynamically either redefine your scaling function or have it depend on the data (vs. values calculated from the data)

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • upvoted, This is great but now if i want only some of them to be big some smaller? What alternative can you suggest? – user1010101 Jun 08 '15 at 15:08
  • you can replace 15 by a function too - for example - r: function(d) { return Math.random() * 30; }, will give you bubbles of varying radius. Your data doesn't have a third dimension btw, so any reason you actually want to do this? – potatopeelings Jun 08 '15 at 15:31
  • Here is another sample that modifies the radius based on the dataset - https://github.com/masayuki0812/c3/blob/master/htdocs/samples/point_r.html – potatopeelings Jun 08 '15 at 15:36
  • d.x is the x value of the point and d.value the y value, d.index is the index (you can use it to refer to an z dimension). For e.g. the function(d) { return d.x * d.value * 5; } makes the radius a function of both the x and y value. – potatopeelings Jun 08 '15 at 15:43
  • Well what i wanted to do was like for example say "10" is the biggest value in my data set i would want it to have the biggest bubble size and the rest of the bubble size should be in respect to that. I am trying to do that. so would i pass in function to do that? – user1010101 Jun 08 '15 at 15:51
  • So you'd need to determine the smallest (0?) & largest (10?) value. Then use the function to calculate what the size for each point would be. Something like: `return (d.value - smallest) / (largest - smallest) * x;` ... where x determines how big it can get. – Sean Jun 08 '15 at 18:00
  • 1
    @user1010101 - you have 2 dimensions. You will have 2 biggest values per dataset - one for each axis. For the logic, the logic would be similar to what Sean said. Cheers! – potatopeelings Jun 08 '15 at 21:55
  • @potatopeelings Hey i just posted a new question for c3.js and I think you may be able to help me http://stackoverflow.com/questions/31035173/customize-a-donut-chart-in-c3-js – user1010101 Jun 24 '15 at 19:28
  • @potatopeelings I have this question and you maybe able to help me with it. http://stackoverflow.com/questions/37031238/using-d3-js-to-modify-c3-js-chart – user1010101 May 04 '16 at 14:56
  • @user1010101 - I see you got an answer there. Cheers! – potatopeelings May 04 '16 at 22:36
  • @potatopeelings yes i did, however there is one issue with the answer the arcs do expand but once i hover over the chart they shrink again. is there way to fix this? – user1010101 May 05 '16 at 14:16
  • @potatopeelings i have not accepted that answer yet as there is some flaw i would highly appreciate if you can post a fix to that so i can vote you also – user1010101 May 05 '16 at 14:43
  • @user1010101 - just override `chart.internal.unexpandArc` to not do anything for ids A and B like https://jsfiddle.net/uamd1404/ – potatopeelings May 05 '16 at 15:48
  • @potatopeelings https://jsfiddle.net/svo2hp2h/ I tried your solution now the arcs are not expanding at all – user1010101 May 05 '16 at 15:50
  • @potatopeelings I just asked this question seprate I think you deserve credit for your work so you can post here http://stackoverflow.com/questions/37054899/chart-arcs-shrink-after-refresh – user1010101 May 05 '16 at 15:52
2

d3 size would work for you. For example:

var circle = svg.selectAll("circle")
    .data([32, 57, 293], String);

circle.enter().append("svg:circle")
    .attr("cy", 90)
    .attr("cx", String)
    .attr("r", Math.sqrt);

This can be also a good reference: Link or visit this Gist. The file index_previous.html is an example similar to what you are looking for, simply download the example, the data and remove the "//" from this line:

//bubbles.addMeasureAxis("z", "Delay_per_Flight (minutes)");
Rafa
  • 2,879
  • 2
  • 21
  • 27
  • I will take a look at your link is it possible to maybe edit my Fiddle and show me thanks so much for great reference once again. – user1010101 Jun 08 '15 at 14:39