如何在X和Y轴上设置衬线?

时间:2018-12-21 10:30:24

标签: javascript chart.js

enter image description here

我需要像图像上一样在X和Y轴上设置衬线。

我尝试在文档中找到此功能,但一无所获。

1 个答案:

答案 0 :(得分:0)

选项->轴->勾号具有字体属性,您可以在此属性之前或之后进行设置/更改。

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: 'some numbers',
            data: [5, 1, 3, 2, 4, 1],
			backgroundColor: ["red", "blue", "yellow", "green", "purple", "orange"],
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }],
            yAxes: [{
                ticks: {
                    fontSize: "14"
                }
            }]
        }
    }
});

document.getElementById("testbtn").addEventListener("click", function() {
	myChart.options.scales.yAxes[0].ticks.fontStyle = "bold";
	myChart.options.scales.xAxes[0].ticks.fontFamily = "Verdana";
	myChart.options.scales.xAxes[0].ticks.fontColor = "red";
	myChart.update();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
<canvas id="myChart" height="50"></canvas>
  <br>
  <button id="testbtn">change font</button>