Plotly.js 模式栏,下载为 png,给 png 一个名字

fan*_*gio 3 javascript plotly

我的网页上有一个 Plotly,您可以通过单击模式栏中的图片图标将其下载为 png。但是,当我单击它时,它会将其下载为名为 new-plot 的 png,我该如何给它一个自定义名称?

我当前的代码(var data 只是数据,所以省略了):

var layout = {
    showlegend: true,
    legend: {
        x: 0,
        y: 1
    },
    xaxis: {
        title: 'Date',
        titlefont: {
            family: 'Courier New, monospace',
            size: 18,
            color: '#7f7f7f'
        }
    },
    yaxis: {
        title: 'Sales',
        titlefont: {
            family: 'Courier New, monospace',
            size: 18,
            color: '#7f7f7f'
        }
    }
};

var options = {
    scrollZoom: true,
    showLink: false,
    modeBarButtonsToRemove: ['zoom2d', 'pan', 'pan2d', 'sendDataToCloud', 'hoverClosestCartesian', 'autoScale2d'],
    displaylogo: false,
    displayModeBar: true,
};

Plotly.newPlot('tester', data, layout, options);
Run Code Online (Sandbox Code Playgroud)

ram*_*ssa 7

Plotly.downloadImage

https://plot.ly/javascript/plotlyjs-function-reference/#plotlydownloadimage

将此添加到按钮回调的模式栏设置中:

Plotly.downloadImage({
    filename: 'customNamedImage',
    format: 'png', //also can use 'jpeg', 'webp', 'svg'
    height: 500,
    width: 500
});
Run Code Online (Sandbox Code Playgroud)

编辑:

我运行了一个自定义示例,我认为您会希望在模式栏中自定义您自己的下载按钮,如下所示:

Plotly.newPlot(gd, [{
  y: [1, 2, 1],
  line: { shape: 'spline' }
}], {
  title: 'custom modebar button',
  width: 400,
  height: 700
}, {
  showTips: false,
  displayModeBar: true,
  modeBarButtons: [[{
    name: 'custom download button',
    icon: Plotly.Icons.camera,
    click: function (gd) {
      Plotly.downloadImage(gd, {
        filename: 'your_custom_name',
        format: 'jpeg',
        width: gd._fullLayout.width,
        height: gd._fullLayout.height
      })
    }
  }, 'toImage'
  ], []]
})
Run Code Online (Sandbox Code Playgroud)