使用d3.js在线图中的IF语句描边

时间:2013-05-08 14:00:38

标签: javascript if-statement d3.js svg attr

我使用数据库中的数据绘制了一个图表,当数据库中的数字高于500时,我希望该行变为红色(如下所示)。不幸的是,它没有这种方式,我尝试了其他一些东西,但没有成功。

虽然虚线确实可以使用代码.style("stroke-dasharray", ("3, 3"))

我的问题:当y值高于d3.js中的某个点时,是否可以让笔触颜色变为红色?

// Adds the svg canvas
var svg = d3.select("body")                                 // Explicitly state where the svg element will go on the web page (the 'body')
    .append("svg")                                          // Append 'svg' to the html 'body' of the web page
        .attr("width", width + margin.left + margin.right)  // Set the 'width' of the svg element
        .attr("height", height + margin.top + margin.bottom)// Set the 'height' of the svg element
    .append("g")                                            // Append 'g' to the html 'body' of the web page
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // in a place that is the actual area for the graph

d3.json("php/data2.php", function(error, data) {            // Go to the data folder (in the current directory) and read in the data.tsv file
    data.forEach(function(d) {                              // For all the data values carry out the following
        d.date = parseDate(d.date);                         // Parse the date from a set format (see parseDate)
        d.close = +d.close;                                 // makesure d.close is a number, not a string
});

// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));      // set the x domain so be as wide as the range of dates we have.
y.domain([0, d3.max(data, function(d) { return d.close; })]);   // set the y domain to go from 0 to the maximum value of d.close

// Add the valueline path.
svg.append("path")                                      // append the valueline line to the 'path' element
    .attr("class", "line")                              // apply the 'line' CSS styles to this path
    .style("stroke-dasharray", ("3, 3"))
    .attr("d", valueline(data));                        // call the 'valueline' finction to draw the line

// Add the X Axis
svg.append("g")                                         // append the x axis to the 'g' (grouping) element
    .attr("class", "x axis")                            // apply the 'axis' CSS styles to this path
    .attr("transform", "translate(0," + height + ")")   // move the drawing point to 0,height
    .call(xAxis);                                       // call the xAxis function to draw the axis

// Add the Y Axis
svg.append("g")                                         // append the y axis to the 'g' (grouping) element
    .attr("class", "y axis")                            // apply the 'axis' CSS styles to this path
    .call(yAxis);                                       // call the yAxis function to draw the axis

1 个答案:

答案 0 :(得分:0)

您的数据可能没有正确绑定到您的元素,但如果没有完整的代码,很难说清楚。你可能需要......

svg.selectAll("path")
    .data(data)
    .enter()
    .append("path")                                    
    .attr("class", "line")                              
    .attr("stroke", function(d) {..});

您可以在此处查看代码的简化演示:http://jsfiddle.net/duopixel/V84Fz/