如何在路径上涂色?

时间:2017-03-07 11:22:45

标签: d3.js

window.onload = function(){
    var margin = {
    top: 30,
    right: 20,
    bottom: 30,
    left: 50
};var width = 600 - margin.left - margin.right;
var height = 270 - margin.top - margin.bottom;

var parseDate = d3.timeFormat("%c").parse;

var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);

var xAxis = d3.axisBottom()
    .scale(x).ticks(5);

var yAxis = d3.axisLeft()
    .scale(y).ticks(5);

var valueline = d3.line()
    .x(function (d) {
      return x(d.date);
    })
    .y(function (d) {
      return y(d.close);
    });

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
var data = [{
    date: "1 ",
    close: "58.13"
}, {
    date: "2 ",
    close: "53.98"
}, {
    date: "3 ",
    close: "67.00"
}, {
    date: "4 ",
    close: "89.70"
}, {
    date: "5 ",
    close: "99.00"
}, {
    date: "6 ",
    close: "20.00"
}];

var data2 = [{
    date: "1 ",
    close: "52.13"
}, {
    date: "2 ",
    close: "30.98"
}, {
    date: "3 ",
    close: "36.00"
}, {
    date: "4 ",
    close: "40.70"
}, {
    date: "5 ",
    close: "20.00"
}, {
    date: "6 ",
    close: "96.00"
}];

data.forEach(function (d) {
    d.date = d.date;
    d.close = +d.close;
});

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

svg.append("path") // Add the valueline path.
.attr("d", valueline(data)).attr("stroke","red");

svg.append("path") // Add the valueline path.
.attr("d", valueline(data2)).attr("stroke","blue");

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

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

我是新手。我想为我正在使用的笔画添加颜色,但我无法做到。我添加了在代码中应用笔触颜色的属性,但它不起作用。我希望每一行都有不同的颜色。请帮我解决可行的解决方案。

1 个答案:

答案 0 :(得分:-1)

您的代码似乎运行正常。这是d3的实现方式。

var margin = {
  top: 30,
  right: 20,
  bottom: 30,
  left: 50
};
var width = 600 - margin.left - margin.right;
var height = 270 - margin.top - margin.bottom;

var parseDate = d3.timeFormat("%c").parse;

var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);

var xAxis = d3.axisBottom()
  .scale(x).ticks(5);

var yAxis = d3.axisLeft()
  .scale(y).ticks(5);

var valueline = d3.line()
  .x(function(d) {
    return x(d.date);
  })
  .y(function(d) {
    return y(d.close);
  });

var colors = d3.scaleOrdinal(d3.schemeCategory10);

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
var data = [{
  date: "1 ",
  close: "58.13"
}, {
  date: "2 ",
  close: "53.98"
}, {
  date: "3 ",
  close: "67.00"
}, {
  date: "4 ",
  close: "89.70"
}, {
  date: "5 ",
  close: "99.00"
}, {
  date: "6 ",
  close: "20.00"
}];

var data2 = [{
  date: "1 ",
  close: "52.13"
}, {
  date: "2 ",
  close: "30.98"
}, {
  date: "3 ",
  close: "36.00"
}, {
  date: "4 ",
  close: "40.70"
}, {
  date: "5 ",
  close: "20.00"
}, {
  date: "6 ",
  close: "96.00"
}];

data.forEach(function(d) {
  d.date = d.date;
  d.close = +d.close;
});

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

svg.selectAll("path.line").data([data, data2])
  .enter().
append("path") // Add the valueline path.
  .attr("class", "line")
  .attr("d", valueline)
  .attr("stroke", function(d, i) {
    return colors(i);
  });


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

svg.append("g") // Add the Y Axis
  .attr("class", "y axis")
  .call(yAxis);
path {
  fill: none;
}
<script src="https://d3js.org/d3.v4.min.js"></script>d