Skip to content
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

Need help: Shadows on line charts #4977

Closed
Ashot-KR opened this issue Nov 21, 2017 · 4 comments
Closed

Need help: Shadows on line charts #4977

Ashot-KR opened this issue Nov 21, 2017 · 4 comments

Comments

@Ashot-KR
Copy link

Ashot-KR commented Nov 21, 2017

I'm trying to draw line chart with shadows. The only way i found to override ctx.stroke method and add shadow properties to it, but in this case shadow appears also on gridlines and points.

Example: https://jsfiddle.net/1geu4zd5/2/

I tried to implement shadows by extending line element

const ShadowLineElement = Chart.elements.Line.extend({
  draw () {
    Chart.elements.Line.prototype.draw.apply(this, arguments)

    const { ctx } = this._chart

    const originalStroke = ctx.stroke

    ctx.stroke = function () {
      ctx.save()
      ctx.shadowColor = 'red'
      ctx.shadowBlur = 0
      ctx.shadowOffsetX = 0
      ctx.shadowOffsetY = 8
      originalStroke.apply(this, arguments)
      ctx.restore()
    }
  }
})

Is there any way to draw shadow only for lines?

@etimberg
Copy link
Member

@Ashot-KR I think you could use a plugin that hooks beforeDatasetsDraw and afterDatasetsDraw and only use a different stroke function there. https://github.com/chartjs/Chart.js/blob/master/src/core/core.controller.js#L569

Extending Line to ShadowLine won't work as the line chart will use the old element. If you did something like the following it might work.

const ShadowLineElement = Chart.elements.Line.extend(...);
Chart.elements.Line = ShadowLineElement;

@Ashot-KR
Copy link
Author

Ashot-KR commented Nov 22, 2017

Extending Line to ShadowLine won't work as the line chart will use the old element. If you did something like the following it might work

Extending line works(see the fiddle), i also create a new chart type:

Chart.defaults.ShadowLine = Chart.defaults.line
Chart.controllers.ShadowLine = Chart.controllers.line.extend({
  datasetElementType: ShadowLineElement
})

and create a chart with this type

new Chart(document.getElementById('canvas'), {
  type: 'ShadowLine'
  
}

Didn't notice it here just to be shorter.

I think you could use a plugin that hooks beforeDatasetsDraw and afterDatasetsDraw and only use a different stroke function there.

Thanks, i'll give it a try

@etimberg
Copy link
Member

Sorry about that, I missed that part of your fiddle. Trying the plugin hooks and see how they work.

I modified your fiddle a bit to remove the shadow after drawing the line element. https://jsfiddle.net/dces93wv/

@Ashot-KR
Copy link
Author

@etimberg you're my savior. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants