Angular-ChartJs : 在图形 chartJs 的自定义工具提示中使用服务
Angular-ChartJs : Use a service inside a custom tooltip of a graphic chartJs
我正在尝试在自定义工具提示中使用 angular 服务。
在我的自定义工具提示中,我放了这样的东西:
....
tooltipEl.innerHTML = 'Title ' + this.myService.getTitle() + '';
....
我有这个错误:
错误错误:无法读取未定义的 属性 'getTitle'
那是因为在自定义工具提示中无法识别 THIS。
我的问题是,如何在工具提示中调用 myService?
谢谢。
我在这里准备了一个例子:
https://stackblitz.com/edit/ng2-charts-tooltip-border-ibkso
public pieChartOptions: ChartOptions = {
responsive: true,
tooltips: {
// Disable the on-canvas tooltip
enabled: false,
custom: function(tooltipModel) {
console.log(tooltipModel)
// Tooltip Element
var tooltipEl = document.getElementById('chartjs-tooltip');
// Create element on first render
if (!tooltipEl) {
tooltipEl = document.createElement('div');
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.innerHTML = '<table class="myclass">Title ' + this.myService.getTitle() + '</table>';
document.body.appendChild(tooltipEl);
}
// Hide if no tooltip
if (tooltipModel.opacity === 0) {
tooltipEl.style.opacity = '0';
return;
}
// Set caret Position
tooltipEl.classList.remove('above', 'below', 'no-transform');
if (tooltipModel.yAlign) {
tooltipEl.classList.add(tooltipModel.yAlign);
} else {
tooltipEl.classList.add('no-transform');
}
function getBody(bodyItem) {
return bodyItem.lines;
}
// Set Text
if (tooltipModel.body) {
var titleLines = tooltipModel.title || [];
var bodyLines = tooltipModel.body.map(getBody);
// console.log("tooltipModel", tooltipModel.body)
var innerHtml = '<thead>';
titleLines.forEach(function(title) {
innerHtml += '<tr><th>' + title + ' ToTo</th></tr>';
});
innerHtml += '</thead><tbody>';
// console.log(bodyLines)
bodyLines.forEach(function(body, i) {
var colors = tooltipModel.labelColors[i];
var style = 'background:' + colors.backgroundColor;
style += '; border-color:' + colors.borderColor;
style += '; background-color: red';
style += '; border-width: 10px';
var span = '<span style="' + style + '"></span>';
innerHtml += '<tr><td>' + span + body + '</td></tr>';
console.log(body)
});
innerHtml += '</tbody>';
var tableRoot = tooltipEl.querySelector('table');
tableRoot.innerHTML = innerHtml;
// console.log(tableRoot)
}
// `this` will be the overall tooltip
var position = this._chart.canvas.getBoundingClientRect();
// Display, position, and set styles for font
tooltipEl.style.opacity = '1';
tooltipEl.style.position = 'absolute';
tooltipEl.style.backgroundColor = 'white'
tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
tooltipEl.style.pointerEvents = 'none';
}
}
在函数内部时,this
指的是函数的内部作用域。
要使用 this
在函数范围之外调用引用,您应该使用箭头函数。
tooltips: {
custom: (tooltipModel) => {
// will refer to the myService instance outside of this function
const title = this.myService.getTitle();
}
}
此外,您不应该在使用 Angular 时手动修改 DOM,除非在特殊情况下。这个库不提供通过 API 设置工具提示的方法吗?
我正在尝试在自定义工具提示中使用 angular 服务。
在我的自定义工具提示中,我放了这样的东西:
....
tooltipEl.innerHTML = 'Title ' + this.myService.getTitle() + '';
....
我有这个错误: 错误错误:无法读取未定义的 属性 'getTitle'
那是因为在自定义工具提示中无法识别 THIS。
我的问题是,如何在工具提示中调用 myService?
谢谢。
我在这里准备了一个例子: https://stackblitz.com/edit/ng2-charts-tooltip-border-ibkso
public pieChartOptions: ChartOptions = {
responsive: true,
tooltips: {
// Disable the on-canvas tooltip
enabled: false,
custom: function(tooltipModel) {
console.log(tooltipModel)
// Tooltip Element
var tooltipEl = document.getElementById('chartjs-tooltip');
// Create element on first render
if (!tooltipEl) {
tooltipEl = document.createElement('div');
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.innerHTML = '<table class="myclass">Title ' + this.myService.getTitle() + '</table>';
document.body.appendChild(tooltipEl);
}
// Hide if no tooltip
if (tooltipModel.opacity === 0) {
tooltipEl.style.opacity = '0';
return;
}
// Set caret Position
tooltipEl.classList.remove('above', 'below', 'no-transform');
if (tooltipModel.yAlign) {
tooltipEl.classList.add(tooltipModel.yAlign);
} else {
tooltipEl.classList.add('no-transform');
}
function getBody(bodyItem) {
return bodyItem.lines;
}
// Set Text
if (tooltipModel.body) {
var titleLines = tooltipModel.title || [];
var bodyLines = tooltipModel.body.map(getBody);
// console.log("tooltipModel", tooltipModel.body)
var innerHtml = '<thead>';
titleLines.forEach(function(title) {
innerHtml += '<tr><th>' + title + ' ToTo</th></tr>';
});
innerHtml += '</thead><tbody>';
// console.log(bodyLines)
bodyLines.forEach(function(body, i) {
var colors = tooltipModel.labelColors[i];
var style = 'background:' + colors.backgroundColor;
style += '; border-color:' + colors.borderColor;
style += '; background-color: red';
style += '; border-width: 10px';
var span = '<span style="' + style + '"></span>';
innerHtml += '<tr><td>' + span + body + '</td></tr>';
console.log(body)
});
innerHtml += '</tbody>';
var tableRoot = tooltipEl.querySelector('table');
tableRoot.innerHTML = innerHtml;
// console.log(tableRoot)
}
// `this` will be the overall tooltip
var position = this._chart.canvas.getBoundingClientRect();
// Display, position, and set styles for font
tooltipEl.style.opacity = '1';
tooltipEl.style.position = 'absolute';
tooltipEl.style.backgroundColor = 'white'
tooltipEl.style.left = position.left + window.pageXOffset + tooltipModel.caretX + 'px';
tooltipEl.style.top = position.top + window.pageYOffset + tooltipModel.caretY + 'px';
tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
tooltipEl.style.pointerEvents = 'none';
}
}
在函数内部时,this
指的是函数的内部作用域。
要使用 this
在函数范围之外调用引用,您应该使用箭头函数。
tooltips: {
custom: (tooltipModel) => {
// will refer to the myService instance outside of this function
const title = this.myService.getTitle();
}
}
此外,您不应该在使用 Angular 时手动修改 DOM,除非在特殊情况下。这个库不提供通过 API 设置工具提示的方法吗?