简体   繁体   English

如何在chart.js的图例中设置标题?

[英]how to set title in legend of chart.js?

I am using the angular -chart with chart.js and wanted to apply the title caption When mouse over the line. 我正在使用angular -chart with chart.js并想要应用标题标题当鼠标悬停在线上时。 I want to put the title in the caption When mouse.I could do this using the labels , but appears below graphic names and I do not want it , how to remove the names down the chart and leave only the title of the legend? 我想把标题放在标题中当鼠标。我可以使用标签来做到这一点,但是出现在图形名称下方,我不想要它,如何删除图表中的名称并只保留图例的标题?

 $scope.labels = ["January", "February", "March", "April", "May",    "June", "July"];
   $scope.series = ['Series A', 'Series B'];
    $scope.data = [
  [65, 59, 80, 81, 56, 55, 40],
   [28, 48, 40, 19, 86, 27, 90]
 ];
 $scope.onClick = function (points, evt) {
console.log(points, evt);
 };
 });

I want to remove these texts below the graph and leave only the titles on the legend. 我想删除图表下方的这些文本,只留下图例上的标题。 See Image. 见图。 it's possible? 这是可能的?

在此输入图像描述

Reference: http://jtblin.github.io/angular-chart.js/ 参考: http//jtblin.github.io/angular-chart.js/

JsFidle : http://jsfiddle.net/Lfmhcab3/4/ JsFidlehttp//jsfiddle.net/Lfmhcab3/4/

Preview 预习

在此输入图像描述


Just add the following bit of script right after your chart.js include. 只需在chart.js包含后添加以下位脚本即可。 Explanation inline. 内联说明。

var originalLineInitialize = Chart.types.Line.prototype.initialize;
Chart.types.Line.prototype.initialize = function(data) {
    // we don't want the labels to separate the x axis from the bottom edge of the graph
    var originalLabels = data.labels;
    data.labels = data.labels.map(function() { return '' });

    originalLineInitialize.apply(this, arguments);

    // restore the original labels - because tooltips pick it up from these properties
    var xLabels = this.scale.xLabels;
    for (var i = 0; i < xLabels.length; i++)
        xLabels[i] = originalLabels[i];
    this.datasets.forEach(function(dataset) {
      dataset.points.forEach(function(point, i) {
        point.label = originalLabels[i];
      });
    });

    // override the scale drawing to set the labels to null before drawing 
    // and back to their original values after the scale is drawn
    var originalScaleDraw = this.scale.draw;
    this.scale.draw = function() {
      // we construct the originalLabels instead of using the previous one
      // this allows us to take care of any label updates (eg. in the angular directive)
      for (var i = 0; i < this.xLabels.length; i++) {
        originalLabels[i] = this.xLabels[i];
        this.xLabels[i] = '';
      }
        originalScaleDraw.apply(this, arguments);
      for (var i = 0; i < this.xLabels.length; i++)
        this.xLabels[i] = originalLabels[i];
    }
}

Updated fiddle - http://jsfiddle.net/pdodg04L/ 更新小提琴 - http://jsfiddle.net/pdodg04L/

If I understand your problem correctly, you want to keep the same tooltip that in your JSFiddle, you just want to hide the labels at the bottom of the graph. 如果我正确理解您的问题,您希望保留在JSFiddle中的相同工具提示,您只想隐藏图表底部的标签。 The sad thing is that it is not going to be trivial because the labels displayed at the bottom are linked with the label you can see in the Tooltip. 可悲的是,它不会是微不足道的,因为底部显示的标签与您可以在工具提示中看到的标签相关联。

To remove the labels in chart.js , you can do $scope.labels = ["", "", "", "", "", "", ""]; 删除chart.js中的标签 ,您可以执行$scope.labels = ["", "", "", "", "", "", ""]; but it will result in the loss of the title in the tooltip. 但它会导致工具提示中的标题丢失。

As a workaround, you have 2 solutions : 作为解决方法,您有2个解决方案:

#1: quick but dirty #1:快但脏

Modify the source code of Chart.js to hide the labels at the bottom of the graph as suggested by @MichaelG in his answer . 修改Chart.js的源代码,以隐藏@MichaelG在其答案中建议的图表底部的标签。 Only a few lines of code but you will loose the labels of all your charts importing this Chart.js file, and it will be a mess to upgrade your Chart.js version in the future. 只有几行代码,但是你会丢失导入这个Chart.js文件的所有图表的标签,将来升级你的Chart.js版本会很麻烦。

#2: better but harder #2:更好但更难

As explained in this answer from @potatopeelings, you can override the function displaying the tooltips. 如@potatopeelings的回答中所述,您可以覆盖显示工具提示的功能。 So you can keep the $scope.labels = ["", "", "", "", "", "", ""]; 所以你可以保留$scope.labels = ["", "", "", "", "", "", ""]; thing to hide the labels at the bottom of the graph, and then override the tooltip function to display the title you want like this : 要隐藏图形底部的标签,然后覆盖工具提示功能以显示您想要的标题:

$scope.options = {
   // ...
   customTooltips: function(tooltip) {
       // My code to display 'January' / 'February' as a title according to the tooltip param
   }
}

PS : if you try to use the customTooltips option, consider upgrading your Chart.js version to the latest because the old version in your JSFiddle does not support it. PS:如果您尝试使用customTooltips选项,请考虑将Chart.js版本升级到最新版本,因为JSFiddle中的旧版本不支持它。

Create a div with id called legend 创建一个名为legend的id div

<div id="legend"></div>

The add the following code. 添加以下代码。 Here, data is what you send to draw the chart. 在这里,数据是您发送绘制图表的内容。

legend(document.getElementById('legend'), data);

function legend(parent, data) {
    parent.className = 'legend';
    var datas = data.hasOwnProperty('datasets') ? data.datasets : data;

    // remove possible children of the parent
    while(parent.hasChildNodes()) {
        parent.removeChild(parent.lastChild);
    }

    datas.forEach(function(d) {
        var title = document.createElement('span');
        title.className = 'title';
        parent.appendChild(title);

        var colorSample = document.createElement('div');
        colorSample.className = 'color-sample';
        colorSample.style.backgroundColor = d.hasOwnProperty('strokeColor') ? d.strokeColor : d.color;
        colorSample.style.borderColor = d.hasOwnProperty('fillColor') ? d.fillColor : d.color;
        title.appendChild(colorSample);

        var text = document.createTextNode(d.label);
        title.appendChild(text);
    });
}

This is my nasty answer. 这是我讨厌的答案。

Just create an outer div for the canvas and reduce its height a little so that the labels are invisible. 只需为画布创建一个外部div并稍微降低其高度,以使标签不可见。 also set overflow:hidden to the outer div. 还设置overflow:hidden到外部div。 then detach the legends from the inner div and append it to the outer div. 然后从内部div分离传说并将其附加到外部div。

Here's a working fiddle 这是一个工作小提琴

The HTML HTML

<div class='chart-cont'>
<canvas tc-chartjs-line chart-options="options" chart-data="data" auto-legend ng-click="chartClick($event)" chart="chart"></canvas>

</div>

</div>

Css CSS

canvas{
  height:424px;
}
.chart-cont{
  height:410px;
  overflow:hidden;

}

and the javascript 和JavaScript

onAnimationComplete: function(){ $(".tc-chart-js-legend").detach().appendTo(".outer")},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM