【发布时间】:2019-05-27 19:04:36
【问题描述】:
我为美国每个州的 4 个数据值绘制了堆积条形图。我一直在处理的 CSV 文件如下所示:
这是我为堆积条形图编写的 JS 代码:
// Read the data from CSV
d3.csv('https://raw.githubusercontent.com/krithikaragha/ResPy/master/Flask%20App/static/data/tobacco_use_by_state.csv', function(data) {
var chartData = []; // Array containing all the traces
function makeTrace(d) { // Function to make all 50 traces
return {
x: ["Smokes Everyday", "Smokes Somedays", "Former Smoker", "Never Smoked"],
y: [d.smokesEveryday, d.smokesSomedays, d.formerSmoker, d.neverSmoked],
name: d.state,
type: 'bar'
}
}
// Loop through all rows of the data
for(var i = 0; i < data.length; i++) {
// Call makeTrace to create a trace with index i
chartData.push(makeTrace(data[i]));
// Define a chart layout
var layout = {
barmode: 'stack',
width: 1200,
height: 800
};
// Plot the stacked bar chart
Plotly.newPlot('bar', chartData, layout);
}
});
我想要达到的结果是:我希望美国的每个州都有自己的条形图,上面有各自的 4 个值(smokesEveryday、smokesSomedays、formerSmoker 和 neverSmoked)
有什么方法可以实现吗?提前致谢。
【问题讨论】:
标签: javascript stacked-chart plotly.js