B. *_*non 12 html javascript css jquery chart.js2
我想在Chart.JS水平条形图中将字体更改为snazzier.我尝试了以下,但没有一个有效:
var optionsBar = {
. . .
//fontFamily: "'Candara', 'Calibri', 'Courier', 'serif'"
//bodyFontFamily: "'Candara', 'Calibri', 'Courier', 'serif'"
//bodyFontFamily: "'Candara'"
label: {
font: {
family: "Georgia"
}
}
};
Run Code Online (Sandbox Code Playgroud)
我也读到这会起作用:
Chart.defaults.global.defaultFont = "Georgia"
Run Code Online (Sandbox Code Playgroud)
...但是这段代码会去哪里,它究竟应该如何?我试过这个:
priceBarChart.defaults.global.defaultFont = "Georgia";
Run Code Online (Sandbox Code Playgroud)
......但也没有好的效果.
对于完整的图片/上下文,以下是构成此图表的所有代码:
HTML
<div class="chart">
<canvas id="top10ItemsChart" class="pie"></canvas>
<div id="pie_legend"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
JQUERY
var ctxBarChart =
$("#priceComplianceBarChart").get(0).getContext("2d");
var barChartData = {
labels: ["Bix Produce", "Capitol City", "Charlies Portland",
"Costa Fruit and Produce", "Get Fresh Sales",
"Loffredo East", "Loffredo West", "Paragon", "Piazza Produce"],
datasets: [
{
label: "Price Compliant",
backgroundColor: "rgba(34,139,34,0.5)",
hoverBackgroundColor: "rgba(34,139,34,1)",
data: [17724, 5565, 3806, 5925, 5721, 6635, 14080, 9027,
25553]
},
{
label: "Non-Compliant",
backgroundColor: "rgba(255, 0, 0, 0.5)",
hoverBackgroundColor: "rgba(255, 0, 0, 1)",
data: [170, 10, 180, 140, 30, 10, 50, 100, 10]
}
]
}
var optionsBar = {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
},
//fontFamily: "'Candara', 'Calibri', 'Courier', 'serif'"
//bodyFontFamily: "'Candara', 'Calibri', 'Courier', 'serif'"
//bodyFontFamily: "'Candara'"
//Chart.defaults.global.defaultFont = where does this go?
label: {
font: {
family: "Georgia"
}
}
};
var priceBarChart = new Chart(ctxBarChart, {
type: 'horizontalBar',
data: barChartData,
options: optionsBar
});
//priceBarChart.defaults.global.defaultFont = "Georgia";
Run Code Online (Sandbox Code Playgroud)
我甚至试过这个:
CSS
.candaraFont13 {
font-family:"Candara, Georgia, serif";
font-size: 13px;
}
Run Code Online (Sandbox Code Playgroud)
HTML
<div class="graph_container candaraFont13">
<canvas id="priceComplianceBarChart"></canvas>
</div>
Run Code Online (Sandbox Code Playgroud)
...但我认为画布绘图会照顾字体外观,因为添加它没有任何区别.
我试过这个,它完全打破了它:
Chart.defaults.global = {
defaultFontFamily: "Georgia"
}
Run Code Online (Sandbox Code Playgroud)
正如马修暗示的那样,这种方法有效(在任何特定于图表的脚本之前):
Chart.defaults.global.defaultFontFamily = "Georgia";
Run Code Online (Sandbox Code Playgroud)
Mat*_*ell 13
这应该是有用的:http://www.chartjs.org/docs/.它说"有4种特殊的全局设置可以改变图表上的所有字体.这些选项都在Chart.defaults.global".
您需要更改defaultFontFamily字体.而且defaultFontColor,defaultFontSize和defaultFontStyle对颜色,大小等.
如果要将字体系列添加到图表对象,则可以将其添加到选项对象中。
options: {
legend: {
labels: {
fontFamily: 'YourFont'
}
}...}
Run Code Online (Sandbox Code Playgroud)
这是文档的链接:https : //www.chartjs.org/docs/latest/general/fonts.html
使用chart.js更改字体大小,颜色,字体和粗细
scales: {
yAxes: [{ticks: {fontSize: 12, fontFamily: "'Roboto', sans-serif", fontColor: '#000', fontStyle: '500'}}],
xAxes: [{ticks: {fontSize: 12, fontFamily: "'Roboto', sans-serif", fontColor: '#000', fontStyle: '500'}}]
}
Run Code Online (Sandbox Code Playgroud)
查看完整代码
<head>
<title>Chart.js</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
<script src="js/Chart.bundle.js"></script>
<script src="js/utils.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
font-weight:700;
}
</style>
</head>
<body>
<div id="container" style="width:70%;">
<canvas id="canvas"></canvas>
</div>
<script>
var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var color = Chart.helpers.color;
var barChartData = {
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
datasets: [{
label: 'Completed',
// Green
backgroundColor: '#4caf50',
borderColor: '#4caf50',
borderWidth: 1,
data: [
5, 15, 25, 35, 45, 55
]
}, {
label: 'Created',
// Blue
backgroundColor: '#1976d2',
borderColor: '#1976d2',
borderWidth: 1,
data: [
10, 20, 30, 40, 50, 60
]
}]
};
window.onload = function () {
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
legend: {
position: 'top',
onClick: null
},
title: {
display: true,
text: '',
fontSize: 20
},
scales: {
yAxes: [{ticks: {fontSize: 12, fontFamily: "'Roboto', sans-serif", fontColor: '#000', fontStyle: '500'}}],
xAxes: [{ticks: {fontSize: 12, fontFamily: "'Roboto', sans-serif", fontColor: '#000', fontStyle: '500'}}]
}
}
});
};
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17292 次 |
| 最近记录: |