【问题标题】:D3 - Area graph with logarithmic scaleD3 - 对数刻度的面积图
【发布时间】:2022-01-02 05:03:25
【问题描述】:

我正在尝试用 Y 轴以对数刻度制作面积图。下面我使用我在网上找到的一些示例代码重现了我的错误(我的代码有点不同,但本质上是一样的)。


我从基本图开始:

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

//Read the data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv",

  // When reading the csv, I must format variables:
  function(d){
    return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
  },

  // Now I can use this dataset:
  function(data) {

    // Add X axis --> it is a date format
    var x = d3.scaleTime()
      .domain(d3.extent(data, function(d) { return d.date; }))
      .range([ 0, width ]);
    svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

    // Add Y axis
    var y = d3.scaleLinear()
      .domain([1, d3.max(data, function(d) { return +d.value; })])
      .range([ height, 0 ]);
    svg.append("g")
      .call(d3.axisLeft(y));

    // Add the line
    svg.append("path")
      .datum(data)
      .attr("fill", "none")
      .attr("stroke", "steelblue")
      .attr("stroke-width", 1.5)
      .attr("d", d3.line()
        .x(function(d) { return x(d.date) })
        .y(function(d) { return y(d.value) })
        )

})
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

然后我尝试将其更改为具有对数刻度的面积图。每个功能都可以正常工作 - 但结合起来它们会破坏图表:

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

//Read the data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv",

  // When reading the csv, I must format variables:
  function(d){
    return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
  },

  // Now I can use this dataset:
  function(data) {

    // Add X axis --> it is a date format
    var x = d3.scaleTime()
      .domain(d3.extent(data, function(d) { return d.date; }))
      .range([ 0, width ]);
    svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

    // Add Y axis
    var y = d3.scaleLog()
      .domain([1, d3.max(data, function(d) { return +d.value; })])
      .range([ height, 0 ]);
    svg.append("g")
      .call(d3.axisLeft(y));

    // Add the line
    svg.append("path")
      .datum(data)
      .attr("fill", "steelblue")
      .attr("stroke", "steelblue")
      .attr("stroke-width", 1.5)
      .attr("d", d3.area()
        .x(function(d) { return x(d.date) })
        .y0(y(0))
        .y1(function(d) { return y(d.value) })
        )

})
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

我唯一改变的是:

var y = d3.scaleLinear()
// into
var y = d3.scaleLog()

svg.append("path")
    .datum(data)
    .attr("fill", "none")
    .attr("stroke", "steelblue")
    .attr("stroke-width", 1.5)
    .attr("d", d3.line()
        .x(function(d) { return x(d.date) })
        .y(function(d) { return y(d.value) })
    )
// into
svg.append("path")
    .datum(data)
    .attr("fill", "steelblue")
    .attr("stroke", "steelblue")
    .attr("stroke-width", 1.5)
    .attr("d", d3.area()
        .x(function(d) { return x(d.date) })
        .y0(y(0))
        .y1(function(d) { return y(d.value) })
    )

我不明白出了什么问题。

与面积图结合使用时,所有其他类型的比例(linearsqrtpow)都可以正常工作。唯一的问题是log 规模。这实际上是唯一的区别。您可以将scaleLog 更改为scaleSqrt,它就可以工作...

我想在这里指出,我的比例不是从 0 开始的(log(0) 是负无穷大)。所以这不是问题。

【问题讨论】:

  • 什么Andrew said,或者,如果您使用较新的 D3 版本,您可以使用 Symlog 比例保留您的 y(0)var y = d3.scaleSymlog().etc...

标签: javascript html d3.js graph


【解决方案1】:

问题在于您如何设置基线 (y0):

.attr("d", d3.area()
    .x(function(d) { return x(d.date) })
    .y0(y(0))
    .y1(function(d) { return y(d.value) })
)

这里您使用的是 0 和对数刻度 (y(0)),这当然会返回未定义。这就是为什么其他比例不会给您带来麻烦,而对数比例却会给您带来麻烦。您可以为 y 刻度提供一个非常接近零的值,但您知道基线应该在哪里:它应该等于 y 刻度范围内的第一个值,无需使用刻度来确定这一点:

  .y0(height)

如下图:

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

//Read the data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv",

  // When reading the csv, I must format variables:
  function(d){
    return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
  },

  // Now I can use this dataset:
  function(data) {

    // Add X axis --> it is a date format
    var x = d3.scaleTime()
      .domain(d3.extent(data, function(d) { return d.date; }))
      .range([ 0, width ]);
    svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

    // Add Y axis
    var y = d3.scaleLog()
      .domain([1, d3.max(data, function(d) { return +d.value; })])
      .range([ height, 0 ]);
    svg.append("g")
      .call(d3.axisLeft(y));

    // Add the line
    svg.append("path")
      .datum(data)
      .attr("fill", "steelblue")
      .attr("stroke", "steelblue")
      .attr("stroke-width", 1.5)
      .attr("d", d3.area()
        .x(function(d) { return x(d.date) })
        .y0(height)
        .y1(function(d) { return y(d.value) })
        )

})
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

【讨论】:

  • 谢谢,成功了!在这种类型的图表中是否有任何理由使用比例来设置基线(y0)?或者height 在所有情况下都足够了吗?我看到很多教程都这样使用它,我想知道为什么。
  • 如果您希望该区域的底部占据该空间,则使用表示图表底部的范围值可以很好地设置 y0。如果您最终修改了范围,您需要记住更新 y0,但您也可以使用 scale.range()[0] 访问范围的第一个值。除非您希望将基线缩放到不在图表顶部或底部的值,否则没有真正的理由使用比例。
猜你喜欢
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 2014-03-22
相关资源
最近更新 更多