gpt4 book ai didi

javascript - Chart.js 中条形图的颜色未显示。我尝试了背景颜色和填充颜色,但都不起作用

转载 作者:行者123 更新时间:2023-12-01 03:21:48 25 4
gpt4 key购买 nike

我正在制作一个条形图,它将每月显示数据,但无论我做什么,图表的背景颜色都不会显示。 I can only see the value of the chart when I hover the month

这是代码。

index.php

<body>

<div id="chart-container1">
<canvas id="mycanvas2"></canvas> // I call the bar chart
</div>

<!-- javascript -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/Chart.bundle.js"></script>
<script type="text/javascript" src="js/app2.js"></script>


</body>

数据.php

$allMonth = sprintf("SELECT DATENAME(MONTH, a.date) as monthly, MAX(a.kwh) as kwh
from tbl_totalMonth a
group by DATENAME(MONTH, a.date)");

$allMonthResult = sqlsrv_query($db, $allMonth);

if ($allMonthResult==false) {
echo "ERROR to retrive info!! <br />";
die(print_r(sqlsrv_errors(),TRUE));
}

$data2 = array();

while ($MONTH_RES = sqlsrv_fetch_array($allMonthResult, SQLSRV_FETCH_ASSOC)) {
$data2[] = $MONTH_RES;
}


print json_encode($data2);

输出:[{"monthly":"7月","kwh":"400000000.2120"}]

app2.js

$(document).ready(function(){
$.ajax({
url:" http://localhost/chart/e/data2.php",
method: "GET",
success: function(data2){
console.log(data2);
var monthly = [];
var kwh = [];



for(var i in data2){
monthly.push(data2[i].monthly);
kwh.push(data2[i].kwh);
}

var chartdata = {
labels: monthly,
datasets : [
{
label: 'Month',
// backgroundColor: 'rgba(134,159,152, 1)',
// borderColor: 'rgba(134,159,152, 1)',
// hoverBackgroundColor: 'rgba(230, 236, 235, 0.75)',
// hoverBorderColor: 'rgba(230, 236, 235, 0.75)',
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: kwh
}
]
};

var ctx = $("#mycanvas2");

var barGraph = new Chart(ctx,{

type: 'bar',
data: chartdata
});

},

error: function(data2) {
console.log(data2);
}
});
});

最佳答案

您需要在图表选项中将 y 轴刻度的 beginAtZero 属性设置为 true,像这样:

options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}

另外,请确保您使用的是最新版本的 ChartJS (v2.6.0),

并为您的数据集使用以下属性:

backgroundColor: 'rgba(134,159,152, 1)',
borderColor: 'rgba(134,159,152, 1)',
hoverBackgroundColor: 'rgba(230, 236, 235, 0.75)',
hoverBorderColor: 'rgba(230, 236, 235, 0.75)',

而不是:

fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩

$(document).ready(function() {
$.ajax({
url: "https://istack.000webhostapp.com/json/t11.json",
method: "GET",
success: function(data2) {
console.log(data2);
var monthly = [];
var kwh = [];

for (var i in data2) {
monthly.push(data2[i].monthly);
kwh.push(data2[i].kwh);
}

var chartdata = {
labels: monthly,
datasets: [{
label: 'Month',
backgroundColor: 'rgba(134,159,152, 1)',
borderColor: 'rgba(134,159,152, 1)',
hoverBackgroundColor: 'rgba(230, 236, 235, 0.75)',
hoverBorderColor: 'rgba(230, 236, 235, 0.75)',
data: kwh
}]
};

var ctx = $("#mycanvas2");

var barGraph = new Chart(ctx, {
type: 'bar',
data: chartdata,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
},

error: function(data2) {
console.log(data2);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="mycanvas2"></canvas>

关于javascript - Chart.js 中条形图的颜色未显示。我尝试了背景颜色和填充颜色,但都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45135465/

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