How can I cause a legend to appear to the right of the pie (Chart.JS)?

前端 未结 2 1148
盖世英雄少女心
盖世英雄少女心 2021-01-27 19:26

I\'m creating a fairly simple pie chart with Chart.JS like so:

var data = {
    labels: [
        \"Bananas (18%)\",
        \"Lettuce, Romaine (14%)\",
                 


        
2条回答
  •  遥遥无期
    2021-01-27 19:45

    You have to turn the default legend off in the options first with:

    legend: {
            display: false
    },
    

    Also your id selector for your legend was wrong. Fiddle.

    Wrap the chart in the

    and set the height and width of that since the canvas will be responsive to its container, then set the canvas's div and the legend's div as display:inline-block.

    HTML

    CSS

     #kpi{
          height: 400px;
          width: 100%;
          border: 1px solid black;
          background-color: white;
        }
        .pieLegend li span {
          display: inline-block;
          width: 12px;
          height: 12px;
          margin-right: 5px;
        }
    
        #top10Legend {
          display: inline-block;
        }
    
        #container {
          display: inline-block;
          height: 50%;
          width: 50%;
        }
    
        #top10Legend>ul{
          list-style:none;
        }
    

提交回复
热议问题