如何在不影响工具提示标签的情况下更改图例标签?

How can I change the legend label without affecting my tooltip label?

我正在努力寻找解决问题的方法。我已将图例标签更改为 25th - 75th Percentile。到目前为止一切顺利,这是正确的。但是,当我将鼠标悬停在该线上时,问题就出现了。当我在顶部而不是第 25-75 个百分位时,它应该是第 75 个百分位。这是一个我还想不通的问题。我希望你能帮助我。此外,我想为每一行都做这个,以防万一我有第 10 - 90 个百分位。我想将鼠标悬停在两条线上并获得第 90 个百分位或第 10 个百分位。我也附上一张图片enter image description here

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>

<canvas id="mychart2" width="500" height="500"></canvas>
        <script>
        var canvas = document.getElementById("mychart2");
                var ctx = canvas.getContext('2d');
            const decimals = 0;
            var config = {              //configure the chart
                type: 'line',
                data: {
                    labels: ['0', '1', '2', '3','4','5','6','7','8','9+'],
                    datasets: [{
                        label: "25th Percentile",
                        showInLegend: true,
                        backgroundColor: '#c4c1ff',
                        pointBackgroundColor:"#645bff",
                        borderColor: '#645bff',
                        fill: 4,
                        pointRadius:0,
                        pointBorderWidth:3,
                        pointHoverRadius:3,
                        pointHitRadius:3,//no fill here
                        data: [28, 35, 40, 45,50,55,62,66,70,78]
                    },{
                        label: "90th Percentile",
                        backgroundColor: '#c4c1ff',
                        pointBackgroundColor:"#c4c1ff",
                        borderColor: '#c4c1ff',
                        pointHoverBackgroundColor:"#c4c1ff",
                        pointHoverBorderColor: "#c4c1ff",
                        borderWidth:1,
                        pointRadius:0,
                        pointBorderWidth:3,
                        pointHoverRadius:3,
                        pointHitRadius:3,
                        fill: 3, 
                        //no fill here
                        data: [40, 65, 63, 64,72,79,83,87,100,108]
                    },{
                        label: "Median",
                        backgroundColor: '#0d0e25',
                        pointBackgroundColor:"#0d0e25",
                        borderColor: '#0d0e25',
                        borderWidth:1,
                        pointRadius:2,
                        pointBorderWidth:3,
                        pointHoverRadius:3,
                        pointHitRadius:3,
                        fill: false,  //no fill here
                        data: [30, 40, 45, 50, 56, 60, 66,73 ,78,85]},
                    {
                        label: "25th - 75th Percentile",
                        tooltipTitle:"75th Percentile",
                        showInLegend: false,
                        backgroundColor: '#645bff',
                        pointBackgroundColor:"#645bff",
                        borderColor: '#645bff',
                        pointRadius:0,
                        lineTension: 0.1,
                        pointBorderWidth:3,
                        pointHoverRadius:3,
                        pointHitRadius:3,
                        borderCapStyle: 'butt',
                                        borderDash: [],
                                        borderDashOffset: 0.0,
                                        borderJoinStyle: 'miter',
                        fill:0 ,
                        //fill until previous dataset
                        data: [35, 50, 51, 55,63,69,73,80,85,94]
                    },{
                        label: "10th Percentile",
                        backgroundColor: "#c4c1ff",
                        pointBackgroundColor:"#c4c1ff",
                        pointHoverBackgroundColor:"#c4c1ff",
                        pointHoverBorderColor: "#c4c1ff",
                        borderColor: "#c4c1ff",
                        pointStyle:"circle",
                        borderWidth:1,
                        lineWidth:1,
                        hoverRadius:9,
                        pointRadius:0,
                        pointBorderWidth:3,
                        pointHoverRadius:3,
                        pointHitRadius:3,
                        lineTension:0.3,
                       
                        fill: '0', //fill until previous dataset
                        data: [25, 30, 36, 39, 45, 49, 53,56,60,68]
                    }]
                },
                options: {title: {display:true, text: 'Frontend Engineer salaries (753 datapoints)',fontSize:20},
                    maintainAspectRatio: false,
                    legend: {onClick: (e) => e.stopPropagation(),display:true, labels: {filter:function(item,
                    mychart2)
                    {return !item.text.includes("10th - 90th Percentile" & "10th Percentile");}}},
                    spanGaps: false,
                    elements: {
                        line: {
                            tension: 0.000001
                        }
                    },
                    plugins: {
                        filler: {
                            propagate: false
                        }
                    },
                    scales: {
                        yAxes: [{gridLines: {display:false}, scaleLabel: {display: true, labelString: 'Salary', 
                        fontSize:20},
                            ticks: {beginAtZero:true, stepSize: 20,callback: function(value, index, values) {
                        return '$' + value.toFixed(decimals)}
                            }
                        }], xAxes: [{gridLines: {display:false},
                            ticks: {beginAtZero:true, stepSize: 20,
                                
                            }, scaleLabel: {
                     display: true,
                     labelString: 'Years of relevant experience',
                     fontSize: 20 }
                        }]
                    }
                }
            };
            var chart = new Chart(ctx, config);
        </script>

