有没有办法使用React执行SVG图形的服务器端渲染?

时间:2018-05-21 07:34:00

标签: reactjs svg highcharts serverside-rendering react-dom-server

我正在为项目使用Highcharts和React,并且需要支持生成的SVG的服务器端渲染。有人可以提出一种方法,这样我就可以得到一个带有png / jpg图像的静态渲染页面吗?用于查看呈现内容的浏览器不支持svg。

感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用节点模块 react-highcharts 渲染高级图表您可以将以下内容用于官方文档https://www.npmjs.com/package/react-highcharts

您需要将配置选项传递给ReactHighcharts组件

下面是饼图的例子

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

答案 1 :(得分:1)

找到解决方案。 Highcharts有一个现成的解决方案,可以满足我的需求。它是一个节点/表达服务器,用于使用PhantomJS从highcharts配置json获取png响应。 https://www.highcharts.com/docs/export-module/setting-up-the-server

相关问题