Paj*_*man 26 javascript jquery canvas html5-canvas chart.js
我试图从charJs Docs中找到配置,但没有匹配.它是否可以改变图表区域的背景颜色?如果可能,任何人都可以帮助我
HTML
<canvas id="barChart" width="600" height="300"></canvas>
Run Code Online (Sandbox Code Playgroud)
使用Javascript
var ctx = document.getElementById("barChart");
var barChart = new Chart(ctx,{
type: 'bar',
data: {
labels:["Label1","Label2","Label3","Label4"],
borderColor : "#fffff",
datasets: [
{
data: ["2","3","1","4"],
borderColor : "#fff",
borderWidth : "3",
hoverBorderColor : "#000",
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5"
]
}]
},
options: {
scales: {
yAxes: [{
ticks:{
min : 0,
stepSize : 1,
fontColor : "#000",
fontSize : 14
},
gridLines:{
color: "#000",
lineWidth:2,
zeroLineColor :"#000",
zeroLineWidth : 2
},
stacked: true
}],
xAxes: [{
ticks:{
fontColor : "#000",
fontSize : 14
},
gridLines:{
color: "#fff",
lineWidth:2
}
}]
},
responsive:false
}
});
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码jsFiddle
所以每个人都可以尝试找到解决方案 谢谢你的帮助.
小智 41
没有内置方法来更改背景颜色,但您可以使用CSS.JSFiddle.
ctx.style.backgroundColor = 'rgba(255,0,0,255)';
Run Code Online (Sandbox Code Playgroud)
编辑
如果你想填充图表的确切区域而不是整个div,你可以编写自己的chart.js插件.在JSFiddle上试试吧.
Chart.pluginService.register({
beforeDraw: function (chart, easing) {
if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
var ctx = chart.chart.ctx;
var chartArea = chart.chartArea;
ctx.save();
ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
ctx.restore();
}
}
});
var config = {
type: 'bar',
data: {
labels:["Label1","Label2","Label3","Label4"],
borderColor : "#fffff",
datasets: [
{
data: ["2","3","1","4"],
borderColor : "#fff",
borderWidth : "3",
hoverBorderColor : "#000",
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5"
]
}]
},
options: {
scales: {
yAxes: [{
ticks:{
min : 0,
stepSize : 1,
fontColor : "#000",
fontSize : 14
},
gridLines:{
color: "#000",
lineWidth:2,
zeroLineColor :"#000",
zeroLineWidth : 2
},
stacked: true
}],
xAxes: [{
ticks:{
fontColor : "#000",
fontSize : 14
},
gridLines:{
color: "#fff",
lineWidth:2
}
}]
},
responsive:false,
chartArea: {
backgroundColor: 'rgba(251, 85, 85, 0.4)'
}
}
};
var ctx = document.getElementById("barChart").getContext("2d");
new Chart(ctx, config);
Run Code Online (Sandbox Code Playgroud)
根据定义,画布背景是透明的,就像任何元素一样,因此您只需要在画布中定义背景颜色,例如,在您的情况下,您将需要以下 CSS:
canvas#barChart {
background-color: #f00;
}
Run Code Online (Sandbox Code Playgroud)
或 HTML 内联,以阐明这个想法:
<canvas id="barChart" width="600" height="300" style="background-color: #f00;"></canvas>
Run Code Online (Sandbox Code Playgroud)
小智 7
这适用于最新的 Chartjs
const custom_canvas_background_color = {
id: 'custom_canvas_background_color',
beforeDraw: (chart, args, options) => {
const {
ctx,
chartArea: { top, right, bottom, left, width, height },
scales: { x, y },
} = chart;
ctx.save();
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = '#E5E5E5';
ctx.fillRect(left, top, width, height);
ctx.restore();
},
};
Run Code Online (Sandbox Code Playgroud)
然后添加为插件
plugins: [custom_canvas_background_color]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
49419 次 |
最近记录: |