如何将数字映射为 1,使得 number/2 === 0.5 等等?
How to map a number to 1, so that number/2 === 0.5 and so on?
我正在进行 HTTP 调用,我得到了两个值。我需要将最高值映射到 1,因为我使用的进度条库只接受 0 到 1 之间的值。它类似于:
让交付 = 1000;
让 pending = 250;
然后我需要减去这两个值(在这个例子中我会得到 75)但我需要这个值是 0.75。
如果delivered = 1250,pending = 0,那么我需要一个total变量为1,这样进度条跨度到100%。
这是一个 'working' 片段:
let delivered, pending, cumplidas, progress;
fetch("http://157.230.190.251/api/v1/cmodels/secure/dashboard")
.then(function(response) {
return response.json();
})
.then(function(json) {
console.log(json)
cumplidas = json.kpis[0]
delivered = cumplidas.entregadas;
pending = cumplidas.pendientes;
progress = delivered - pending;
console.log(progress)
})
var bar = new ProgressBar.Circle(progreso, {
strokeWidth: 6,
easing: 'easeInOut',
duration: 1400,
color: '#FFEA82',
trailColor: '#eee',
trailWidth: .6,
svgStyle: {
display: 'block',
width: '100%'
},
text: {
value: '75', // here I need to insert 'progress' variable, not this hardcoded value
style: {
color: '#f00',
position: 'absolute',
left: '50%',
top: '50%',
padding: 0,
margin: 0,
// You can specify styles which will be browser prefixed
transform: {
prefix: true,
value: 'translate(-50%, -50%)'
}
}}
});
bar.animate(.75); /* Number from 0.0 to 1.0 . Here I need to instert 'progress' variable, not this hardcoded value*/
#progreso {
margin: 20px;
width: 150px;
height: 150px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"
></script>
<script src="https://cdn.tutorialjinni.com/progressbar.js/1.1.0/progressbar.js"></script>
</head>
<body>
<div id="progreso">
</div>
<script>
</script>
</body>
</html>
您将通过这种方式实现:
let progress = delivered / (pending + delivered);
我正在进行 HTTP 调用,我得到了两个值。我需要将最高值映射到 1,因为我使用的进度条库只接受 0 到 1 之间的值。它类似于:
让交付 = 1000;
让 pending = 250;
然后我需要减去这两个值(在这个例子中我会得到 75)但我需要这个值是 0.75。
如果delivered = 1250,pending = 0,那么我需要一个total变量为1,这样进度条跨度到100%。
这是一个 'working' 片段:
let delivered, pending, cumplidas, progress;
fetch("http://157.230.190.251/api/v1/cmodels/secure/dashboard")
.then(function(response) {
return response.json();
})
.then(function(json) {
console.log(json)
cumplidas = json.kpis[0]
delivered = cumplidas.entregadas;
pending = cumplidas.pendientes;
progress = delivered - pending;
console.log(progress)
})
var bar = new ProgressBar.Circle(progreso, {
strokeWidth: 6,
easing: 'easeInOut',
duration: 1400,
color: '#FFEA82',
trailColor: '#eee',
trailWidth: .6,
svgStyle: {
display: 'block',
width: '100%'
},
text: {
value: '75', // here I need to insert 'progress' variable, not this hardcoded value
style: {
color: '#f00',
position: 'absolute',
left: '50%',
top: '50%',
padding: 0,
margin: 0,
// You can specify styles which will be browser prefixed
transform: {
prefix: true,
value: 'translate(-50%, -50%)'
}
}}
});
bar.animate(.75); /* Number from 0.0 to 1.0 . Here I need to instert 'progress' variable, not this hardcoded value*/
#progreso {
margin: 20px;
width: 150px;
height: 150px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"
></script>
<script src="https://cdn.tutorialjinni.com/progressbar.js/1.1.0/progressbar.js"></script>
</head>
<body>
<div id="progreso">
</div>
<script>
</script>
</body>
</html>
您将通过这种方式实现:
let progress = delivered / (pending + delivered);