我正在使用dimple js和tryin构建一个图表来调整工具提示。我想出了如何添加“性别”和“存活率”。现在,我想添加存储在flatgroups对象中的存活计数。我该怎么做呢?下面的工作(使用for循环)似乎不起作用。它给我一个错误,告诉我扁平组对象是未定义的,并且它不能接受未定义对象的长度。谢谢!
function draw_bar(data) {
var svg = dimple.newSvg("#chart1", 800, 600);
// Group/nest data by gender and calculate survival rate and number of people that survived per gender
var grouped_data = d3.nest()
.key(function (d) {return d.Sex;})
.rollup(function (v) {return {"Survival Rate": d3.mean(v,
function (d) {
return d.Survived;
}
),
"Survival Count": d3.sum(v,
function (d) {
return d.Survived;
}
)
};
}
)
.entries(data);
// flatten the data structure stored in grouped_data
var flatgroups = [];
grouped_data.forEach(function (group) {
flatgroups.push({
"Gender": group.key,
"Survival Rate": group.values["Survival Rate"],
"Survival Count": group.values["Survival Count"]
});
});
// Construct chart, set axis labels and draw it
var chart = new dimple.chart(svg, flatgroups);
var x = chart.addCategoryAxis("x", "Gender");
x.title = "Gender";
var y = chart.addMeasureAxis("y", "Survival Rate");
y.title = "Survival Rate";
// Format y-axis to show proportions with 2 decimals
y.tickFormat = ',.2f';
var series = chart.addSeries("Gender", dimple.plot.bar);
series.getTooltipText = function (e) {
var key = e.key;
for (i in flatgroups) {
if (i.key == key) {
return [ "Gender" + ": " + e.cx,
"Survival Rate" + ": " + (e.cy).toFixed(2),
"Survival Count" + ": " + i["Survival Count"]
];
};
};
};
chart.assignColor("female", "red")
chart.assignColor("male", "blue")
chart.draw();https://stackoverflow.com/questions/38204790
复制相似问题