简体   繁体   English

如何在图表顶部显示标签(chart.js)

[英]how to display labels at top of charts(chart.js)

in the below code the labels are appiles (that i can see the label name on hovering over the colors) but not displayed in the top of the chart (this.color->this.value)like,need labels at the top of chart as like below image.在下面的代码中,标签是应用程序(我可以看到 label 名称悬停在颜色上)但未显示在图表顶部(this.color->this.value),需要在图表顶部添加标签如下图所示。 help to get those labels.帮助获得这些标签。 需要在图表顶部添加标签,如下图所示

 var p=[22,33,44,55,66];          
firebase.firestore().collection("product_info").onSnapshot(
    async function(querySnapshot){
        querySnapshot.forEach(async function(doc){                      
            var temp=await doc.data().name;
            it.push(await temp);
                console.log(it);
});     
    });  
     console.log(it);
Chart.defaults.global.defaultFontFamily = '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
Chart.defaults.global.defaultFontColor = '#292b2c';
var ctx = document.getElementById("myPieChart");
var myPieChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: it,
    datasets: [{
      data: p,
      backgroundColor: ['#007bff', '#dc3545', '#ffc107', '#28a745'],
    }],
  }
,
});

Add a legend to your config:在配置中添加 图例

{

    type: 'pie',
    data: {
        ...
    },
    options: {
        legend: {
            position: 'top'
        }
    }
    ...
}

Example :示例

 var ctx = document.getElementById("myChart").getContext('2d'); var myChart = new Chart(ctx, { type: 'pie', data: { labels: [ "My", "Dataset", "Labels" ], datasets: [{ data: [ 22, 33, 44 ], backgroundColor: [ '#007bff', '#dc3545', '#9932CC' ] }] }, options: { legend: { position: 'top' } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.js"></script> <div class="container"> <div> <canvas id="myChart"></canvas> </div> </div>

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

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