score:1
i would have done it by following steps.
create a key value map for your data array first.(you can dynamically generate it from the data array if key value names has a regular pattern)
var keymap = [{ category: 'producttype', value: 'value' }, { category: 'processtype', value: 'errorcount' }, { category: 'message', value: 'count' }];
convert the existing data in array of objects format to and array of arrays format.
var formatteddata = d3.nest() .key(function(d) { return object.keys(d)[0]; }) .entries(data) .map(function(d){ return d.values });
once a key map is created, you can iterate over the data array and create charts as shown in below snippet.
- calculate the chart positions as per the requirement inside the loop and place svg accordingly.
here is a sample code snippet.
var width = 300,
height = 155,
radius = math.min(width, height) / 3;
var keymap = [{
category: 'producttype1',
value: 'value1'
}, {
category: 'producttype',
value: 'value'
}, {
category: 'producttype2',
value: 'value2'
}];
var data =[
{"producttype1":"999999","value1":"99"},
{"producttype1":"88888","value1":"88"},
{"producttype1":"77777","value1":"77"},
{"producttype1":"999999","value1":"99"},
{"producttype":"132023","value":"144"},
{"producttype":"132030","value":"275"},
{"producttype":"132053","value":"42"},
{"producttype":"132093","value":"1"},
{"producttype":"132197","value":"94"},
{"producttype2":"132207","value2":"23"},
{"producttype2":"304055","value2":"51"},
{"producttype2":"520002","value2":"27"},
{"producttype2":"522275","value2":"34"}
];
var formatteddata = d3.nest()
.key(function(d) { return object.keys(d)[0]; })
.entries(data).map(function(d){ return d.values });
formatteddata.foreach(function(piedata, i) {
var color = d3.scale.category10();
var arc = d3.svg.arc()
.outerradius(radius - 10)
.innerradius(0);
var labelarc = d3.svg.arc()
.outerradius(radius - 40)
.innerradius(radius - 40);
var svg = d3.select("body")
.append("div")
.attr("width",width)
.attr("height",height)
.append("svg")
.attr("width", "100%")
.attr("height", "100%")
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var pie = d3.layout.pie()
.sort(null)
.value(function(d) {
console.log(keymap[i].value, d);
return d[keymap[i].value];
});
var g = svg.selectall(".arc")
.data(pie(piedata))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d, j) {
return color(j);
});
g.append("text")
.attr("transform", function(d) {
return "translate(" + labelarc.centroid(d) + ")";
})
.attr("dy", ".35em")
.text(function(d) {
return d.data[keymap[i].category];
});
function type(d) {
d[keymap[i].value] = +d[keymap[i].value];
return d;
}
});
.arc text {
font: 10px sans-serif;
text-anchor: middle;
}
.arc path {
stroke: #fff;
}
div{
float:left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- want to generate multiple charts in d3 in same page for different data set
- D3 with AngularJs, Multiple charts on same page with different data based on common key
- placing multiple d3 charts on the same page but applying different styling
- d3.js - multiple charts on same page from different parts of same JSON object
- React + d3 multiple charts components with different data
- Using a local geoPath variable for multiple maps using the same path data, different projections in d3.js
- Same dates for multiple data series
- Multiple d3 charts on a page with different (linear and log) axes
- Multiple D3 TreeMap on the same page with different div id's
- Creating Multiplie Pie Charts in a same page using d3 with JSON data
- d3.js reusable charts on multiple elements in same page
- Multiple d3 charts on a page using the same Angular2 component
- d3.js nvd3 issue: different charts on the same HTML page look the same (even though they have different IDs)
- How to set different color for same text in D3?
- Multiple Responsive D3 Charts on same page reusing chart script
- How to avoid redundancy with HTML code -- do not want same code for multiple html files
- d3 javascript display multiple charts on the same page
- Present same D3 Charts multiple times on a same html page
- Focus+context based zoom for multiple graphs in the same page
- 2 completely different d3 charts on same page
- D3.js: Multiple line charts mouseover with same x (time) showing different y (price)
- How to Create/Show Multiple Iterations of Charts with the same data in One Area with Buttons using D3.js
- Setting up Scale Domain and Bar Width for multiple bar charts in one page
- D3.js patterns for managing multiple elements bound to same data
- Appending multiple non-nested elements for each data member with D3.js
- multiple versions of a script on the same page (d3.js)
- Using different D3 versions on same html page
- Display multiple d3.js charts in a single html page
- d3-tooltips for multiple linked dc.js charts
- dc.js add class to data points in multiple charts based on criteria from first chart
More Query from same tag
- Calculate moving average by group/column - javascript
- Creating X-Axis with Array of Date() Objects - D3.js
- Arrays that d3 accepts for linegraph
- Sorting Horizontal Bar Chart D3js
- D3 Tree Graph Modified Data Model
- Sweep direction of arc in ring diagram
- D3 JS Time Format
- Using d3 to read in information from a CSV file, how can i store values of a particular type into a variable?
- dc.js - data table using jquery data-table plugin
- d3v4 Lollipop chart - refactored conversion - static
- Change value of a variable on click event in d3
- Change title dynamically in ndv3 pie chart
- d3.js line numbers in rows of rects
- d3.js setting all fill colors to stroke colors for a given selection
- how do i implement brush.move in d3 v3
- D3,js graph dashboard conversion to png/jpg/pdf
- DataMaps.js - Network Weather-map (inbound and outbound arcs)
- Angular-NVD3 Stacked Bar Chart With Line
- Scatterplot keeps copying itself
- D3 Graphviz d3.select.graphviz is not a function
- Use Web Worker and D3.js to asynchronously generate graphs?
- Failing to Load geoJSON for d3.js in Gist
- How to configure x-axis and manually set start/end time for d3.js
- Trying to add x and y axis D3
- d3 update number - count up/down instead of replacing number immediately
- reading in csv with date parsing
- d3 - Add single line to grouped bar graph
- Remove weekends in d3js calendar
- create values instead of random data in d3.js
- D3.JS time-series line chart with real-time data, panning and zooming