如何修改Chart.js工具提示以添加自定义属性 [英] How to modify chartjs tooltip to add customized attribute

查看:178
本文介绍了如何修改Chart.js工具提示以添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编辑工具提示模板,以便可以从json文件添加自定义属性?在示例下方:








脚本

  function Label(short,long){
this.short = short;
this.long = long
}
Label.prototype.toString = function(){
返回this.short;
}

var data = {
标签:[
新Label( J, JAN),
新Label( F , FEB),
个新标签( M, MAR),
个新标签( A, APR),
个新标签( M, MAY),
新标签( J, JUN),
新标签( J, JUL)
],
数据集:[
{
标签:我的第一个数据集,
fillColor: rgba(220,220,220,0.5),
strokeColor: rgba(220,220,220,0.8),
HighlightFill : rgba(220,220,220,0.75),
高亮描边: rgba(220,220,220,1),
数据:[65、59、80、81、56、55、40]
}
]
};

//创建图表
var ctx = document.getElementById( chart)。getContext(’2d’);
new Chart(ctx).Bar(data,{
tooltipTemplate:<%if(label){%><%= label.long%> ;:<%}%> ;<%= value%>,
});






提琴手- https://jsfiddle.net/7z1s1feg/


How I have to edit the tooltip template that i can add customized attributes from my json file ? Below the example:

ChartJS Example

In the x-axis are the Months with the first letter, but in the tooltip i want to show the first three letters of the month - How can i do this ?

EDIT: My JSON File:

{"modules":[{
"name":"Chart 1",
"link":"www.google.com",
"type":"Bar",
"series":"SeriesA",
"data":[[20,40,50,40,20,20,20,20,20,20,20,20]],
"labels":["M","J","J","A","S","O","N","D","J","F","M","A","M"],
"colors":[{
    "fillColor":"blue"
    }],
"options":{
    "scaleShowGridLines":false
    }

    }]}

解决方案

Different Labels in Tooltip vs Scale

Just use the tooltipTemplate option

Preview


Script

function Label(short, long) {
  this.short = short;
  this.long = long
}
Label.prototype.toString = function() {
  return this.short;
}

var data = {
    labels: [ 
      new Label("J", "JAN"), 
      new Label("F", "FEB"), 
      new Label("M", "MAR"),
      new Label("A", "APR"),
      new Label("M", "MAY"),
      new Label("J", "JUN"),
      new Label("J", "JUL")
    ],
    datasets: [
        {
            label: "My First dataset",
            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: [65, 59, 80, 81, 56, 55, 40]
        }
    ]
};

// create chart
var ctx = document.getElementById("chart").getContext('2d');
new Chart(ctx).Bar(data, {
  tooltipTemplate: "<%if (label){%><%=label.long%>: <%}%><%= value %>",
});


Fiddle - https://jsfiddle.net/7z1s1feg/

这篇关于如何修改Chart.js工具提示以添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