Javascript 单图中二元图与线图的组合
在单个图d3.js中结合二元面积图和线图是我面临的一项艰巨任务。如果我成功地使用了二元面积图,我就无法在同一个图中绘制折线图,反之亦然。任何一个都在为我工作。我提供了位于x轴和y轴的二元面积的数据。是否可以用相同的数据绘制折线图?我在网上查阅了很多可用的图表,但都找不到。需要帮助用二元面积图绘制折线图Javascript 单图中二元图与线图的组合,javascript,d3.js,Javascript,D3.js,在单个图d3.js中结合二元面积图和线图是我面临的一项艰巨任务。如果我成功地使用了二元面积图,我就无法在同一个图中绘制折线图,反之亦然。任何一个都在为我工作。我提供了位于x轴和y轴的二元面积的数据。是否可以用相同的数据绘制折线图?我在网上查阅了很多可用的图表,但都找不到。需要帮助用二元面积图绘制折线图 <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.area {
fill: steelblue;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
// data
var data = [{
"month": "January",
"high": "59.5",
"low" : "57.0"
}, {
"month": "February",
"high": "59.5",
"low" : "53.4"
}, {
"month": "March",
"high": "59.0",
"low" : "53.4"
}, {
"month": "April",
"high": "62.4",
"low" : "54.7"
}, {
"month": "May",
"high": "58.3",
"low" : "52.7"
}, {
"month": "June",
"high": "62.1",
"low" : "54.5"
}, {
"month": "July",
"high": "60.8",
"low" : "53.4"
}, {
"month": "August",
"high": "61.0",
"low" : "52.5"
}, {
"month": "September",
"high": "62.4",
"low" : "52.9"
}, {
"month": "October",
"high": "65.3",
"low" : "54.0"
}, {
"month": "November",
"high": "70.3",
"low" : "55.0"
}, {
"month": "December",
"high": "82.2",
"low" : "58.6"
}];
// margins
var margin = {top: 20, right: 30, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parsing data
var parseDate = d3.time.format("%B").parse;
// x-axis encoding
var x = d3.time.scale()
.range([0, width]);
// y-axis encoding
var y = d3.scale.linear()
.range([height, 0]);
// x-axis scaling
var xAxis = d3.svg.axis()
.scale(x)
.tickFormat(d3.time.format("%b"))
.orient("bottom");
// y-axis scaling
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
// area draw
var area = d3.svg.area()
.x(function(d) { return x(d.month); })
.y0(function(d) { return y(d.low); })
.y1(function(d) { return y(d.high); });
// adding the svg element
var svg = 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 + ")");
//function
function chart(error, data) {
data.forEach(function(d) {
d.month = parseDate(d.month);
d.low = +d.low;
d.high = +d.high;
});
// setting scales
x.domain(d3.extent(data, function(d) { return d.month;})).range([0, 600]);
y.domain([d3.min(data, function(d) { return d.low; }),
d3.max(data, function(d) { return d.high; })]);
// area logic
svg.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
// appending x axis data
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// text label for the x axis
svg.append("text")
.attr("x", 0 )
.attr("y", 480 )
.style("text-anchor", "middle")
.text("2015");
// appending y axis data
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Temperature (ºF)");
}
chart([],data);
/*);*/
</script>
身体{
字体:10px无衬线;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
形状渲染:边缘清晰;
}
.x轴路径{
显示:无;
}
.区域{
填充:钢蓝;
}
//资料
风险值数据=[{
“月”:“一月”,
“高”:“59.5”,
“低”:“57.0”
}, {
“月”:“二月”,
“高”:“59.5”,
“低”:“53.4”
}, {
“月”:“三月”,
“高”:“59.0”,
“低”:“53.4”
}, {
“月”:“四月”,
“高”:“62.4”,
“低”:“54.7”
}, {
“月”:“五月”,
“高”:“58.3”,
“低”:“52.7”
}, {
“月”:“六月”,
“高”:“62.1”,
“低”:“54.5”
}, {
“月”:“七月”,
“高”:“60.8”,
“低”:“53.4”
}, {
“月”:“八月”,
“高”:“61.0”,
“低”:“52.5”
}, {
“月”:“九月”,
“高”:“62.4”,
“低”:“52.9”
}, {
“月”:“十月”,
“高”:“65.3”,
“低”:“54.0”
}, {
“月”:“十一月”,
“高”:“70.3”,
“低”:“55.0”
}, {
“月”:“十二月”,
“高”:“82.2”,
“低”:“58.6”
}];
//边距
var margin={顶部:20,右侧:30,底部:30,左侧:50},
宽度=960-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
//解析数据
var parseDate=d3.time.format(“%B”).parse;
//x轴编码
var x=d3.time.scale()
.范围([0,宽度]);
//y轴编码
变量y=d3.scale.linear()
.范围([高度,0]);
//x轴缩放
var xAxis=d3.svg.axis()
.比例(x)
.tickFormat(d3.time.format(“%b”))
.东方(“底部”);
//y轴缩放
var yAxis=d3.svg.axis()
.比例(y)
.东方(“左”);
//面积图
var area=d3.svg.area()
.x(函数(d){返回x(d.month);})
.y0(函数(d){返回y(d.low);})
.y1(函数(d){返回y(d.high);});
//添加svg元素
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
//作用
功能图(错误、数据){
data.forEach(函数(d){
d、 月=解析日期(d.月);
d、 低=+d.低;
d、 高=+d.高;
});
//设置刻度
x、 域(d3.extent(数据,函数(d){returnd.month;})).range([0600]);
y、 域([d3.min(数据,函数(d){返回d.low;}),
d3.max(数据,函数(d){返回d.high;})];
//区域逻辑
追加(“路径”)
.基准(数据)
.attr(“类别”、“区域”)
.attr(“d”,区域);
//附加x轴数据
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
//x轴的文本标签
svg.append(“文本”)
.attr(“x”,0)
.attr(“y”,480)
.style(“文本锚定”、“中间”)
.文本(“2015”);
//附加y轴数据
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis)
.append(“文本”)
.attr(“变换”、“旋转(-90)”)
.attr(“y”,6)
.attr(“dy”,“.71em”)
.style(“文本锚定”、“结束”)
.文本(“温度(ºF)”;
}
图表([],数据);
/*);*/
这样做
工作代码
身体{
字体:10px无衬线;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
形状渲染:边缘清晰;
}
.x轴路径{
显示:无;
}
.区域{
填充:钢蓝;
}
.线路{
填充:透明;
笔画:红色;
笔画宽度:3px;
}
.line1{
填充:透明;
笔画:绿色;
笔画宽度:3px;
}
//资料
风险值数据=[{
“月”:“一月”,
“高”:“59.5”,
“低”:“57.0”
}, {
“月”:“二月”,
“高”:“59.5”,
“低”:“53.4”
}, {
“月”:“三月”,
“高”:“59.0”,
“低”:“53.4”
}, {
“月”:“四月”,
“高”:“62.4”,
“低”:“54.7”
}, {
“月”:“五月”,
“高”:“58.3”,
“低”:“52.7”
}, {
“月”:“六月”,
“高”:“62.1”,
“低”:“54.5”
}, {
“月”:“七月”,
“高”:“60.8”,
“低”:“53.4”
}, {
“月”:“八月”,
“高”:“61.0”,
“低”:“52.5”
}, {
“月”:“九月”,
“高”:“62.4”,
“低”:“52.9”
}, {
“月”:“十月”,
“高”:“65.3”,
“低”:“54.0”
}, {
“月”:“十一月”,
“高”:“70.3”,
“低”:“55.0”
}, {
“月”:“十二月”,
“高”:“82.2”,
“低”:“58.6”
}];
//边距
var margin={顶部:20,右侧:30,底部:30,左侧:50},
宽度=960-margin.left-margin.right,
高度=500-margin.top-margin.bottom;
//解析数据
var parseDate=d3.time.format(“%B”).parse;
//x轴编码
var x=d3.time.scale()
.范围([0,宽度]);
//y轴编码
变量y=d3.scale.linear()
.范围([高度,0]);
//x轴缩放
var xAxis=d3.svg.axis()
.比例(x)
.tickFormat(d3.time.format(“%b”))
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.area {
fill: steelblue;
}
.line {
fill: transparent;
stroke:red;
stroke-width:3px;
}
.line1 {
fill: transparent;
stroke:green;
stroke-width:3px;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
// data
var data = [{
"month": "January",
"high": "59.5",
"low" : "57.0"
}, {
"month": "February",
"high": "59.5",
"low" : "53.4"
}, {
"month": "March",
"high": "59.0",
"low" : "53.4"
}, {
"month": "April",
"high": "62.4",
"low" : "54.7"
}, {
"month": "May",
"high": "58.3",
"low" : "52.7"
}, {
"month": "June",
"high": "62.1",
"low" : "54.5"
}, {
"month": "July",
"high": "60.8",
"low" : "53.4"
}, {
"month": "August",
"high": "61.0",
"low" : "52.5"
}, {
"month": "September",
"high": "62.4",
"low" : "52.9"
}, {
"month": "October",
"high": "65.3",
"low" : "54.0"
}, {
"month": "November",
"high": "70.3",
"low" : "55.0"
}, {
"month": "December",
"high": "82.2",
"low" : "58.6"
}];
// margins
var margin = {top: 20, right: 30, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parsing data
var parseDate = d3.time.format("%B").parse;
// x-axis encoding
var x = d3.time.scale()
.range([0, width]);
// y-axis encoding
var y = d3.scale.linear()
.range([height, 0]);
// x-axis scaling
var xAxis = d3.svg.axis()
.scale(x)
.tickFormat(d3.time.format("%b"))
.orient("bottom");
// y-axis scaling
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
// area draw
var area = d3.svg.area()
.x(function(d) { return x(d.month); })
.y0(function(d) { return y(d.low); })
.y1(function(d) { return y(d.high); });
var line = d3.svg.line()
.x(function(d) { return x(d.month); })
.y(function(d) { return y(d.high); });
var line1 = d3.svg.line()
.x(function(d) { return x(d.month); })
.y(function(d) { return y(d.low); });
// adding the svg element
var svg = 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 + ")");
//function
function chart(error, data) {
data.forEach(function(d) {
d.month = parseDate(d.month);
d.low = +d.low;
d.high = +d.high;
});
// setting scales
x.domain(d3.extent(data, function(d) { return d.month;})).range([0, 600]);
y.domain([d3.min(data, function(d) { return d.low; }),
d3.max(data, function(d) { return d.high; })]);
// area logic
svg.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
svg.append("path")
.datum(data)
.attr("class", "line1")
.attr("d", line1);
// appending x axis data
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// text label for the x axis
svg.append("text")
.attr("x", 0 )
.attr("y", 480 )
.style("text-anchor", "middle")
.text("2015");
// appending y axis data
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Temperature (ºF)");
}
chart([],data);
/*);*/
</script>