Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在chartjs和angular中在自定义工具提示和普通工具提示之间切换_Javascript_Angular_Chart.js - Fatal编程技术网

Javascript 如何在chartjs和angular中在自定义工具提示和普通工具提示之间切换

Javascript 如何在chartjs和angular中在自定义工具提示和普通工具提示之间切换,javascript,angular,chart.js,Javascript,Angular,Chart.js,我必须根据某些条件显示工具提示 options: { tooltips: tooltipCondition=== true ? { mode: 'index', position: 'nearest' } : {enabled: false, custom: function (tooltipModel) { // code } } } 第一次显示自定义工具提示是因为“tooltipCondition”值为false,在将to

我必须根据某些条件显示工具提示

    options: {
        tooltips: tooltipCondition=== true ? 
       { mode: 'index', position: 'nearest' } : 
       {enabled: false, custom: function (tooltipModel) { // code } } 
     }

第一次显示自定义工具提示是因为“tooltipCondition”值为false,在将
tooltipCondition
值更改为true后,tooltip仍然显示自定义工具提示,而它应该显示默认的
chartjs
tooltip

在更改现有图表上的
tooltip
选项后,必须调用

例如,如果允许用户在默认和自定义工具提示模式之间切换,则可以定义
onclick
事件处理程序,如下所示

<input type="checkbox" onclick='enableCustomTooltip(this.checked)'> Custom Tooltip
...
const enableCustomTooltip = function(enabled) {
  if (enabled) {    
    chart.options.tooltips = { enabled: false, custom: (tooltipModel) => customTooltip(tooltipModel) };
  } else {
    chart.options.tooltips = { enabled: true };
  }
  chart.update();
};
自定义工具提示
...
const enableCustomTooltip=函数(已启用){
如果(启用){
chart.options.tooltips={enabled:false,custom:(tooltipModel)=>customTooltip(tooltipModel)};
}否则{
chart.options.tooltips={enabled:true};
}
chart.update();
};
请根据中的代码查看以下图表,并了解其工作原理

const customTooltip=函数(tooltipModel){
var tooltipEl=document.getElementById('chartjs-tooltip');
如果(!工具管){
tooltipEl=document.createElement('div');
tooltipEl.id='chartjs tooltip';
tooltipEl.innerHTML='';
document.body.appendChild(工具管道);
}
如果(ToolTimpodel.opacity==0){
tooltipEl.style.opacity=0;
返回;
}
移除('Upper'、'Down'、'no transform');
如果(ToolTimpodel.yAlign){
添加(tooltipModel.yAlign);
}否则{
tooltipEl.classList.add('no-transform');
}
函数getBody(bodyItem){
返回bodyItem.line;
}
如果(ToolTimpodel.body){
var titleLines=tooltipModel.title | |[];
var bodyLines=tooltipModel.body.map(getBody);
var innerHtml='';
标题行。forEach(函数(标题){
innerHtml+=''+标题+'';
});
innerHtml+='';
bodyLines.forEach(函数(body,i){
var colors=tooltipModel.labelColors[i];
var style='background:'+colors.backgroundColor;
样式+=';边框颜色:'+colors.borderColor;
样式+=';边框宽度:2px';
var span='';
innerHtml+=''+span+body+'';
});
innerHtml+='';
var tableRoot=tooltipEl.querySelector('table');
tableRoot.innerHTML=innerHTML;
}
var position=chart.canvas.getBoundingClientRect();
tooltipEl.style.opacity=1;
tooltipEl.style.position='absolute';
tooltipEl.style.left=position.left+window.pageXOffset+tooltipModel.caretX+'px';
tooltipEl.style.top=position.top+window.pageYOffset+tooltipModel.caretY+'px';
tooltipEl.style.fontFamily=ToolTimpodel.\u bodyFontFamily;
tooltipEl.style.fontSize=tooltipModel.bodyFontSize+'px';
tooltipEl.style.fontStyle=tooltipModel.\u bodyFontStyle;
tooltipEl.style.padding=tooltipModel.yPadding+'px'+tooltipModel.xPadding+'px';
tooltipEl.style.pointerEvents='none';
};
const enableCustomTooltip=函数(已启用){
如果(启用){
chart.options.tooltips={enabled:false,custom:(tooltipModel)=>customTooltip(tooltipModel)};
}否则{
chart.options.tooltips={enabled:true};
}
chart.update();
};
var图表=新图表(“myChart”{
键入“pie”,
数据:{
标签:[“一月”、“二月”、“三月”],
数据集:[{
数据:[504453365515900],
背景颜色:['#FF6384','#36A2EB','#FFCE56']
}]
},
选项:{
}
});

自定义工具提示

更改现有图表上的
工具提示
选项后,必须调用

例如,如果允许用户在默认和自定义工具提示模式之间切换,则可以定义
onclick
事件处理程序,如下所示

<input type="checkbox" onclick='enableCustomTooltip(this.checked)'> Custom Tooltip
...
const enableCustomTooltip = function(enabled) {
  if (enabled) {    
    chart.options.tooltips = { enabled: false, custom: (tooltipModel) => customTooltip(tooltipModel) };
  } else {
    chart.options.tooltips = { enabled: true };
  }
  chart.update();
};
自定义工具提示
...
const enableCustomTooltip=函数(已启用){
如果(启用){
chart.options.tooltips={enabled:false,custom:(tooltipModel)=>customTooltip(tooltipModel)};
}否则{
chart.options.tooltips={enabled:true};
}
chart.update();
};
请根据中的代码查看以下图表,并了解其工作原理

const customTooltip=函数(tooltipModel){
var tooltipEl=document.getElementById('chartjs-tooltip');
如果(!工具管){
tooltipEl=document.createElement('div');
tooltipEl.id='chartjs tooltip';
tooltipEl.innerHTML='';
document.body.appendChild(工具管道);
}
如果(ToolTimpodel.opacity==0){
tooltipEl.style.opacity=0;
返回;
}
移除('Upper'、'Down'、'no transform');
如果(ToolTimpodel.yAlign){
添加(tooltipModel.yAlign);
}否则{
tooltipEl.classList.add('no-transform');
}
函数getBody(bodyItem){
返回bodyItem.line;
}
如果(ToolTimpodel.body){
var titleLines=tooltipModel.title | |[];
var bodyLines=tooltipModel.body.map(getBody);
var innerHtml='';
标题行。forEach(函数(标题){
innerHtml+=''+标题+'';
});
innerHtml+='';
bodyLines.forEach(函数(body,i){
var colors=tooltipModel.labelColors[i];
var style='background:'+colors.backgroundColor;
样式+=';边框颜色:'+colors.borderColor;
样式+=';边框宽度:2px';
var span='';
innerHtml+=''+span+body+'';
});
innerHtml+='';
var tableRoot=tooltipEl.querySelector('table');
tableRoot.innerHTML=innerHTML;
}
var position=chart.canvas.getBoundingClientRect();
tooltipEl.style.opacity=1;
tooltipEl.style.position='absolute';
tooltipEl.style.left=position.left+window.pageXOffset+tooltipModel.caretX+'px';
tooltipEl.style.top=position.top+window.pageYOffset+tooltipModel.caretY+'px';
tooltipEl.style.fontFamily=ToolTimpodel.\u bodyFontFamily;
tooltipEl.style.fontSize=tooltipModel.bodyFontSize+'px';
tooltipEl.style.fontStyle=tooltipModel.\u bodyFontStyle;
也