-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Sample plugin in docs for canvas background #8492
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3536df
color done
LeeLenaleee fcad86b
add example inline plugins for background image and color
LeeLenaleee 336c80b
add link to css background
LeeLenaleee c93b406
improve text bit
LeeLenaleee 7bacdf1
fix build error
LeeLenaleee d346ef8
implement kurkles feedback
LeeLenaleee 609d454
fix indenting tab -> spaces
LeeLenaleee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| --- | ||
| title: Canvas background | ||
| --- | ||
|
|
||
| In some use cases you would want a background image or color over the whole canvas. There is no build in support for this, the way you can achieve this is by writing a custom plugin. | ||
|
|
||
| In the two example plugins underneath here you can see how you can draw an color or image to the canvas as background. This way of giving the chart a background is only necessary if you want to export the chart with that specific background. | ||
| For normal use you can set the background more easily with [CSS](https://www.w3schools.com/cssref/css3_pr_background.asp). | ||
|
|
||
| import { useEffect, useRef } from 'react'; | ||
| import Tabs from '@theme/Tabs'; | ||
| import TabItem from '@theme/TabItem'; | ||
|
|
||
| <Tabs | ||
| defaultValue='color' | ||
| values={[ | ||
| {label: 'Color', value: 'color' }, | ||
| {label: 'Image', value: 'image' }, | ||
| ]} | ||
| > | ||
| <TabItem value="color"> | ||
|
|
||
| ```jsx live | ||
| function example() { | ||
| const canvas = useRef(null); | ||
| useEffect(() => { | ||
| const cfg = { | ||
| type: 'doughnut', | ||
| data: { | ||
| labels: [ | ||
| 'Red', | ||
| 'Blue', | ||
| 'Yellow' | ||
| ], | ||
| datasets: [{ | ||
| label: 'My First Dataset', | ||
| data: [300, 50, 100], | ||
| backgroundColor: [ | ||
| 'rgb(255, 99, 132)', | ||
| 'rgb(54, 162, 235)', | ||
| 'rgb(255, 205, 86)' | ||
| ], | ||
| hoverOffset: 4 | ||
| }] | ||
| }, | ||
| plugins: [{ | ||
| id: 'custom_canvas_background_color', | ||
| beforeDraw: (chart) => { | ||
| const ctx = chart.canvas.getContext('2d'); | ||
| ctx.save(); | ||
| ctx.globalCompositeOperation = 'destination-over'; | ||
| ctx.fillStyle = 'lightGreen'; | ||
| ctx.fillRect(0, 0, chart.canvas.width, chart.canvas.height); | ||
| ctx.restore(); | ||
| } | ||
| }] | ||
| }; | ||
| const chart = new Chart(canvas.current.getContext('2d'), cfg); | ||
| return () => chart.destroy(); | ||
| }); | ||
| return <div className="chartjs-wrapper"><canvas ref={canvas} className="chartjs"></canvas></div>; | ||
| } | ||
| ``` | ||
|
|
||
| </TabItem> | ||
|
|
||
| <TabItem value="image"> | ||
|
|
||
| ```jsx live | ||
| function example() { | ||
| const canvas = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| const image = new Image(); | ||
| image.src = "https://www.chartjs.org/img/chartjs-logo.svg"; | ||
|
|
||
| const cfg = { | ||
| type: 'doughnut', | ||
| data: { | ||
| labels: [ | ||
| 'Red', | ||
| 'Blue', | ||
| 'Yellow' | ||
| ], | ||
| datasets: [{ | ||
| label: 'My First Dataset', | ||
| data: [300, 50, 100], | ||
| backgroundColor: [ | ||
| 'rgb(255, 99, 132)', | ||
| 'rgb(54, 162, 235)', | ||
| 'rgb(255, 205, 86)' | ||
| ], | ||
| hoverOffset: 4 | ||
| }] | ||
| }, | ||
| plugins: [{ | ||
| id: 'custom_canvas_background_image', | ||
| beforeDraw: (chart) => { | ||
| if (image.complete) { | ||
| const ctx = chart.canvas.getContext('2d'); | ||
| ctx.drawImage(image, chart.canvas.width/2-image.width/2, chart.canvas.height/2-image.height/2); | ||
| } | ||
| } | ||
| }] | ||
| }; | ||
|
|
||
| const chart = new Chart(canvas.current.getContext('2d'), cfg); | ||
| image.onload = () => chart.draw(); | ||
|
|
||
| return () => chart.destroy(); | ||
|
LeeLenaleee marked this conversation as resolved.
|
||
| }); | ||
| return <div className="chartjs-wrapper"><canvas ref={canvas} className="chartjs"></canvas></div>; | ||
| } | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.