【问题标题】:how to draw horizontal line in y axis in d3 column chart?如何在d3柱形图中在y轴上绘制水平线?
【发布时间】:2018-09-01 08:12:22
【问题描述】:

我的 Angular 6 应用程序中有一个 d3 柱形图,我想为 y 轴上的每个标签在 y 轴上画线。我希望线条是这样的:

我不知道怎么画那些浅灰色的线!

这是我的 d3 代码:

createChart() {
const element = this.chartContainer.nativeElement;
this.width = element.offsetWidth - this.margin.left - this.margin.right;
this.height = element.offsetHeight - this.margin.top - this.margin.bottom;
const svg = d3.select(element).append('svg')
  .attr('width', element.offsetWidth)
  .attr('height', element.offsetHeight);

// chart plot area
this.chart = svg.append('g')
  .attr('class', 'bars')
  .attr('transform', `translate(${this.margin.left}, ${this.margin.top})`);

// define X & Y domains
const xDomain = this.data.map(d => d[0]);
const yDomain = [10000, d3.max(this.data, d => d[1])];

// create scales
this.xScale = d3.scaleBand().padding(0.6).domain(xDomain).rangeRound([0, this.width]);
this.yScale = d3.scaleLinear().domain(yDomain).range([this.height, 0]);

// bar colors
this.colors = d3.scaleLinear().domain([0, this.data.length]).range(<any[]>['#00a0e9', '#00a0e9']);

// x & y axis
this.xAxis = svg.append('g')
  .attr('class', 'axis axis-x')
  .attr('transform', `translate(${this.margin.left}, ${this.margin.top + this.height})`)
  .attr("stroke", "#777777")
  .attr("stroke-width", 0.5)
  .attr("fill", "none")
  .call(d3.axisBottom(this.xScale));
this.yAxis = svg.append('g')
  .attr('class', 'axis axis-y')
  .attr('transform', `translate(${this.margin.left}, ${this.margin.top})`)
  .attr("stroke", "#777777")
  .attr("stroke-width", 0.5)
  .attr("fill", "none")
  .call(d3.axisLeft(this.yScale));
}

** 更新 =>

我在 y 轴下方添加了新配置,它工作得有点好,但线条的粗细不相等,另一个问题是我有 120000 和 122000 作为两个最大 y 值,但图表绘制线两个都。它应该只为 120000 绘制

this.yAxis = svg.append('g')
  .attr('class', 'axis axis-y')
  .attr('transform', `translate(${this.margin.left}, ${this.margin.top})`)
  .attr("stroke", "#777777")
  .attr("stroke-width", 0.5)
  .attr("fill", "none")
  .call(d3.axisLeft(this.yScale));

svg.append("g")
  .attr("class", "grid")
  .attr('transform', `translate(${this.margin.left}, ${this.margin.top})`)
  .attr("stroke-width", 0.1)
  .attr("fill", "none")
  .call(d3.axisLeft(this.yScale)
          .tickSize(-this.width)
          .tickFormat("")

  );

【问题讨论】:

    标签: javascript typescript d3.js line angular6


    【解决方案1】:

    我曾经发现过这个sn-p:绘制y轴的第二个版本

      g.append("g")
          .attr("class", "grid")
          .call(d3.axisLeft(y)
                  .tickSize(-width)
                  .tickFormat("")
          );
    

    你需要一点 CSS 来设置线条的样式

    .grid line {stroke: lightgrey;stroke-opacity: 0.7;shape-rendering: crispEdges;}
    .grid path {stroke-width: 0;}
    

    如果你只想要网格线而不想要轴,你可以使用axis.tickSizeInner([size])

    【讨论】:

    • 感谢您的帮助,但我有新问题,我更新了我的帖子。你能帮帮我吗?
    • 122000 线是外刻度线,阅读轴文档以了解如何不绘制外刻度线
    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多