【发布时间】: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