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

前端 未结 2 1146
盖世英雄少女心
盖世英雄少女心 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 <div> 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

    <div id="kpi">
        <div id="container">
          <canvas id="top10ItemsChart"></canvas>
        </div>
        <div id="top10Legend" class="pieLegend"></div>
    </div>
    

    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;
        }
    
    0 讨论(0)
  • 2021-01-27 19:48

    Another way to right align the legend is to add:

    legend: {
       position:"right"
    }
    

    To the Chart options

    JSfiddle

    0 讨论(0)
提交回复
热议问题