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

3.7k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

1
On

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>
  )
}