【问题标题】:my d3 line chart looks weird, I can't figure out what's wrong [duplicate]我的 d3 折线图看起来很奇怪,我不知道出了什么问题 [重复]
【发布时间】:2021-02-09 13:03:59
【问题描述】:

我想知道是什么原因造成的,而且我还想在图表中插入另一条线,这样做的正确方法是什么?我知道如何更新数据,但不知道如何制作多行, 任何帮助表示赞赏,谢谢! D3.js 是一个 JavaScript 库,用于在 Web 浏览器中生成动态、交互式数据可视化。它利用可缩放矢量图形、HTML5 和级联样式表标准。它是早期 Protovis 框架的继承者。

const data = [{
    name: "A",
    x: 10,
  },
  {
    name: "B",
    x: 22,
  },
  {
    name: "C",
    x: 33,
  },
  {
    name: "D",
    x: 20,
  },
  {
    name: "E",
    x: 21,
  },
];
//No.1 define the svg
let graphWidth = 600,
  graphHeight = 450;
let margin = {
  top: 30,
  right: 10,
  bottom: 30,
  left: 85
};
let totalWidth = graphWidth + margin.left + margin.right,
  totalHeight = graphHeight + margin.top + margin.bottom;
let svg = d3
  .select("body")
  .append("svg")
  .attr("width", totalWidth)
  .attr("height", totalHeight);
//No.2 define mainGraph
let mainGraph = svg
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//No.3 define axises
let categoriesNames = data.map((d) => d.name);
let xScale = d3
  .scalePoint()
  .domain(categoriesNames)
  .range([0, graphWidth]); // scalepoint make the axis starts with value compared with scaleBand
var yScale = d3
  .scaleLinear()
  .range([graphHeight, 0])
  .domain([0, d3.max(data, (data) => data.x)]); //* If an arrow function is simply returning a single line of code, you can omit the statement brackets and the return keyword

//No.4 set axises
mainGraph
  .append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + graphHeight + ")")
  .call(d3.axisBottom(xScale));
mainGraph.append("g").attr("class", "y axis").call(d3.axisLeft(yScale));
//No.5 make lines
var line = d3
  .line()
  .x(function(d) {
    return xScale(d.name);
  }) // set the x values for the line generator
  .y(function(d) {
    return yScale(d.x);
  }) // set the y values for the line generator
  .curve(d3.curveMonotoneX); // apply smoothing to the line

mainGraph
  .append("path")
  .datum(data) // 10. Binds data to the line
  .attr("class", "line") // Assign a class for styling
  .attr("d", line); // 11. Calls the line generator
<script src="https://d3js.org/d3.v6.min.js"></script>

【问题讨论】:

    标签: d3.js linechart


    【解决方案1】:

    您需要为path 设置fill: none;stroke: <your line colour here>。否则,它会认为它是一个封闭的形状并尝试填充它。

    那是因为通常paths 用于绘制二维形状。只有lines 假定没有二维。另见the MDN docs

    const data = [{
        name: "A",
        x: 10,
      },
      {
        name: "B",
        x: 22,
      },
      {
        name: "C",
        x: 33,
      },
      {
        name: "D",
        x: 20,
      },
      {
        name: "E",
        x: 21,
      },
    ];
    //No.1 define the svg
    let graphWidth = 600,
      graphHeight = 450;
    let margin = {
      top: 30,
      right: 10,
      bottom: 30,
      left: 85
    };
    let totalWidth = graphWidth + margin.left + margin.right,
      totalHeight = graphHeight + margin.top + margin.bottom;
    let svg = d3
      .select("body")
      .append("svg")
      .attr("width", totalWidth)
      .attr("height", totalHeight);
    //No.2 define mainGraph
    let mainGraph = svg
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    //No.3 define axises
    let categoriesNames = data.map((d) => d.name);
    let xScale = d3
      .scalePoint()
      .domain(categoriesNames)
      .range([0, graphWidth]); // scalepoint make the axis starts with value compared with scaleBand
    var yScale = d3
      .scaleLinear()
      .range([graphHeight, 0])
      .domain([0, d3.max(data, (data) => data.x)]); //* If an arrow function is simply returning a single line of code, you can omit the statement brackets and the return keyword
    
    //No.4 set axises
    mainGraph
      .append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + graphHeight + ")")
      .call(d3.axisBottom(xScale));
    mainGraph.append("g").attr("class", "y axis").call(d3.axisLeft(yScale));
    //No.5 make lines
    var line = d3
      .line()
      .x(function(d) {
        return xScale(d.name);
      }) // set the x values for the line generator
      .y(function(d) {
        return yScale(d.x);
      }) // set the y values for the line generator
      .curve(d3.curveMonotoneX); // apply smoothing to the line
    
    mainGraph
      .append("path")
      .datum(data) // 10. Binds data to the line
      .attr("class", "line") // Assign a class for styling
      .attr("d", line); // 11. Calls the line generator
    .line {
      stroke: blue;
      fill: none;
    }
    <script src="https://d3js.org/d3.v6.min.js"></script>

    【讨论】:

    • 非常感谢,我也想通过更新数据插入另一行,我正在检查代码d3-graph-gallery.com/graph/line_several_group.html,但它在v6中不起作用,抛出错误“.nest不是功能”,你知道如何解决这个问题吗?非常感谢!
    • v4和v6有很大区别。请参阅 v5v6 的发行说明,其中包含重大更改。我建议搜索不同的示例,例如 observablehq.com,其中包含更多 v6 内容
    • 再次感谢,我会探索那个网站,干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多