javascript - 无法在角度上自定义 d3 折线图
问题描述
我的应用程序上有一个图表,但我尝试应用的所有自定义样式都没有改变任何东西。另外,如何在悬停时添加工具提示?
这就是我现在所拥有的:
折线图.component.ts
import { Component, ElementRef, Input, OnChanges } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.scss']
})
export class LineChartComponent implements OnChanges {
@Input() public data: { value: number, date: string }[];
private width = 700;
private height = 700;
private margin = 20;
public svg;
public svgInner;
public yScale;
public xScale;
public xAxis;
public yAxis;
public lineGroup;
constructor(public chartElem: ElementRef) {
}
public ngOnChanges(changes): void {
if (changes.hasOwnProperty('data') && this.data) {
console.log(this.data)
this.initializeChart();
this.drawChart();
window.addEventListener('resize', () => this.drawChart());
}
}
private initializeChart(): void {
this.svg = d3
.select(this.chartElem.nativeElement)
.select('.linechart')
.append('svg')
.attr('height', this.height);
this.svgInner = this.svg
.append('g')
.style('transform', 'translate(' + this.margin + 'px, ' + this.margin + 'px)');
this.yScale = d3
.scaleLinear()
.domain([d3.max(this.data, d => d.value) + 1, d3.min(this.data, d => d.value) - 1])
.range([0, this.height - 2 * this.margin]);
this.yAxis = this.svgInner
.append('g')
.attr('id', 'y-axis')
.style('transform', 'translate(' + this.margin + 'px, 0)')
.style('fill', 'none');
this.xScale = d3.scaleTime().domain(d3.extent(this.data, d => new Date(d.date)));
this.xAxis = this.svgInner
.append('g')
.attr('id', 'x-axis')
.style('transform', 'translate(0, ' + (this.height - 2 * this.margin) + 'px)')
.style('fill', 'none');
this.lineGroup = this.svgInner
.append('g')
.append('path')
.attr('id', 'line')
.style('fill', 'none')
.style('stroke', '#ec3a47')
.style('stroke-width', '4px')
}
private drawChart(): void {
this.width = this.chartElem.nativeElement.getBoundingClientRect().width;
this.svg.attr('width', this.width);
this.xScale.range([this.margin, this.width - 2 * this.margin]);
const xAxis = d3
.axisBottom(this.xScale)
.ticks(10)
.tickFormat(d3.timeFormat('%m / %Y'));
this.xAxis.call(xAxis);
const yAxis = d3
.axisLeft(this.yScale);
this.yAxis.call(yAxis);
const line = d3
.line()
.x(d => d[0])
.y(d => d[1])
.curve(d3.curveMonotoneX);
const points: [number, number][] = this.data.map(d => [
this.xScale(new Date(d.date)),
this.yScale(d.value),
]);
this.lineGroup.attr('d', line(points));
}
}
折线图.component.scss
.linechart {
text {
fill: #6a707d;
font-weight: 700;
}
path,
line {
fill: none;
stroke: none;
shape-rendering: crispEdges;
}
#line {
filter: drop-shadow(0px 10px 5px rgba(#ec3a47 ,0.4))
}
}
所以,我想像下图那样设置它的样式,但是我应用的 css 没有效果。
解决方案
推荐阅读
- vb.net - 如何在VS 2012中使拖放对象实时移动,带有边框等
- wordpress - Woocommerce 购物车 - 按类别排序
- google-oauth - 更改已验证应用的应用名称 - Google OAuth 同意屏幕
- javascript - 如何根据所选日期获取图表上的数据(使用日期选择器过滤数据图表 JS)
- javascript - 如何通过部分或 div 创建缩放滚动效果到下一个?
- visual-studio - 如何获取 Visual Studio Community 2019 的 x64 本机工具开发人员命令提示符?
- javascript - 转换对象数组后将它们分成两半:Javascript
- sql-server - 创建不包括视图的 DACPAC
- ios - 在没有 PODS 的 iOS 项目中集成 Firebase Crashlytics (beta) SDK
- javascript - 在制表器中显示行数