Higcharts:有没有办法将图形显示为另一个页面作为图像?

时间:2017-02-16 03:54:32

标签: javascript jquery highcharts

我的第一页中有一个Highchart图表,我想要的是添加一个重定向到另一个页面的按钮,并通过获取data:image将图形显示为图像。

但是如果可以获取图形data:image并将其保存为变量而不重定向那对我有利。

我唯一的目的是获取图像文件(data:image)或其他方式让我将其发送到我的mPDF文件。

$(function () {
    // Create the chart
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Browser market shares. January, 2015 to May, 2015'
        },
        subtitle: {
            text: 'Click the columns to view versions. Source: <a href="http://netmarketshare.com">netmarketshare.com</a>.'
        },
        xAxis: {
            type: 'category'
        },
        yAxis: {
            title: {
                text: 'Total percent market share'
            }

        },
        legend: {
            enabled: false
        },
        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true,
                    format: '{point.y:.1f}%'
                }
            }
        },

        tooltip: {
            headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
            pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
        },

        series: [{
            name: 'Brands',
            colorByPoint: true,
            data: [{
                name: 'Microsoft Internet Explorer',
                y: 56.33,
                drilldown: 'Microsoft Internet Explorer'
            }, {
                name: 'Chrome',
                y: 24.03,
                drilldown: 'Chrome'
            }, {
                name: 'Firefox',
                y: 10.38,
                drilldown: 'Firefox'
            }, {
                name: 'Safari',
                y: 4.77,
                drilldown: 'Safari'
            }, {
                name: 'Opera',
                y: 0.91,
                drilldown: 'Opera'
            }, {
                name: 'Proprietary or Undetectable',
                y: 0.2,
                drilldown: null
            }]
        }],
        drilldown: {
            series: [{
                name: 'Microsoft Internet Explorer',
                id: 'Microsoft Internet Explorer',
                data: [
                    [
                        'v11.0',
                        24.13
                    ],
                    [
                        'v8.0',
                        17.2
                    ],
                    [
                        'v9.0',
                        8.11
                    ],
                    [
                        'v10.0',
                        5.33
                    ],
                    [
                        'v6.0',
                        1.06
                    ],
                    [
                        'v7.0',
                        0.5
                    ]
                ]
            }, {
                name: 'Chrome',
                id: 'Chrome',
                data: [
                    [
                        'v40.0',
                        5
                    ],
                    [
                        'v41.0',
                        4.32
                    ],
                    [
                        'v42.0',
                        3.68
                    ],
                    [
                        'v39.0',
                        2.96
                    ],
                    [
                        'v36.0',
                        2.53
                    ],
                    [
                        'v43.0',
                        1.45
                    ],
                    [
                        'v31.0',
                        1.24
                    ],
                    [
                        'v35.0',
                        0.85
                    ],
                    [
                        'v38.0',
                        0.6
                    ],
                    [
                        'v32.0',
                        0.55
                    ],
                    [
                        'v37.0',
                        0.38
                    ],
                    [
                        'v33.0',
                        0.19
                    ],
                    [
                        'v34.0',
                        0.14
                    ],
                    [
                        'v30.0',
                        0.14
                    ]
                ]
            }, {
                name: 'Firefox',
                id: 'Firefox',
                data: [
                    [
                        'v35',
                        2.76
                    ],
                    [
                        'v36',
                        2.32
                    ],
                    [
                        'v37',
                        2.31
                    ],
                    [
                        'v34',
                        1.27
                    ],
                    [
                        'v38',
                        1.02
                    ],
                    [
                        'v31',
                        0.33
                    ],
                    [
                        'v33',
                        0.22
                    ],
                    [
                        'v32',
                        0.15
                    ]
                ]
            }, {
                name: 'Safari',
                id: 'Safari',
                data: [
                    [
                        'v8.0',
                        2.56
                    ],
                    [
                        'v7.1',
                        0.77
                    ],
                    [
                        'v5.1',
                        0.42
                    ],
                    [
                        'v5.0',
                        0.3
                    ],
                    [
                        'v6.1',
                        0.29
                    ],
                    [
                        'v7.0',
                        0.26
                    ],
                    [
                        'v6.2',
                        0.17
                    ]
                ]
            }, {
                name: 'Opera',
                id: 'Opera',
                data: [
                    [
                        'v12.x',
                        0.34
                    ],
                    [
                        'v28',
                        0.24
                    ],
                    [
                        'v27',
                        0.17
                    ],
                    [
                        'v29',
                        0.16
                    ]
                ]
            }]
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<button>Next Page(Image)</button>  

<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/drilldown.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

2 个答案:

答案 0 :(得分:0)

您可以通过chart.getSVG()获取图表的svg图像并将其保存到变量中。然后在你决定时显示它(附加到dom)。

  var image;

  $('#save-image').on('click', function () {
    var chart = $('#container').highcharts();
    image = chart.getSVG();
  });

  $('#show-image').on('click', function () {
    if (image) {
      $('#next-page').html(image);
      image = null;
    }
  });

实例

https://jsfiddle.net/6b8cc10d/

&#13;
&#13;
$(function() {
  var image;

  $('#save-image').on('click', function () {
    var chart = $('#container').highcharts();
    image = chart.getSVG();
  });
  
  $('#show-image').on('click', function () {
    if (image) {
      $('#next-page').html(image);
      image = null;
    }
  });
  

  // Create the chart
  $('#container').highcharts({
    chart: {
      type: 'column'
    },
    title: {
      text: 'Browser market shares. January, 2015 to May, 2015'
    },
    subtitle: {
      text: 'Click the columns to view versions. Source: <a href="http://netmarketshare.com">netmarketshare.com</a>.'
    },
    xAxis: {
      type: 'category'
    },
    yAxis: {
      title: {
        text: 'Total percent market share'
      }

    },
    legend: {
      enabled: false
    },
    plotOptions: {
      series: {
        borderWidth: 0,
        dataLabels: {
          enabled: true,
          format: '{point.y:.1f}%'
        }
      }
    },

    tooltip: {
      headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
      pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
    },

    series: [{
      name: 'Brands',
      colorByPoint: true,
      data: [{
        name: 'Microsoft Internet Explorer',
        y: 56.33,
        drilldown: 'Microsoft Internet Explorer'
      }, {
        name: 'Chrome',
        y: 24.03,
        drilldown: 'Chrome'
      }, {
        name: 'Firefox',
        y: 10.38,
        drilldown: 'Firefox'
      }, {
        name: 'Safari',
        y: 4.77,
        drilldown: 'Safari'
      }, {
        name: 'Opera',
        y: 0.91,
        drilldown: 'Opera'
      }, {
        name: 'Proprietary or Undetectable',
        y: 0.2,
        drilldown: null
      }]
    }],
    drilldown: {
      series: [{
        name: 'Microsoft Internet Explorer',
        id: 'Microsoft Internet Explorer',
        data: [
          [
            'v11.0',
            24.13
          ],
          [
            'v8.0',
            17.2
          ],
          [
            'v9.0',
            8.11
          ],
          [
            'v10.0',
            5.33
          ],
          [
            'v6.0',
            1.06
          ],
          [
            'v7.0',
            0.5
          ]
        ]
      }, {
        name: 'Chrome',
        id: 'Chrome',
        data: [
          [
            'v40.0',
            5
          ],
          [
            'v41.0',
            4.32
          ],
          [
            'v42.0',
            3.68
          ],
          [
            'v39.0',
            2.96
          ],
          [
            'v36.0',
            2.53
          ],
          [
            'v43.0',
            1.45
          ],
          [
            'v31.0',
            1.24
          ],
          [
            'v35.0',
            0.85
          ],
          [
            'v38.0',
            0.6
          ],
          [
            'v32.0',
            0.55
          ],
          [
            'v37.0',
            0.38
          ],
          [
            'v33.0',
            0.19
          ],
          [
            'v34.0',
            0.14
          ],
          [
            'v30.0',
            0.14
          ]
        ]
      }, {
        name: 'Firefox',
        id: 'Firefox',
        data: [
          [
            'v35',
            2.76
          ],
          [
            'v36',
            2.32
          ],
          [
            'v37',
            2.31
          ],
          [
            'v34',
            1.27
          ],
          [
            'v38',
            1.02
          ],
          [
            'v31',
            0.33
          ],
          [
            'v33',
            0.22
          ],
          [
            'v32',
            0.15
          ]
        ]
      }, {
        name: 'Safari',
        id: 'Safari',
        data: [
          [
            'v8.0',
            2.56
          ],
          [
            'v7.1',
            0.77
          ],
          [
            'v5.1',
            0.42
          ],
          [
            'v5.0',
            0.3
          ],
          [
            'v6.1',
            0.29
          ],
          [
            'v7.0',
            0.26
          ],
          [
            'v6.2',
            0.17
          ]
        ]
      }, {
        name: 'Opera',
        id: 'Opera',
        data: [
          [
            'v12.x',
            0.34
          ],
          [
            'v28',
            0.24
          ],
          [
            'v27',
            0.17
          ],
          [
            'v29',
            0.16
          ]
        ]
      }]
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<button id="save-image">Save image</button>
<button id="show-image">Show image</button>
<div id="next-page">
</div>

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/drilldown.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
&#13;
&#13;
&#13;

我认为当您不需要服务器时,它是SPA精神的解决方案。但是,您仍然可以设置服务器,将图表导出为图像,并在下一页上将其作为静态内容提供。

答案 1 :(得分:0)

如果您想将图表渲染为图像,则需要PhantomJS服务器。

这是一个食谱: http://www.highcharts.com/docs/export-module/render-charts-serverside

基本上PhantomJS接收javascript作为输入并输出图像,你可以通过命令行来实现

phantomjs highcharts-convert.js -infile options.js -outfile chart.png

或通过http-request。

如果您的图表是动态的(js代码是服务器端生成的),您可以调用PhantomJS,检索图像,将其存储为静态资源,并使用链接到该资源的HTML进行响应。