Kan*_*vel 16 javascript chart.js
我正在尝试使用Chart.Js构建图表.这个chart.js有工具提示的默认选项,我想制作自定义的工具提示选项.有没有办法让它成为可能?
这是我的代码
var chart = null;
barChart: function (data1, data2, data3, label) {
var data = {
labels: label,
datasets: [
{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: data1
},
{
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: data2
},
{
fillColor: "rgba(0,255,0,0.3)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(0,255,0,0.3)",
data: data3
},
]
}
var cht = document.getElementById('exampleCanvas');
var ctx = cht.getContext('2d');
if (chart)
chart.destroy();
chart = new Chart(ctx).Bar(data);
}
Run Code Online (Sandbox Code Playgroud)
试试这个:
您可以使用以下代码进行全局更改:
Chart.defaults.global = {
// Boolean - Determines whether to draw tooltips on the canvas or not
showTooltips: true,
// Array - Array of string names to attach tooltip events
tooltipEvents: ["mousemove", "touchstart", "touchmove"],
// String - Tooltip background colour
tooltipFillColor: "rgba(0,0,0,0.8)",
// String - Tooltip label font declaration for the scale label
tooltipFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
// Number - Tooltip label font size in pixels
tooltipFontSize: 14,
// String - Tooltip font weight style
tooltipFontStyle: "normal",
// String - Tooltip label font colour
tooltipFontColor: "#fff",
// String - Tooltip title font declaration for the scale label
tooltipTitleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
// Number - Tooltip title font size in pixels
tooltipTitleFontSize: 14,
// String - Tooltip title font weight style
tooltipTitleFontStyle: "bold",
// String - Tooltip title font colour
tooltipTitleFontColor: "#fff",
// Number - pixel width of padding around tooltip text
tooltipYPadding: 6,
// Number - pixel width of padding around tooltip text
tooltipXPadding: 6,
// Number - Size of the caret on the tooltip
tooltipCaretSize: 8,
// Number - Pixel radius of the tooltip border
tooltipCornerRadius: 6,
// Number - Pixel offset from point x to tooltip edge
tooltipXOffset: 10,
// String - Template string for single tooltips
tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>",
// String - Template string for single tooltips
multiTooltipTemplate: "<%= value %>",
// Function - Will fire on animation progression.
onAnimationProgress: function(){},
// Function - Will fire on animation completion.
onAnimationComplete: function(){}
}
Run Code Online (Sandbox Code Playgroud)
使用此链接作为参考
新版本的 chart.js,版本 2,可以在这里找到:
https://github.com/nnnick/Chart.js/releases
版本 2 添加tooltip callbacks:
每个工具提示回调(beforeTitle、title、afterTitle 等)都接受一个字符串或一个数组。如果使用数组,它将产生多行。工具提示现在也提供了更多的视觉定制选项。
但是,有一个名为 chartNew.js 的 chart.js 分支,可在此处找到:
https://github.com/FVANCOP/ChartNew.js/
它为古老的 chart.js 添加了几项重要的增强功能,包括:
工具提示功能(下载/解压缩时,查看Samples文件夹并查看annotateFunction.html。将鼠标悬停在任何点上时,您可以执行任何操作。)
将一组颜色传递给条形图(而不是每个条形都具有相同的颜色)
将文本放在图表上您想要的任何位置
许多等等。
请注意,chart.js 在版本 2 中得到了极大的增强,但新版本并不完全向后兼容(只是更改为 v2 插件破坏了我现有的代码),而 chartNew.js 将在扩展功能的同时使用旧代码。
小智 5
我已经使用过这个,我在stackoverflow上发现它,但我很难再找到它
<div id="chartjs-tooltip"></div>
Run Code Online (Sandbox Code Playgroud)
var chartoptions =
{
customTooltips: function ( tooltip )
{
var tooltipEl = $( "#chartjs-tooltip" );
if ( !tooltip )
{
tooltipEl.css( {
opacity: 0
} );
return;
}
tooltipEl.removeClass( 'above below' );
tooltipEl.addClass( tooltip.yAlign );
// split out the label and value and make your own tooltip here
var parts = tooltip.text.split( ":" );
var innerHtml = '<span>' + parts[0].trim() + '</span> : <span><b>' + parts[1].trim() + '</b></span>';
tooltipEl.html( innerHtml );
tooltipEl.css( {
opacity: 1,
left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px',
fontFamily: tooltip.fontFamily,
fontSize: tooltip.fontSize,
fontStyle: tooltip.fontStyle
} );
}
}
Run Code Online (Sandbox Code Playgroud)
链接到我得到它:Chart.js:更改工具提示模板
| 归档时间: |
|
| 查看次数: |
74486 次 |
| 最近记录: |