-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gradient to bar chart and pie chart #166
Comments
Yes, I want to add gradient in bar graph rods. please add this feature. |
What kind of gradient do you need in the PieChart? |
It has just been added in 0.12.0 version. Check out this sample The pie chart gradient is yet to come! |
<script>
const settings = {
canvasId : 'canvas',
fillAmount : 85,
baseAmount : 100,
fillColor : '#64c832',
baseColor : '#EBF5FB',
lineWidth : 20,
}
const state = {
curAmount: 0,
curDegrees: 0
}
const canvas = document.getElementById(settings.canvasId)
canvas.halfWidth = canvas.width / 2
canvas.halfHeight = canvas.height / 2
const ctx = canvas.getContext('2d')
animate()
function animate () {
update()
draw()
if (state.curAmount < settings.fillAmount) {
requestAnimationFrame(animate)
}
}
function update () {
state.curAmount += 1
state.curDegrees = (state.curAmount / settings.baseAmount) * 360
}
function draw () {
ctx.clearRect(0, 0, canvas.width, canvas.height)
drawArc(0, 360, settings.baseColor)
drawArc(-90, (state.curDegrees - 90), settings.fillColor)
drawText(state.curAmount)
}
function drawArc (startDegrees, endDegrees, lineColor) {
ctx.lineWidth = settings.lineWidth
ctx.strokeStyle = lineColor
ctx.beginPath()
ctx.arc(
canvas.halfWidth,
canvas.halfHeight,
canvas.halfWidth - (settings.lineWidth / 2),
degreesToRadians(startDegrees),
degreesToRadians(endDegrees)
)
ctx.stroke()
}
function drawText (text) {
ctx.fillStyle = settings.fillColor
ctx.font = 'bold ' + settings.fontSize + ' ' + settings.fontFace
ctx.textAlign = 'center'
ctx.fillText(
text,
canvas.halfWidth,
canvas.halfHeight + (parseInt(settings.fontSize) / 3)
)
}
function degreesToRadians (degrees) {
return degrees * Math.PI / 180
}
document.getElementById('replay').addEventListener('click', function() {
state.curAmount = 0
state.curDegrees = 0
animate()
})
</script>
|
I have created pull request it will add support for piechart gradients |
PieChart gradient is added in 0.66.0. |
I think now we're done with this issue ;) |
Instead of a solid color, can I use a gradient to fill in the pie chart segments and bar chart segments?
The text was updated successfully, but these errors were encountered: