I am trying to create a bubble chart.
My data is like:
And I am trying to visualize medicines which is an array within the array of "meds" on the base of prescription. I think the problem is that I am not able to assign data properly in the code which is medicines and prescription in this case due to which is doesn't show anything at all. My code is like:
My data is like:
Code:
meds =
[
{"cause": "abc", "medicines": ["def"], "prescription": "234"},
{"cause": "dec", "medicines": ["def"], "prescription": "234"}
]
Code:
renderChart() {
this.get_meds().subscribe((medicines) => {
this.meds = medicines;
let height = 400;
let width = 600;
let color = d3.scaleOrdinal(d3.schemeCategory10);
let bubble = d3.pack()
.size([width, height])
.padding(1.5);
let svg = d3.select('#chart')
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "bubble");
let nodes = d3.hierarchy(this.meds)
.sum(function (d: any) {
return d.prescription;
});
let node = svg.selectAll(".node")
.data(bubble(nodes).descendants())
.enter()
.filter(function (d) {
return !d.children
})
.append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
}).style("fill", function (d, i: any) {
return color(i);
});
node.append("title")
.text(function (d: any) {
return d.medicines + ": " + d.prescription;
});
node.append("circle")
.attr("x", function (d) { return d.x; })
.attr("y", function (d) { return d.y })
.attr("r", function (d) {
return d.r;
})
.style("fill", function (d, i: any) {
return color(i);
});
node.append("text")
.attr("dy", ".2em")
.style("text-anchor", "middle")
.text(function (d: any) {
return d.data.medicines.substring(0, d.r / 3);
})
.attr("font-family", "sans-serif")
.attr("font-size", function (d) {
return d.r / 5;
})
.attr("fill", "white");
node.append('text')
.attr("dy", "1.3em")
.style("text-anchor", "middle")
.text(function (d: any) {
return d.data.prescription;
})
.attr("font-family", "Gill Sans")
.attr("font-size", function (d) {
return d.r / 5;
})
.attr("fill", "white");
}
)
}
Comment