基于坡度填充的D3高程图

时间:2015-03-16 20:13:55

标签: csv d3.js fill elevation

我正在尝试根据点之间的渐变创建实线图。我得到了一些帮助创建填充,但我不认为我理解它是如何确定使用哪个填充以及如何前进以获得我想要的东西。我希望有0%,4%,7%,10%,> 12%的线条以及那些线条的负数。我知道d3中有一个颜色功能,但我不知道如何在这里工作。工作Plunker

我拍摄的内容看起来像this

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

body { font: 12px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}

.line {
    fill: url(#line-gradient);
    stroke: url(#line-gradient);
    stroke-width: 2px;
}

</style>
<body>

<!-- load the d3.js library -->    
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 1300 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Set the ranges
var x = d3.scale.linear().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(5);

// Define the line
var valueline = d3.svg.line()
    .interpolate("basis")
    .x(function(d) { return x(d.distance); })
    .y(function(d) { return y(d.elevation); });

// Adds the svg canvas
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 + ")");

// Get the data
d3.csv("data.csv", function(error, data) {
    data.forEach(function(d) {
        d.distance = +d.distance;
        d.elevation = +d.elevation;
    });

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


    svg.append("linearGradient")                
        .attr("id", "line-gradient")            
        .attr("gradientUnits", "userSpaceOnUse")    


    .selectAll("stop")                      
        .data([                             
            {offset: "0%", color: "red"},       
            {offset: "4%", color: "red"},   
            {offset: "4%", color: "black"},     
            {offset: "6%", color: "black"},     
            {offset: "6%", color: "lawngreen"}, 
            {offset: "9%", color: "lawngreen"}  
        ])                  
    .enter().append("stop")         
        .attr("offset", function(d) { return d.offset; })   
        .attr("stop-color", function(d) { return d.color; });

    // Add the valueline path.
    var maxX = x(d3.extent(data, function(d) { return d.distance; })[1]);
    svg.append("path")
        .attr("class", "line").attr("fill","url(#")
        .attr("d", ''+valueline(data)+"L0,"+y(0)+'L'+maxX+","+y(0));

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

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

});

</script>
</body>

0 个答案:

没有答案