多系列折线图

时间:2019-10-11 00:19:54

标签: javascript d3.js linechart

我正在尝试创建多个折线图。我发现了this个问题,似乎在做与尝试做的事情类似的示例,但是由于无法运行该示例,因此该示例似乎并不完整。下面是我可以创建的最简单的示例,以演示我正在尝试做的事情,但是它没有用。它应创建一个包含两个系列的折线图,一个用于打开,一个用于关闭。我尝试使用与HTML表示例here中使用的相同原理。 我正在使用d3版本5。

const data = [
  { date: "Wed, 14 Aug 2019 00:00:00 GMT", close: 2.92, open: 3.2 },
  { date: "Wed, 28 Aug 2019 00:00:00 GMT", close: 2.89, open: 2.99 },
  { date: "Fri, 30 Aug 2019 00:00:00 GMT", close: 2.88, open: 3.4 },
  { date: "Mon, 02 Sep 2019 00:00:00 GMT", close: 2.89, open: 3.5 },
];

const margin = { top: 20, right: 20, bottom: 30, left: 50 };
const width = 960 - margin.left - margin.right;
const height = 500 - margin.top - margin.bottom;

// set the ranges
const x = d3
  .scaleTime()
  .domain(d3.extent(data, d => d.date)) 
  .range([0, width]);
const y = d3
  .scaleLinear()
  .domain([0, 4])
  .range([height, 0]);

const g = 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})`);

const seriesNames = ["open", "close"]

// create array where each element is a dataset for a specific series
const transformedData = seriesNames.map(colName => data.map(row => ({ date: row.date, value: row[colName] })));

const valueline = d3
  .line()
  .x(d => x(d.date))
  .y(d => y(d.value));

let series = g
  .selectAll("path")
  .data(seriesNames)
  .enter()
  .append("path") // create path for each series
  .data(colName => transformedData[colName]) // bind series-specific dataset to path
  .attr("d", valueline) // create line from series-specific dataset
  .attr("stroke", "red")
  .attr("fill", "none");

// Add the X Axis
g.append("g")
  .attr("transform", `translate(0,${height})`)
  .call(d3.axisBottom(x));

// Add the Y Axis
g.append("g").call(d3.axisLeft(y));

1 个答案:

答案 0 :(得分:1)

两件事:首先,将数据数组直接传递给data方法:

let series = g
  .selectAll("path")
  .data(transformedData)
  //etc...

第二,您有一个时间刻度。因此,请使用日期,而不是字符串:

data.forEach(d => d.date = new Date(d.date));

这是您的代码,其中有两个更改:

const data = [{
    date: "Wed, 14 Aug 2019 00:00:00 GMT",
    close: 2.92,
    open: 3.2
  },
  {
    date: "Wed, 28 Aug 2019 00:00:00 GMT",
    close: 2.89,
    open: 2.99
  },
  {
    date: "Fri, 30 Aug 2019 00:00:00 GMT",
    close: 2.88,
    open: 3.4
  },
  {
    date: "Mon, 02 Sep 2019 00:00:00 GMT",
    close: 2.89,
    open: 3.5
  },
];

data.forEach(d => d.date = new Date(d.date));

const margin = {
  top: 20,
  right: 20,
  bottom: 30,
  left: 50
};
const width = 960 - margin.left - margin.right;
const height = 500 - margin.top - margin.bottom;

// set the ranges
const x = d3
  .scaleTime()
  .domain(d3.extent(data, d => d.date))
  .range([0, width]);
const y = d3
  .scaleLinear()
  .domain([0, 4])
  .range([height, 0]);

const g = 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})`);

const seriesNames = ["open", "close"]

// create array where each element is a dataset for a specific series
const transformedData = seriesNames.map(colName => data.map(row => ({
  date: row.date,
  value: row[colName]
})));

const valueline = d3
  .line()
  .x(d => x(d.date))
  .y(d => y(d.value));

let series = g
  .selectAll("path")
  .data(transformedData)
  .enter()
  .append("path") // create path for each series
  .attr("d", valueline) // create line from series-specific dataset
  .attr("stroke", "red")
  .attr("fill", "none");

// Add the X Axis
g.append("g")
  .attr("transform", `translate(0,${height})`)
  .call(d3.axisBottom(x));

// Add the Y Axis
g.append("g").call(d3.axisLeft(y));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>