Created
June 21, 2020 13:01
-
-
Save kronaemmanuel/be7f8825815cb2d718634a10711c1b06 to your computer and use it in GitHub Desktop.
Nivo chart Render
This file contains 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
const React = require('react') | |
const { renderToStaticMarkup } = require('react-dom/server') | |
const { Bar } = require('nivo') | |
const storage = require('../../utils/storage') | |
const theme = {} | |
const render = (props) => { | |
const rendered = renderToStaticMarkup( | |
React.createElement( | |
Bar, | |
Object.assign( | |
{ | |
animate: false, | |
isInteractive: false, | |
theme, | |
}, | |
{ | |
margin: {top: 0, right: 0, bottom: 0, left: 0}, | |
}, | |
props | |
) | |
) | |
) | |
return `<?xml version="1.0" ?>${rendered}` | |
} | |
export default (req, res) => { | |
// get id from the request (Note there are other ways, this works well in Next JS) | |
const { | |
query: { id }, | |
} = req | |
// Get the relvant props and data for the chart | |
const config = storage.get(id) | |
// If chart is not found then send back an error | |
if (!config) { | |
return res.status(404).send(`no chart found for id "${id}"`) | |
} | |
// Otherwise send back the rendered chart | |
const rendered = render(config, req.query) | |
res.set('content-type', 'image/svg+xml').status(200).send(rendered) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment