Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何调整D3折线图的大小_Javascript_D3.js - Fatal编程技术网

Javascript 如何调整D3折线图的大小

Javascript 如何调整D3折线图的大小,javascript,d3.js,Javascript,D3.js,我有一个关于调整D3多折线图大小的问题。我跟着。但是我很难更新图表中的元素。我正在成功地更新轴,但不是线本身。我也不确定如何更新图表中的点和文本 谢谢你的帮助 以下是我目前掌握的代码: <svg id="graph"></svg> <script> // Set the dimensions of the canvas / graph var margin = 40, width = 960 - margin*2, height = 500

我有一个关于调整D3多折线图大小的问题。我跟着。但是我很难更新图表中的元素。我正在成功地更新轴,但不是线本身。我也不确定如何更新图表中的点和文本

谢谢你的帮助

以下是我目前掌握的代码:

<svg id="graph"></svg>

<script>

// Set the dimensions of the canvas / graph
var margin = 40,
    width = 960 - margin*2,
    height = 500 - margin*2;


// Parse the date / time
var parseDate = d3.time.format("%Y-%m-%d").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(8);

// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.average_ticnum); });

var valueline2 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.average_fatiguenum); });

var valueline3 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.average_stressnum); });

var valueline4 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.sum_nf_sugars); });

var valueline5 = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.sum_nf_total_carbohydrate); });

// Adds the svg canvas
var graph = d3.select("#graph")
    .attr("width", width + margin*2)
    .attr("height",  height + margin*2)
.append("g")
   .attr("transform", "translate(" + margin + "," + margin + ")");


// Get the data
d3.json("progress_output.php?useremail=<?php echo $useremail; ?>",     function(error, data) {
data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.average_ticnum = +d.average_ticnum;
    d.fatiguenum = +d.average_fatiguenum;
    d.stressnum = +d.average_stressnum;
});

// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0,12]);

// Add the valueline path.
graph.append("path")
.attr("class", "line")
.style("stroke", "#891f83")
.attr("id","ticLine")
.attr("d", valueline(data));

graph.append("path")
.attr("class", "line")
.style("stroke", "#7db6e3")
.attr("id","fatigueLine")
.attr("d", valueline2(data));

graph.append("path")
.attr("class", "line")
.style("stroke", "#36376a")
.attr("id","stressLine")
.attr("d", valueline3(data));

graph.append("path")
.attr("class", "line")
.style("stroke", "#9bcf81")
.attr("id","sugarLine")
.attr("d", valueline4(data));

graph.append("path")
.attr("class", "line")
.style("stroke", "#efa465")
.attr("id","carbLine")
.attr("d", valueline5(data));

// Add the Dots
graph.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("id","ticDots")
.attr("r", 3.5)
.style("fill", "#891f83")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.average_ticnum); });

graph.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 3.5)
.style("fill", "#7db6e3")
.attr("id","fatigueDots")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.average_fatiguenum); });

graph.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 3.5)
.style("fill", "#36376a")
.attr("id", "stressDots")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.average_stressnum); });

graph.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("id","sugarDots")
.attr("r", 3.5)
.style("fill", "#9bcf81")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.sum_nf_sugars); });

graph.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 3.5)
.style("fill", "#efa465")
.attr("id","carbDots")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.sum_nf_total_carbohydrate); });


// Add the X Axis
graph.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

// Add the Y Axis
graph.append("g")
.attr("class", "y axis")
.call(yAxis);

//Add the Tic title
graph.append("text")
.attr("x", 15)             
.attr("y", 0)    
.attr("class", "legend")
.attr("id", "ticText")
.style("color", "#891f83")
.style("fill", "#891f83") 
.style("cursor", "pointer")
.style("font-family", "cooperbook")        
.on("click", function(){
        // Determine if current line is visible
        var active   = ticLine.active ? false : true,
        newOpacity = active ? 0 : 1;
        // Hide or show the elements
        width = 200;
        height = 200;
        d3.select("#ticLine").style("opacity", newOpacity);
        d3.selectAll("#ticDots").style("opacity", newOpacity);

        // Update whether or not the elements are active
        ticLine.active = active;
    })
.text("Tic Severity");

//Add the fatigue title
graph.append("text")
.attr("x", 115)             
.attr("y", 0)    
.attr("class", "legend")
.style("color", "#7bd6e3")
.style("fill", "#7bd6e3")
.style("cursor", "pointer")
.style("font-family", "cooperbook")
.on("click", function(){
        // Determine if current line is visible
        var active   = fatigueLine.active ? false : true,
        newOpacity = active ? 0 : 1;
        // Hide or show the elements
        d3.select("#fatigueLine").style("opacity", newOpacity);
        d3.selectAll("#fatigueDots").style("opacity", newOpacity);
        // Update whether or not the elements are active
        fatigueLine.active = active;
    })
.text("Fatigue");


//Add the Stress title
graph.append("text")
.attr("x", 190)             
.attr("y", 0)    
.attr("class", "legend")
.style("color", "#36376a")
.style("fill", "#36376a")
.style("cursor", "pointer")
.style("font-family", "cooperbook")          
.on("click", function(){
        // Determine if current line is visible
        var active   = stressLine.active ? false : true,
        newOpacity = active ? 0 : 1;
        // Hide or show the elements
        d3.select("#stressLine").style("opacity", newOpacity);
        d3.selectAll("#stressDots").style("opacity", newOpacity);
        // Update whether or not the elements are active
        stressLine.active = active;
    })
.text("Stress");

//Add the sugar title
graph.append("text")
.attr("x", 250)             
.attr("y", 0)    
.attr("class", "legend")
.style("color", "#9bcf81")
.style("fill", "#9bcf81")
.style("cursor", "pointer")
.style("font-family", "cooperbook")          
.on("click", function(){
        // Determine if current line is visible
        var active   = sugarLine.active ? false : true,
        newOpacity = active ? 0 : 1;
        // Hide or show the elements
        d3.select("#sugarLine").style("opacity", newOpacity);
        d3.selectAll("#sugarDots").style("opacity", newOpacity);
        // Update whether or not the elements are active
        sugarLine.active = active;
    })
.text("Sugars");

//Add the carb title
graph.append("text")
.attr("x", 320)             
.attr("y", 0)    
.attr("class", "legend")
.style("color", "#efa465")
.style("fill", "#efa465")
.style("cursor", "pointer")
.style("font-family", "cooperbook")          
.on("click", function(){
        // Determine if current line is visible
        var active   = carbLine.active ? false : true,
        newOpacity = active ? 0 : 1;
        // Hide or show the elements
        d3.select("#carbLine").style("opacity", newOpacity);
        d3.selectAll("#carbDots").style("opacity", newOpacity);
        // Update whether or not the elements are active
        carbLine.active = active;
    })
.text("Carbohydrates");
});



function resize() {
/* Find the new window dimensions */
var width = parseInt(d3.select("#graph").style("width")) - margin*2,
height = parseInt(d3.select("#graph").style("height")) - margin*2;

/* Update the range of the scale with new width/height */
x.range([0, width]).nice(d3.time.year);
y.range([height, 0]).nice();

/* Update the axis with the new scale */
graph.select('.x.axis')
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

graph.select('.y.axis')
  .call(yAxis);

/* Force D3 to recalculate and update the line */
graph.selectAll('.line')
  .attr("d", valueline(data));
}


d3.select(window).on('resize', resize); 

