I am working on creating a bubble chart with D3 and I can build the chart just fine with dummy data, but when I try to consume my live data the chart won't work. I understand why it doesn't work, it's because the data is coming in, in a bad format for D3. I just don't know how to reorganize the data with JavaScript.
My data is stored in an object like this:
datas = {fieldA:[Array[32]], fieldB[Array[32]]};
And the Array inside of the properties looks like this:
Array[Object,Object,Object...]
The data I need is a property in the array of objects called raw_data
So to access one of the data points it would look something like this:
datas.fieldA[0].raw_data
I need the data to be accessed like this:
datas[0].fieldA.raw_data
This will allow me to bind datas
in my D3 viz and have access to both fieldA and fieldB. Right now, I can bind datas.fieldA which is an issue because I need both fields to create the viz.
Below is the actual d3 code I am writing if that helps. I can create the circles, but now need to append text to the circles. The real field names are countword and word.
var h = options.dataset.chart_information.height;
var w = options.dataset.chart_information.width;
var margin = {left:5, right:5, top:5, bottom:5};
var dataWord = options.dataset.data.countword;
var dataText = options.dataset.data.word;
var simulation = d3.forceSimulation()
.force("x", d3.forceX(w/2).strength(0.05))
.force("y", d3.forceY(h/2).strength(0.05))
.force("collide", d3.forceCollide(function(d) { return rScale(d.raw_data) + 0.2; }));
var rScale = d3.scaleSqrt()
.domain([d3.min(dataWord, function(d) { return d.raw_data; }),
d3.max(dataWord, function(d) { return d.raw_data; })])
.range([10,60]);
var svg = d3.select(options.divSelector)
.append("svg")
.attr("height",h)
.attr("width",w);
var circles = svg.selectAll(".circle")
.data(dataWord)
.enter()
.append("circle")
.attr("r", function(d) { return rScale(d.raw_data); })
.attr("fill","steelblue");
simulation.nodes(dataWord)
.on("tick", ticked);
function ticked() {
circles
.attr("cx", function(d) { return d.x })
.attr("cy", function(d) { return d.y });
};