Is there a way to perform server side rendering of SVG graphics using React?

旧时模样 提交于 2021-01-28 05:56:02

问题


I am using Highcharts and React for a project and need to support server side rendering for the SVG generated. Could someone suggest a way of doing it so that I get a static rendered page with images as png/jpg? The browser to be used for viewing rendered content does not support svg.

Thanks.


回答1:


You can render highcharts in react using the node module react-highcharts You may use the following for official documentation https://www.npmjs.com/package/react-highcharts

You need to pass the config options to ReactHighcharts component

the below is the example of a pie chart

_piechartDataLoad() {
        return (
            {
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie',
                    style: {
                        fontFamily: "-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, sans-serif",
                        fontSize: "0.875rem",
                        fontWeight: "normal",
                        lineHeight: 1.5,
                        color: "#263238",
                    }
                },
                title: {
                    text: 'Product with more complaints'
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            connectorColor: 'silver'
                        }
                    }
                },
                series: [{
                           name: 'Brands',
                           colorByPoint: true,
                           data: [{
                             name: 'Chrome',
                             y: 61.41,
                             sliced: true,
                             selected: true
                          }, {
                             name: 'Internet Explorer',
                             y: 11.84
                          }, {
                             name: 'Firefox',
                             y: 10.85
                          }, {
                             name: 'Edge',
                             y: 4.67
                          }, {
                             name: 'Safari',
                             y: 4.18
                          }, {
                             name: 'Sogou Explorer',
                             y: 1.64
                          }, {
                             name: 'Opera',
                             y: 1.6
                          }, {
                             name: 'QQ',
                             y: 1.2
                          }, {
                             name: 'Other',
                             y: 2.61
                          }]
                      }]
            }
        )
    }

render(){
  return(
    <ReactHighcharts config={this._piechartDataLoad} ></ReactHighcharts>
  )
}



回答2:


Found the solution. Highcharts has a ready solution for doing just what I was looking for. Its a node/express server for getting png response from the highcharts configuration json using PhantomJS. https://www.highcharts.com/docs/export-module/setting-up-the-server



来源:https://stackoverflow.com/questions/50444003/is-there-a-way-to-perform-server-side-rendering-of-svg-graphics-using-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!