D3折线图-在Y轴刻度上显示值标签,且不按比例显示

时间:2019-02-06 18:18:37

标签: javascript d3.js

我想在Y轴上打印标签,而不是刻度刻度范围。这是我的数据集,我希望在其中在Y轴上打印data.values [i] .label。

var data = [
  {
    name: "USA",
    values: [
      {date: "2000", price: "500", label: "High"},
      {date: "2001", price: "400", label: "High"},
      {date: "2002", price: "300", label: "Medium"},
      {date: "2003", price: "200", label: "Medium"},
      {date: "2004", price: "100", label: "Low"},
      {date: "2005", price: "200", label: "Medium"}
    ]
  }
];

我认为我需要在第157行做一些事情,就像回答here一样,但是我需要传入value [i],而不仅仅是价格。像

var yAxis = d3.axisLeft(yScale).tickFormat( function(d) { return d.label } );

一些指导将不胜感激。 我已经在Codepen here

上对其进行了测试

1 个答案:

答案 0 :(得分:0)

希望对您有帮助

var yAxis = d3.axisLeft(yScale).ticks(5).tickFormat( function(d, i) { return data[0].values[i].label;  } );
相关问题