我正在使用新的 Chart.js 并尝试完成一些自定义。精通 JS,但对画布很陌生,我有点挣扎。我会尽量提供尽可能多的信息。
相关链接
代码 - JSBin
JavaScript
/**
* Chart.js Global Config
*/
Chart.defaults.global.pointHitDetectionRadius = 5;
window.count = 0;
/**
* Chart Data
* @type {Object}
*/
var lineChartData = {
labels: ["", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC", ""],
datasets: [{
label: "Students",
data: [ 200, 250,220,180,290,300,370,350,200,280,260,190,210, 200 ],
backgroundColor: "rgba(247,155,45,1.0)",
borderColor: "rgba(247,155,45,1.0)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
pointBorderColor: "rgba(245,245,245,1)",
pointBackgroundColor: "rgba(80,81,81,1)",
pointHoverBorderWidth: 5,
pointBorderWidth: 5,
pointRadius: 8,
pointHoverRadius: 9,
pointHitRadius: 8,
}]
};
/**
* Init
*/
window.onload = function() {
var $chart = $('#chart');
window.lineChart = new Chart($chart[0], {
type: 'line',
data: lineChartData,
options: {
showLines: true,
// Legend
legend : {
display: false
},
// title
title:{
display:false,
text:'Student Hours'
},
// Tooltips
tooltips: {
enabled: false,
},
// Scales
scales: {
yAxes: [{
id: 'y-axis-0',
gridLines: {
display: true,
lineWidth: 1,
color: "rgba(255,255,255,0.85)"
},
ticks: {
beginAtZero:true,
mirror:false,
suggestedMin: 0,
suggestedMax: 500,
},
afterBuildTicks: function(chart) {
}
}],
xAxes: [{
id: 'x-axis-0',
gridLines: {
display: false
},
ticks: {
beginAtZero: true
}
}]
},
}
});
};
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="chartjs-container" class="chartjs-wrap">
<canvas id="chart"></canvas>
</div>
</body>
</html>
CSS
#chartjs-container {
width:80%;
margin:20px auto;
position: relative;
}
.chartjs-wrap {
background-color: rgba(250, 210, 162, 1.0);
}
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
height:100%;
}
我正在尝试做的事情
- 移除 y 轴上的网格线
- 删除数据集的第一项和最后一项上与图表左/右边缘相交的点
- 插入 y 轴刻度标签
- 使 x 轴网格线覆盖在线数据填充的顶部
截图
任何为我指明正确方向的帮助都会很棒。画布的“检查元素”是否有等价物?这些修复在 CSS 中是微不足道的,但我不确定如何调试。
干杯