gpt4 book ai didi

javascript - 在 Chart.js 栏上画线

转载 作者:行者123 更新时间:2023-11-30 09:52:39 32 4
gpt4 key购买 nike

我正在使用 Chart.js,我将创建一个小条形图。如何在 y 轴上绘制红线(无需创建新数据集),如图片所示?

在不同类型的图表中创建这条线没有问题,但在条形图中我找不到任何解决方案。

screen

我的代码:

<!doctype html>
<html>
<head>
<title>Bar Chart</title>
<script src="../Chart.js"></script>
</head>
<body>
<div style="width: 50%">
<canvas id="canvas" height="450" width="600"></canvas>
</div>


<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*70)};

var orders = [ '181-1','181-2','181-3','182-1','183-1','183-2','183-3','183-4','184-1','184-2'];

var barChartData = {
labels : orders,
datasets : [
{
label: "Order",
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 : [6,randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),5,6]
}
]

}



window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");

var chart = new Chart(ctx).Bar(barChartData, {
responsive : true,
animation: true,
barValueSpacing : 5,
barDatasetSpacing : 1,
tooltipFillColor: "rgba(0,0,0,0.8)",
// multiTooltipTemplate: "<%= datasetLabel %> - <%= value %>"
tooltipTemplate: function(valuesObject){
console.log(valuesObject);
// do different things here based on whatever you want;

var label = valuesObject.label
var objLen = label.length;
// 111-1
var string = label.substring(0,objLen-2);

console.log(string);

return "Order nr: " + string;
}


});

最佳答案

要添加红线,您需要扩展默认条形图并将红线添加到特定位置。这是将红线添加到条形图的代码。你只需要计算出需要添加红线的位置即可。

这是完整的 HTML

<!doctype html>
<html>
<head>
<title>Bar Chart</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
</head>
<body>
<div style="width: 50%">
<canvas id="canvas" height="450" width="600"></canvas>
</div>


<script type="text/javascript">
var randomScalingFactor = function(){ return Math.round(Math.random()*70)};

var orders = [ '181-1','181-2','181-3','182-1','183-1','183-2','183-3','183-4','184-1','184-2'];

var barChartData = {
labels : orders,
datasets : [
{
label: "Order",
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 : [6,randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),5,6]
}
]

}



window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");


// Notice now we're extending the particular Bar chart type, rather than the base class.
Chart.types.Bar.extend({
// Passing in a name registers this chart in the Chart namespace in the same way
name: "BarAlt",
initialize: function(data){
console.log('Bar chart extension to add red line');
Chart.types.Bar.prototype.initialize.apply(this, arguments);
},
draw: function() {
Chart.types.Bar.prototype.draw.apply(this, arguments);
this.chart.ctx.beginPath();
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(25, 50);
this.chart.ctx.lineTo(1000,50);
this.chart.ctx.strokeStyle="red";
this.chart.ctx.stroke();
}
});

var chart = new Chart(ctx).BarAlt(barChartData, {
responsive : true,
animation: true,
barValueSpacing : 5,
barDatasetSpacing : 1,
tooltipFillColor: "rgba(0,0,0,0.8)",
// multiTooltipTemplate: "<%= datasetLabel %> - <%= value %>"
tooltipTemplate: function(valuesObject){
console.log(valuesObject);
// do different things here based on whatever you want;

var label = valuesObject.label
var objLen = label.length;
// 111-1
var string = label.substring(0,objLen-2);

console.log(string);

return "Order nr: " + string;
}


});
}
</script>
</body>

</html>

关于javascript - 在 Chart.js 栏上画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576797/

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