如何为csv中的每一列创建单独的图表
How to create a separate chart for every column in csv
我有一个csv,我希望每个品牌+日期都有一个单独的图表。
date,Apple,Google,Amazon,Microsoft,IBM,Facebook
2015-08-11,113.489998,690.299988,527.460022,46.41,155.509995,93.620003
2015-08-10,119.720001,663.140015,524,47.330002,156.75,94.150002
2015-08-07,115.519997,664.390015,522.619995,46.740002,155.119995,94.300003
2015-08-06,115.129997,670.150024,529.460022,46.619999,156.320007,95.120003
2015-08-05,115.400002,673.289978,537.01001,47.580002,157.899994,96.440002
现在我可以为每个品牌创建这个代码,我得到了6个单独的图表。但我认为必须有一个简单的解决方案。
// Adds the svg canvas
var chart1 = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Get the data
d3.csv("data1.php", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.m_data = +d.mrr;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([d3.min(data, function(d) { return d.mrr; }), d3.max(data, function(d) { return d.mrr; })]);
// Add the valueline path.
chart1.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
chart1.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
chart1.append("g")
.attr("class", "y axis")
.call(yAxis);
chart1.append("text")
.attr("x", width / 2 )
.attr("y", 0)
.style("text-anchor", "middle")
.text("mrr");
});
现在把它放在函数中
add_chart("chart1",'mrr');
add_chart("chart2",'arr');
function add_chart(id,field_name){
console.log(id+', ' + field_name );
var my_object = {};
// Adds the svg canvas
my_object[id] = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Get the data
d3.csv("data1.php", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.m_data = +d[field_name];
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([d3.min(data, function(d) { return d[field_name]; }), d3.max(data, function(d) { return d[field_name]; })]);
// Add the valueline path.
my_object[id].append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
my_object[id].append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
my_object[id].append("g")
.attr("class", "y axis")
.call(yAxis);
my_object[id].append("text")
.attr("x", width / 2 )
.attr("y", 0)
.style("text-anchor", "middle")
.text(field_name);
});
}
相关文章:
- 柱状图后面的一列
- 数据表通过分析一列的值对其进行排序
- Datatables-如何在未显示所有列的情况下禁用表中最后一列的排序
- 获取表中每行的一列的内容,然后循环访问每个内容
- 对象数组-与其他列数据相比,增加一列
- 在最初的查询/表格绘制中只提取谷歌电子表格的一列
- jQuery Datatables:单击一列时对另一列进行排序
- 如何使用jQuery和乘法行和列创建HTML表
- 我的砖石图像只显示在一列中
- 删除“;使用javascript从pentaho中的一列中选择“[”
- 使用 Javascript 在一列中更改属性内容
- jquery插件 - 已选择 - 多项选择不在一行中列出,而是列在一列中
- 单击包含按钮的最后一列时获取表的第一列值
- 排除行选择中的最后一列
- 取消表中一列的onclick事件
- jqGrid有条件地创建一列
- Dgrid格式化程序基于另一列中的值创建列
- 如何在不使用固定大小的情况下创建两列(一列有两行,另一列有一行)
- 如何为csv中的每一列创建单独的图表
- jQuery数据表插件,动态创建的最后一列平均在fnRowCallBack是不可排序的