Chart.js 如果标签为falsy,则ChartJ不会呈现任何图例

Chart.js 如果标签为falsy,则ChartJ不会呈现任何图例,chart.js,Chart.js,我正在使用ChartJS2.4,我想在数据集中预先定义很多设置。但使用空数据数组和未定义的标签。现在我想阻止ChartJS呈现图例,该图例随后显示为“null”或“undefined”。稍后,我将设置一个适当的标签,并开始将数据推送到数据集,最后在图表上调用update(),这很好 以下是此代码的基本配置: var data = { labels: ["January", "February", "March", "April", "May", "June", "July"],

我正在使用ChartJS2.4,我想在数据集中预先定义很多设置。但使用空数据数组和未定义的标签。现在我想阻止ChartJS呈现图例,该图例随后显示为“null”或“undefined”。稍后,我将设置一个适当的标签,并开始将数据推送到数据集,最后在图表上调用update(),这很好

以下是此代码的基本配置:

var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            fill: false,
            lineTension: 0.1,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 5,
            pointHitRadius: 10,
            data: [],
        }
    ]
};

var myLineChart = new Chart("myChart", {
  type: "line",
    data: data
});


setTimeout(function(){
  data.datasets[0].label = "The Label"
  data.datasets[0].data.push(10, 20)
    myLineChart.update()
}, 2000)
我曾试图覆盖GenerateLabel,但没有成功。设置dateaset.hidden仅在为真时才会在图例中出现,而不是真正的“隐藏”


编辑1:另一方面,将
选项.legend.display设置为false将隐藏所有图例,这也永远不是我所需要的。让我们创建一个插件,在每次更新开始时,根据是否(分别)定义了第一个数据集的标签来决定是否显示图例。如果设置为
true
,则
autoDisplayLegend
图表选项将启用插件

一把小提琴正在工作。下面也提供了代码

var autoDisplayLegendPlugin={
//在更新开始时调用。
更新前:函数(chartInstance){
if(chartInstance.options.autoDisplayLegend){
//第一个(假设是唯一的)数据集。
var dataset=chartInstance.data.datasets[0];
if(dataset.label)
chartInstance.options.legend.display=true;
其他的
chartInstance.options.legend.display=false;
}
}
};
Chart.pluginService.register(autoDisplayLegendPlugin);
风险值数据={
标签:[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”],
数据集:[{
填充:假,
线张力:0.1,
背景颜色:“rgba(75192192,0.4)”,
边框颜色:“rgba(75192192,1)”,
borderCapStyle:“butt”,
borderDash:[],
borderDashOffset:0.0,
borderJoinStyle:'斜接',
pointBorderColor:“rgba(75192192,1)”,
pointBackgroundColor:#fff“,
点边界宽度:1,
点半径:5,
pointHoverBackgroundColor:“rgba(75192192,1)”,
pointHoverBorderColor:“rgba(2201)”,
pointHoverBorderWidth:2,
点半径:5,
点半径:10,
数据:[],
}]
};
var myLineChart=新图表(“myChart”{
键入:“行”,
数据:数据,
选项:{
//用于自动显示图例的选项。
autoDisplayLegend:真
}
});
setTimeout(函数(){
data.dataset[0]。label=“标签”
data.dataset[0].data.push(10,20)
myLineChart.update()
},2000)

如果需要,可以删除数据集,以便图例不会显示:

var autoDisplayLegendPlugin = {

beforeUpdate: function(chartInstance) {

if (chartInstance.canvas.id == 'chart6') {
  // The first (and, assuming, only) dataset.
  var dataset = chartInstance.data.datasets[2];
  if (dataset.data.length != 0){ // delete only if there is no data in dataset
    console.log(chartInstance);
    chartInstance.options.legend.display = true;

  }
  else{
    chartInstance.options.legend.display = true;
    chartInstance.data.datasets.pop(); // if is the last datasets you don't want the legend to appear
  }
}
}};

但这会永远隐藏所有的图例,而不仅仅是标签为空。因此,当且仅当定义了数据集的标签时,您需要一种完全自动的方式来显示图例?准确地说,或者说尽可能自动地选择一种基于插件的方法。这很有趣。但这只在我只有一个数据集的情况下有效,对吗?我想提前设计很多东西,我们能不能用插件的方式把每一个传说都挂起来?