html - 如何控制我的 Chart.JS 饼图图例的位置及其外观?

标签 html css chart.js legend legend-properties

我可以使用 Chart.JS 和以下代码创建饼图:

HTML

<div>
    <canvas id="top10ItemsChart" style="padding-left:20px" width="320" height="320"></canvas>
    <div id="top10Legend" class="chart-legend"></div>
</div>

jQuery
var data = [{
    value: 2755,
    color: "#FFE135",
    label: "Bananas"
}, {
    value: 2256,
    color: "#3B5323",
    label: "Lettuce, Romaine"
}, {
    value: 1637,
    color: "#fc6c85",
    label: "Melons, Watermelon"
}, {
    value: 1608,
    color: "#ffec89",
    label: "Pineapple"
}, {
    value: 1603,
    color: "#021c3d",
    label: "Berries"
}, {
    value: 1433,
    color: "#3B5323",
    label: "Lettuce, Spring Mix"
}, {
    value: 1207,
    color: "#046b00",
    label: "Broccoli"
}, {
    value: 1076,
    color: "#cef45a",
    label: "Melons, Honeydew"
}, {
    value: 1056,
    color: "#421C52",
    label: "Grapes"
}, {
    value: 1048,
    color: "#FEA620",
    label: "Melons, Cantaloupe"
}];

var optionsPie = { 
   legend: {
       display: true,
       position: 'right',
       labels: {
           fontColor: 'rgb(255, 99, 132)'
       }
  }
}

var ctx = $("#top10ItemsChart").get(0).getContext("2d");
var top10PieChart = new Chart(ctx).Pie(data, optionsPie);
document.getElementById('top10Legend').innerHTML = top10PieChart.generateLegend();

问题是它将图例定位到饼图的底部,甚至在我希望饼图限制自身的 div 边界之外溢出和流血:

enter image description here