//设置画布/图形的尺寸
var保证金=40,
宽度=960-边距*2,
高度=500-边距*2;
//解析日期/时间
var parseDate=d3.time.format(“%Y-%m-%d”).parse;
//设定范围
var x=d3.time.scale().range([0,width]);
变量y=d3.scale.linear().range([height,0]);
//定义轴
var xAxis=d3.svg.axis().scale(x)
.方向(“底部”)。刻度(5);
var yAxis=d3.svg.axis().scale(y)
.方向(“左”)。刻度(8);
//界定界线
var valueline=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.average_ticnum);});
var valueline2=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.average_fatiguenum);});
var valueline3=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.average_stressnum);});
var valueline4=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.sum_nf_sugars);});
var valueline5=d3.svg.line()
.x(函数(d){返回x(d.date);})
.y(函数(d){返回y(d.sum_nf_total_carbone)});
//添加svg画布
var graph=d3。选择(“曲线图”)
.attr(“宽度”,宽度+边距*2)
.attr(“高度”,高度+边距*2)
.附加(“g”)
.attr(“转换”、“转换”(“+margin+”,“+margin+”));
//获取数据
d3.json(“progress_output.php?useremail=”,函数(错误,数据){
data.forEach(函数(d){
d、 日期=解析日期(d.date);
d、 average_ticnum=+d.average_ticnum;
d、 疲劳指数=+d.平均疲劳指数;
d、 stressnum=+d.平均值_stressnum;
});
//缩放数据的范围
x、 域(d3.extent(数据,函数(d){返回d.date;}));
y、 域([0,12]);
//添加valueline路径。
graph.append(“路径”)
.attr(“类”、“行”)
.风格(“笔划”,“#891f83”)
.attr(“id”、“ticLine”)
.attr(“d”,valueline(数据));
graph.append(“路径”)
.attr(“类”、“行”)
.style(“笔划”,“#7db6e3”)
.attr(“id”、“疲劳线”)
.attr(“d”,valueline2(数据));
graph.append(“路径”)
.attr(“类”、“行”)
.风格(“笔划”,“#36376a”)
.attr(“id”、“压力线”)
.attr(“d”,valueline3(数据));
graph.append(“路径”)
.attr(“类”、“行”)
.样式(“笔划”,“9bcf81”)
.attr(“id”、“sugarLine”)
.attr(“d”,valueline4(数据));
graph.append(“路径”)
.attr(“类”、“行”)
.style(“笔划”、“efa465”)
.attr(“id”、“carbLine”)
.attr(“d”,valueline5(数据));
//加点
图形。选择全部(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“id”、“ticDots”)
.attr(“r”,3.5)
.样式(“填充”,“#891f83”)
.attr(“cx”,函数(d){返回x(d.date);})
.attr(“cy”,函数(d){返回y(d.average_ticnum);});
图形。选择全部(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“r”,3.5)
.style(“填充”,“#7db6e3”)
.attr(“id”、“疲劳测试”)
.attr(“cx”,函数(d){返回x(d.date);})
.attr(“cy”,函数(d){returny y(d.average_fatiguenum);});
图形。选择全部(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“r”,3.5)
.样式(“填充”、“#36376a”)
.attr(“id”、“应力点”)
.attr(“cx”,函数(d){返回x(d.date);})
.attr(“cy”,函数(d){returny y(d.average_stressnum);});
图形。选择全部(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“id”、“sugarDots”)
.attr(“r”,3.5)
.样式(“填充”、“9bcf81”)
.attr(“cx”,函数(d){返回x(d.date);})
.attr(“cy”,函数(d){returny y(d.sum_nf_sugars);});
图形。选择全部(“点”)
.数据(数据)
.enter().append(“圆”)
.attr(“r”,3.5)
.样式(“填充”、“efa465”)
.attr(“id”、“Carbbots”)
.attr(“cx”,函数(d){返回x(d.date);})
.attr(“cy”,函数(d){返回y(d.sum_nf_total_carbone);});
//添加X轴
图.附加(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis);
//添加Y轴
图.附加(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis);
//添加Tic标题
图形追加(“文本”)
.attr(“x”,15)
.attr(“y”,0)
.attr(“类”、“图例”)
.attr(“id”、“ticText”)
.风格(“颜色”,“#891f83”)
.样式(“填充”,“#891f83”)
.style(“光标”、“指针”)
.style(“字体系列”、“cooperbook”)
.on(“单击”,函数(){
//确定当前行是否可见
var active=ticLine.active?false:true,
newOpacity=active?0:1;
//隐藏或显示元素
宽度=200;
高度=200;
d3.选择(“#ticLine”).style(“不透明度”,newOpacity);
d3.选择所有(#ticDots”).style(“不透明度”,newOpacity);
//更新元素是否处于活动状态
ticLine.active=active;
})
.文本(“Tic严重性”);
//添加疲劳标题
图形追加(“文本”)
.attr(“x”,115)
.attr(“y”,0)
.attr(“类”、“图例”)
.风格(“颜色”,“#7bd6e3”)
.样式(“填充”,“#7bd6e3”)
.style(“光标”、“指针”)
.style(“字体系列”、“cooperbook”)
.on(“单击”,函数(){
//确定当前行是否可见
var active=fatigueLine.active?假:真,
newOpacity=active?0:1;
//隐藏或显示元素
d3.选择(“#疲劳线”).style(“不透明度”,newOpacity);
d3.选择All(#fatigueDots”).style(“不透明度”,newOpacity);
//更新元素是否处于活动状态
fatigueLine.active=active;
})
.文本(“疲劳”);
//添加重音标题
图形追加(“文本”)
.attr(“x”,190)
.attr(“y”,0)
.attr(“类”、“图例”)
.风格(“颜色”,“#36376a”)
.样式(“填充”、“#36376a”)
.style(“光标”、“指针”)
.style(“字体系列”、“cooperbook”)
.on(“单击”,函数(){
//确定当前行是否可见
var active=stressLine.active?false:true,
newOpacity=active?0:1;
//隐藏或显示元素
d3.选择(“#应力