图表JS线图multitooltipkey背景颜色问题

时间:2016-10-17 19:28:54

标签: javascript canvas chart.js

我对Chart.js有疑问。我使用的是带有2组数据(2行)的折线图,当我将鼠标悬停在数据上时,它会显示"图例"。在图例中有2个可以着色的盒子。我可以毫无问题地改变盒子的背景颜色,但是当我改变颜色时,它适用于两个盒子。如何单独设置盒子的颜色?感谢。

    <canvas
    class="ex-line-graph"
    width="1200" height="1200"
    data-chart="line"
    data-scale-start-value="0"
    data-scale-step-width ="100"
    data-scale-steps ="4"
    data-point-fill-color = "RGBA( 255,28,221,.3)"
    data-scale-line-color="transparent"
    data-scale-grid-line-color="rgba(255,255,255,.05)"
    data-scale-font-color="#a2a2a2"
    data-labels="         ['April','May','June','July','August','September','October','November','December        ','January','February','March']"
    data-value="[{ fillColor: 'RGBA( 28,168,221,.3)', strokeColor:      
    '#1CA8DD', label:'Data', data: [151, 154, 173, 169, 176,161,0,0,0,0,0,0]}, {fillColor: 'RGBA(37,40,48,.0)', strokeColor: 'rgba(255,255,255,1)', label :'Target', data: [200, 200,200,200,200, 200,200,200,200, 200,200,200] }]">
  </canvas>

1 个答案:

答案 0 :(得分:1)

如果我正确遵循,这是一个2部分答案:

1.-在数据集上指定颜色:

var data = {
  labels: ["January", "February", "March", "April", "May"],
  datasets: [{
    label: "Series A",
    data: [10, 30, 20, 40, 10],
    borderColor: "rgba(0,0,255,0.8)",
    backgroundColor:"rgba(0,0,255,0.5)"
  }, {
    label: "Series B",
    data: [25, 40, 10, 40, 30],
    borderColor: "rgba(255,0,0,0.8)",
    backgroundColor:"rgba(255,0,0,0.5)"
  }]
};

并在选项对象上启用工具提示(注意模式标签):

var options = {
  tooltips: {
   enabled: true,
   mode: 'label'
 },          
  legend: {
    display: true,
  }  
};

结果:

Chart.js label tooltips

Codepen:

Codepen - Chart.js Multiline Tooltip labels