它还将图例显示为一个简单的无序列表。我想要做的是控制图例中各种元素的颜色(“香蕉”应该与香蕉派(可以这么说)等颜色相同(#FFE135)等)

如何使各个元素与其各自数据点的颜色相匹配?

更新

官方文档中的“图例标签配置”主题here表示您可以设置图例的 fontColor ,但这是针对整个shebang的;我想知道的是,如何控制每个元素的颜色?

更新 2

为了至少让图例显示在所需的位置,我将其添加到 jQuery:
var optionsPie = {
            legend: {
                display: true,
                position: 'right',
                labels: {
                    fontColor: 'rgb(255, 99, 132)'
                }
            }
        }

. . .
var myPieChart = new Chart(ctx).Pie(data, optionsPie);
document.getElementById("legendDiv").innerHTML = myPieChart.generateLegend();

...但它没有区别 - 图例仍然悬卡在饼图的底部,其字体仍然是默认的黑色。

更新 3

我使用了一些建议的代码,但图例仍然是重力馈送而不是卡在右侧:

enter image description here

因此,图例会影响其下方的图表,而不是将其限制在自己的附近。

此外,我不希望元素符号感染图例 - 彩色方 block (和措辞 - 以及值)就是我所需要的。怎么把馅饼南边的传说推到馅饼东边?

更新 4

我已经基于 this 重构了代码它看起来更好(我也向数据数组的“标签”值添加了更多数据):

enter image description here

尽管如此,正如您所看到的,图例在其下方的象限中侵犯了版权。不过,馅饼周围有一大堆空的/浪费的空间 - 我想将馅饼向左移动,将图例移到馅饼的右侧。这也将为馅饼的成长提供更多的垂直空间。

我怎样才能做到这一点?这是我现在使用的代码:

HTML
<div>
    <canvas id="top10ItemsChart" class="pie" style="padding-left:20px"></canvas>
    <div id="top10Legend"></div>
</div>

CSS
.pie-legend {
    list-style: none;
    margin: 0;
    padding: 0;
}

    .pie-legend span {
        display: inline-block;
        width: 14px;
        height: 14px;
        border-radius: 100%;
        margin-right: 16px;
        margin-bottom: -2px;
    }

    .pie-legend li {
        margin-bottom: 10px;
        display: inline-block;
        margin-right: 10px;
    }

查询
    var data = [{
        value: 2755,
        color: "#FFE135",
        label: "Bananas: 2,755 (18%)"
    }, {
. . .            
}, {
        value: 1048,
        color: "#FEA620",
        label: "Melons, Cantaloupe: 1,048 (7%)"
    }];

    var optionsPie = {
        responsive: true,
        scaleBeginAtZero: true,
        legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
    }

    var ctx = $("#top10ItemsChart").get(0).getContext("2d");
    var top10PieChart = new Chart(ctx).Pie(data, optionsPie);
    $("#top10Legend").html(top10PieChart.generateLegend());

注意 :将此添加到 optionsPie:
legend: {
    display: true,
    position: 'right'
},

......什么都不做 - 传说仍然沉重地压在地板上,就像一只被鹌鹑射到下巴的 Frog 。

更新 5

我玩过 Teo 的例子,试图让它恰到好处地工作,但是,虽然它更好,但馅饼非常小,而且图例应该更宽,但我不知道如何水平拉伸(stretch)图例和各个方向的馅饼。这是它现在的样子:

enter image description here

这是现在的代码(JQUERY 是一样的):

HTML
<div class="col-md-6">
    <div class="topleft">
        <h2 class="sectiontext">Top 10 Items</h2>
        <br />
        <div class="legendTable">
            <div class="legendCell">
                <canvas id="top10ItemsChart" class="pie" style="padding-left:20px"></canvas>
            </div>
            <div class="legendCell" id="top10Legend">
            </div>
        </div>
    </div>
</div>

CSS
    .topleft {
        margin-top: -4px;
        margin-left: 16px;
        margin-bottom: 16px;
        padding: 16px;
        border: 1px solid black;
    }

canvas {
    width: 100% !important;
    height: auto !important;
}

.legendTable {
    border: 1px solid forestgreen;
    display: table;
    width: 100%;
    table-layout: fixed;
}

.legendCell {
    display: table-cell;
    vertical-align: middle;
}

.pie-legend ul {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 300px;
}

.pie-legend span {
    display: inline-block;
    width: 14px;
    height: 12px;
    border-radius: 100%;
    margin-right: 4px;
    margin-bottom: -2px;
}

.pie-legend li {
    margin-bottom: 4px;
    display: inline-block;
    margin-right: 4px;
}

有些东西正在挤压馅饼并将图例的外边缘推到一起。

更新 6

Ochi 等人:这是我的代码 Ochification 后看到的内容:

enter image description here

这是我的代码 - 我什至按照你的方式订购了 jQuery,尽管我怀疑这是否真的有必要:

HTML
    <div class="row" id="top10Items">
        <div class="col-md-6">
            <div class="topleft">
                <h2 class="sectiontext">Top 10 Items</h2>
                <br />
                @*<div class="legendTable">
                    <div class="legendCell">
                        <canvas id="top10ItemsChart" class="pie" style="padding-left:20px"></canvas>
                    </div>
                    <div class="legendCell" id="top10Legend">
                    </div>
                </div>*@
                <div class="chart">
                    <canvas id="top10ItemsChart" class="pie"></canvas>
                    <div id="pie_legend"></div>
                </div>
            </div>
        </div>
    . . .
</div>

CSS
.pie-legend {
  list-style: none;
  margin: 0;
  padding: 0;
}

.pie-legend span {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 100%;
  margin-right: 16px;
  margin-bottom: -2px;
}

.pie-legend li {
  margin-bottom: 10px;
  display: block;
  margin-right: 10px;
}

.chart,
#priceComplianceBarChart,
#pie_legend {
  display: inline-flex;
  padding: 0;
  margin: 0;
}

查询
var optionsPie = {
    responsive: true,
    scaleBeginAtZero: true,
    legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
}

var ctx = $("#top10ItemsChart").get(0).getContext("2d");

var data = [{
    value: 2755,
    color: "#FFE135",
    label: "Bananas: 2,755 (18%)"
    . . .
}, {
    value: 1048,
    color: "#FEA620",
    label: "Melons, Cantaloupe: 1,048 (7%)"
}];

var top10PieChart = new Chart(ctx).Pie(data, optionsPie);
$("#pie_legend").html(top10PieChart.generateLegend());

......然而,馅饼比大象身上的弹力裤更有 flex 。

更新 7

可能是配置问题什么的。我决定“升级”到 Chart.JS 的 2.1.3 版(从 1.0.2 版开始):
@*<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>*@
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.js"></script>

...并且几乎完全复制了 Teo Dragovic 的 CodePen here .

我唯一改变的是两个 CSS 类的名称(“table”变成了“legendTable”,“cell”变成了“legendCell”)和表格边框的颜色从红色变成了森林绿色,我现在明白了:

enter image description here

我是否还需要引用 Chart.JS CSS 文件或其他内容?

最佳答案

我想这就是你想要的: DEMO

首先,您需要制作canvas通过覆盖固定的宽度和高度来响应并将其包装在额外的 div 中可用于定位。我用过 display: table用于居中元素但将内部 div 设置为 inline-block如果您希望图表和图例占用与 50:50 不同的空间量,也可以使用。

HTML:

<div class="table">
    <div class="cell">
        <canvas id="top10ItemsChart" class="pie"></canvas>
    </div>
    <div class="cell" id="top10Legend"></div>
</div>

CSS:
canvas {
    width: 100% !important;
    height: auto !important;
}

.table {
    border: 1px solid red;
    display: table;
    width: 100%;
    table-layout: fixed;
}

.cell {
    display: table-cell;
    vertical-align: middle;
}

更新:根据 OP 的附加信息进行了一些调整NEW DEMO

HTML:
<div class="container">
<div class="row">
<div class="col-md-6">
    <div class="topleft">
        <h2 class="sectiontext">Top 10 Items</h2>
        <br />
        <div class="chart">
            <div class="pie">
                <canvas id="top10ItemsChart" class="pie"></canvas>
            </div>
            <div class="legend" id="top10Legend">
            </div>
        </div>
    </div>
</div>
</div>
</div>

CSS:
    .topleft {
        margin-top: -4px;
        margin-left: 16px;
        margin-bottom: 16px;
        padding: 16px;
        border: 1px solid black;
    }

canvas {
    width: 100% !important;
    height: auto !important;
    margin-left: -25%;
}

.chart {
    border: 1px solid forestgreen;
    width: 100%;
    overflow: hidden;
    position: relative;
}

.pie {
    position: relative;
    padding: 10px 0;
    // adjust as necessary
    padding-left: 10px;
    padding-right: 0;
}

.legend {
    position: absolute;
    right: 10px;
    top: 10px;
    height: 100%;
    // adjust as necessary:
    width: 48%;
}

@media (max-width: 480px) {
    .legend {
        position: relative;
        width: 100%;
    }
    .pie {
        margin: 0;
    }
}

.pie-legend ul {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 300px;
}

.pie-legend span {
    display: inline-block;
    width: 14px;
    height: 12px;
    border-radius: 100%;
    margin-right: 4px;
    margin-bottom: -2px;
}

.pie-legend li {
    margin-bottom: 4px;
    display: inline-block;
    margin-right: 4px;
}

关于html - 如何控制我的 Chart.JS 饼图图例的位置及其外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540981/

相关文章:

jquery - 如何在依赖于另一个字段的文本框上绑定(bind)动态屏蔽格式

php - 更改 WordPress/WooCommerce 商店中的按钮位置

javascript - 我在哪里使用chartjs的destroy函数?

javascript - Angular2 使用标签更新 ng2-charts

javascript - 设置 ChartJs 中所有图表通用的通用标签和背景颜色

Javascript - 使用 bool 值显示表数据以追加或创建新表

javascript - 当 background-size 被覆盖时,如何获取当前 "real"背景图像大小?

java - 在长字符串中查找模式?

html - Google 字体间距/渲染问题

javascript - 如何触发单击具有相同类别的链接?