How can I control the placement of my Chart.JS pie chart's legend, as well as its appearance?

前端 未结 3 1070
眼角桃花
眼角桃花 2021-02-05 07:01

I am able to create a pie chart using Chart.JS with this code:

HTML

3条回答
  •  北海茫月
    2021-02-05 07:34

    This is what works (more or less) using version 2 of Chart.JS:

    HTML

    Top 10 Items


    JQUERY

    var data = {
        labels: [
            "Bananas: 2,755 (18%)",
            "Lettuce, Romaine: 2,256 (14%)",
            "Melons, Watermelon: 1,637 (10%)",
            "Pineapple: 1,608 (10%)",
            "Berries: 1,603 (10%)",
            "Lettuce, Spring Mix: 1,433 (9%)",
            "Broccoli: 1,207 (8%)",
            "Melons, Honeydew: 1,076 (7%)",
            "Grapes: 1,056 (7%)",
            "Melons, Cantaloupe: 1,048 (7%)"
        ],
        datasets: [
            {
                data: [2755, 2256, 1637, 1608, 1603, 1433, 1207, 1076, 1056, 1048],
                backgroundColor: [
                    "#FFE135",
                    "#3B5323",
                    "#fc6c85",
                    "#ffec89",
                    "#021c3d",
                    "#3B5323",
                    "#046b00",
                    "#cef45a",
                    "#421C52",
                    "#FEA620"
                ],
            }]
    };
    
    var optionsPie = {
        responsive: true,
        scaleBeginAtZero: true
    }
    
    var ctx = $("#top10ItemsChart").get(0).getContext("2d");
    var top10PieChart = new Chart(ctx,
    {
        type: 'pie',
        data: data,
        options: optionsPie
    });
    
    $("#top10Legend").html(top10PieChart.generateLegend());
    

    I say, "more or less" because the pie pieces are still pitifully puny:

提交回复
热议问题