【发布时间】:2020-06-14 14:11:51
【问题描述】:
所以我有 2 个组件,一个组件包含实际网格,另一个包含 D3 图表,我将图表呈现到其中的组件包含下面提到的构建列定义功能,我共享的组件是一个我试图在 Ag-grid 单元格内渲染。
在 Ag-Grid 中渲染自定义组件时,单击here 在控制台上查看错误消息。
Ag Grid Coldef:
// buildColumnDefinition functions returns the attributes needed by the grid
// -------In code i am mapping a model in column definition------------
private buildColumnDefinition(columnModel: ColumnModel): ColDef {
return {
headerName: columnModel.name,
field: columnModel.accessor,
sort: columnModel.sort,
filter: columnModel.filter,
cellRendererFramework: columnModel.componentRenderer
};
自定义单元格渲染器组件:
//Below is the code in the component i am trying to render----//
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'app-bar-chart',
templateUrl: './bar-chart.component.html',
styleUrls: ['./bar-chart.component.scss']
})// Bar chart component is the one rendered in the cell redenderer
export class BarChartComponent implements OnInit {
constructor() { }
ngOnInit(): void {
this.createChart();// Creating chart here
}
randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);//Generates random number for my charts
}
//Function that creates bar charts
createChart() {
const random = this.randomIntFromInterval(10, 100);//Call random number generator
const w = 100;//setting width
const h = 30; // setting height of region
const padding = 1; //setting padding
const dataset = [random]; // each time new random value is passed
const svg = d3.selectAll('#mychart').attr('width', w).attr('height', h); //selecting svg in html to bind the attributes
const nodes = svg.selectAll('.rect').data(dataset)
.enter()
.append('g')
.classed('rect', true);
nodes.append('rect')//appending rect
.attr('x', () => 0)//setting x coordinate
.attr('y', (d, i) => i * (h / dataset.length)) //setting y coordinate
.attr('height', () => 20) //setting height of bar chart
.attr('width', (d) => d + '%') //setting width of bar chart
.attr('fill', () => '#169bd5'); //filling color
nodes.append('rect') //appending 2nd rectangle
.attr('x', (d) => d + '%') //binding data to 'x' axis
.attr('y', (d, i) => i * (h / dataset.length)) //binding the data to y axis
.attr('height', () => 20)//setting the height pf bar chart
.attr('width', (d) => (100 - d) + '%') //setting width of chart
.attr('fill', () => '#FFFFFF'); //filling white color
}
【问题讨论】:
-
提供有关错误的更多详细信息(首先不共享)以及您要达到的目标。
-
能分享一下你要渲染的组件的sn-p代码吗?
-
请点击“在代码中我正在映射列定义中的模型”查看控制台错误的详细信息,不幸的是Stack Overflow不允许更改句子!
-
@Shravan 我已经分享了我要在主组件中渲染的组件。
标签: javascript angular typescript d3.js ag-grid