gpt4 book ai didi

javascript - 如何在 pie chart.js 中创建图例位置?

转载 作者:行者123 更新时间:2023-11-30 14:42:42 25 4
gpt4 key购买 nike

我正在使用图表 js 来开发我的饼图。我想像这样创建图例位置。任何人都请帮我解决这个问题......

enter image description here

这是我的代码...我实际上想要的结果就像图片一样。当我在 html 中使用 css zoom:70% 并使 javascript 不起作用时出现错误..

<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
</head>
<style type="text/css">
body{ background-color: #1f1d1d; font-family: Roboto, Myriad Pro, Segoe UI;zoom:70%;}
.col-sm-1.content { border: 2px solid #1f1d1d}
</style>
<body>
<div class="col-sm-1 content" style="background-color: #4f4f4f; height:377px;"><br><br><br>
<canvas id="myChartBBC" style="width:375px; height: 375px;"></canvas>
<br>
<p style="text-align:center; color: #fff;font-size: 14px;"><i>Browse by Channel</i></p>
</div>
</body>
<script>
var ctx = document.getElementById('myChartBBC').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'pie',
// The data for our dataset
data: {
labels: ["USSD", "URP", "MyTsel App", "Chatbot"],
datasets: [{
backgroundColor: ['rgb(12, 146, 204)',
'rgb(255, 67, 0)',
'rgb(131, 0, 255)',
'rgb(250, 255, 0)'
],
borderColor: ['rgb(12, 146, 204)',
'rgb(255, 67, 0)',
'rgb(131, 0, 255)',
'rgb(250, 255, 0)'
],
data: [73, 17, 3, 7],
}]
},

// Configuration options go here
options: {
legend:{
position: 'bottom',
labels:{
fontColor: "white"
}
}
}
});
</script>
</html>

最佳答案

试试这个更新后的代码。

<!doctype html>
<html>

<head>
<title>Pie Chart</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style type="text/css">
body {
background-color: #1f1d1d;
font-family: Roboto, Myriad Pro, Segoe UI;
width: 800px;
height: 800px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
</head>

<body>
<div id="canvas-holder" style="width:30%;background-color: #4f4f4f; height:35%;">
<canvas id="chart-area"></canvas>
<p style="text-align:center; color: #fff;font-size: 14px;"><i>Browse by Channel</i></p>
</div>
<script>
var config = {
type: 'pie',
data: {
labels: ["USSD", "URP", "MyTsel App", "Chatbot"],
datasets: [{
backgroundColor: ['rgb(12, 146, 204)',
'rgb(255, 67, 0)',
'rgb(131, 0, 255)',
'rgb(250, 255, 0)'
],
borderColor: ['rgb(12, 146, 204)',
'rgb(255, 67, 0)',
'rgb(131, 0, 255)',
'rgb(250, 255, 0)'
],
data: [73, 17, 3, 7],
}]
},
options: {
responsive: true,
legend: {
position: 'bottom',
labels: {
fontColor: "white",
boxWidth: 20,
padding: 20
}
}
}
};

window.onload = function() {
var ctx = document.getElementById('chart-area').getContext('2d');
window.myPie = new Chart(ctx, config);
};
</script>
</body>

</html>

关于javascript - 如何在 pie chart.js 中创建图例位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49422790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com