tooltipTitle 不是 chart.js 中的 属性。要调整标题,您必须像这样实现自定义回调:

options: {
      tooltips: {
        callbacks: {
          label: function(tooltipItem, data) {
            var label = data.datasets[tooltipItem.datasetIndex].label || '';
            if (label) {
              label += ': ';
            }
            if (label === "25th - 75th Percentile: ") {
              label = "75th Percentile: "
            }
            if (label === "10th - 90th Percentile: ") {
              label = "90th Percentile: "
            }
            label += tooltipItem.yLabel
            return label;
          }
        }
      }
}

示例:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>

<canvas id="mychart2" width="500" height="500"></canvas>
<script>
  var canvas = document.getElementById("mychart2");
  var ctx = canvas.getContext('2d');
  const decimals = 0;
  var config = { //configure the chart
    type: 'line',
    data: {
      labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9+'],
      datasets: [{
          label: "25th Percentile",
          showInLegend: true,
          backgroundColor: '#c4c1ff',
          pointBackgroundColor: "#645bff",
          borderColor: '#645bff',
          fill: 4,
          pointRadius: 0,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3, //no fill here
          data: [28, 35, 40, 45, 50, 55, 62, 66, 70, 78]
        }, {
          label: "90th Percentile",
          backgroundColor: '#c4c1ff',
          pointBackgroundColor: "#c4c1ff",
          borderColor: '#c4c1ff',
          pointHoverBackgroundColor: "#c4c1ff",
          pointHoverBorderColor: "#c4c1ff",
          borderWidth: 1,
          pointRadius: 0,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          fill: 3,
          //no fill here
          data: [40, 65, 63, 64, 72, 79, 83, 87, 100, 108]
        }, {
          label: "Median",
          backgroundColor: '#0d0e25',
          pointBackgroundColor: "#0d0e25",
          borderColor: '#0d0e25',
          borderWidth: 1,
          pointRadius: 2,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          fill: false, //no fill here
          data: [30, 40, 45, 50, 56, 60, 66, 73, 78, 85]
        },
        {
          label: "25th - 75th Percentile",
          showInLegend: false,
          backgroundColor: '#645bff',
          pointBackgroundColor: "#645bff",
          borderColor: '#645bff',
          pointRadius: 0,
          lineTension: 0.1,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          borderCapStyle: 'butt',
          borderDash: [],
          borderDashOffset: 0.0,
          borderJoinStyle: 'miter',
          fill: 0,
          //fill until previous dataset
          data: [35, 50, 51, 55, 63, 69, 73, 80, 85, 94]
        }, {
          label: "10th Percentile",
          backgroundColor: "#c4c1ff",
          pointBackgroundColor: "#c4c1ff",
          pointHoverBackgroundColor: "#c4c1ff",
          pointHoverBorderColor: "#c4c1ff",
          borderColor: "#c4c1ff",
          pointStyle: "circle",
          borderWidth: 1,
          lineWidth: 1,
          hoverRadius: 9,
          pointRadius: 0,
          pointBorderWidth: 3,
          pointHoverRadius: 3,
          pointHitRadius: 3,
          lineTension: 0.3,

          fill: '0', //fill until previous dataset
          data: [25, 30, 36, 39, 45, 49, 53, 56, 60, 68]
        }
      ]
    },
    options: {
      tooltips: {
        callbacks: {
          label: function(tooltipItem, data) {
            var label = data.datasets[tooltipItem.datasetIndex].label || '';
            if (label) {
              label += ': ';
            }
            if (label === "25th - 75th Percentile: ") {
              label = "75th Percentile: "
            }
            label += tooltipItem.yLabel
            return label;
          }
        }
      },
      title: {
        display: true,
        text: 'Frontend Engineer salaries (753 datapoints)',
        fontSize: 20
      },
      maintainAspectRatio: false,
      legend: {
        onClick: (e) => e.stopPropagation(),
        display: true,
        labels: {
          filter: function(item,
            mychart2) {
            return !item.text.includes("10th - 90th Percentile" & "10th Percentile");
          }
        }
      },
      spanGaps: false,
      elements: {
        line: {
          tension: 0.000001
        }
      },
      plugins: {
        filler: {
          propagate: false
        }
      },
      scales: {
        yAxes: [{
          gridLines: {
            display: false
          },
          scaleLabel: {
            display: true,
            labelString: 'Salary',
            fontSize: 20
          },
          ticks: {
            beginAtZero: true,
            stepSize: 20,
            callback: function(value, index, values) {
              return '$' + value.toFixed(decimals)
            }
          }
        }],
        xAxes: [{
          gridLines: {
            display: false
          },
          ticks: {
            beginAtZero: true,
            stepSize: 20,

          },
          scaleLabel: {
            display: true,
            labelString: 'Years of relevant experience',
            fontSize: 20
          }
        }]
      }
    }
  };
  var chart = new Chart(ctx, config);
</script>