次のコードで 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 の境界の外側にまではみ出したりはみ出したりしてしまうことです。
また、凡例は単純な順序なしリストとして表示されます。私がやりたいのは、凡例のさまざまな要素の色を制御することです (「バナナ」はバナナ パイと同じ色 (#FFE135) にするなど)。
個々の要素をそれぞれのデータ ポイントの色と一致させるにはどうすればよいですか?
アップデート
公式ドキュメントの「凡例ラベルの設定」トピックここ凡例の fontColor を設定できることを示していますが、これは全体の設定です。私が知りたいのは、各項目の色をどのように制御できるかということです。
アップデート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
提案されたコードをいくつか利用しましたが、凡例は右側に垂れ下がるのではなく、依然として重力で供給されています。
したがって、凡例は、その近傍に限定されるのではなく、その下のグラフに影響を及ぼします。
また、凡例に箇条書きがはみ出るのは望ましくありません。必要なのは、色付きの四角形 (および言葉、そして値) だけです。凡例を円グラフの南側から東側に移動するにはどうすればよいでしょうか。
アップデート4
私は以下のコードに基づいてリファクタリングしましたこれ見た目も良くなりました (データ配列の「ラベル」値にもデータを追加しました):
しかし、ご覧のとおり、凡例がその下の象限を侵害しています。円グラフの周りには大量の空きスペース/無駄なスペースがあります。円グラフを左に移動し、凡例を円グラフの右に移動したいと思います。そうすれば、円グラフの高さを増すための垂直方向のスペースも増えます。
どうすればそれができるでしょうか? 現在使用しているコードは次のとおりです:
html
<div>
<canvas id="top10ItemsChart" class="pie" style="padding-left:20px"></canvas>
<div id="top10Legend"></div>
</div>
CS
.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'
},
...何もしない - 伝説は、ウズラの弾丸を顎まで詰め込まれたカエルのように、床に押し付けられたままである。
アップデート5
Teo の例をいろいろ試して、うまく動作するようにしてみましたが、改善はされたものの、円グラフが非常に小さく、凡例はもっと広くするべきなのに、凡例を水平に、円グラフを全方向に伸ばす方法がわかりません。現在の状態は次のとおりです。
これが現在のコードです (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>
CS
.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 ら: コードを Och 化した後、次のようになります。
これは私のコードです。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>
CS
.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());
...それでも、パイは象のストレッチパンツよりも伸縮性があります。
アップデート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をほぼそのままコピーしましたここ。
変更したのは、2 つの CSS クラスの名前 (「table」が「legendTable」に、「cell」が「legendCell」に) と、テーブルの境界線の色を赤からフォレストグリーンに変更しただけです。これで、次のようになります。
Chart.JS CSS ファイルなどを参照する必要もありますか?
ベストアンサー1
あなたが望んでいるのはこれだと思います:デモ
canvas
まず、固定幅と高さをオーバーライドしてレスポンシブにし、div
配置に使用できる追加要素でラップする必要があります。要素を中央揃えに使用しましたが、チャートと凡例に 50:50 以外のスペースを割り当てたい場合は、display: table
内部の div を に設定しても機能します。inline-block
HTML:
<div class="table">
<div class="cell">
<canvas id="top10ItemsChart" class="pie"></canvas>
</div>
<div class="cell" id="top10Legend"></div>
</div>
CS: ...
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による追加情報に基づいて調整を行いました新しいデモ
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>
CS: ...
.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;
}