最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger zoom in and zoom out in plotly chart using user created on click buttons? - Stack Overflow

programmeradmin1浏览0评论

I am building an angular application. In which we need to create on click buttons for zooming in and zooming out for plotly chart. We can zoom in zoom out in plotly chart using buttons on hoverable mode bar but this is not required for our application. We want to zoom in and zoom out the chart using user created on click buttons. Is there a way to trigger actions of hoverable mode bar zoom in and out using on click buttons? if not then what are the other ways to do? any suggestion is appreciated.

Code in .ts file

basicChart() {

    var trace1 = {
        x: ['2020-10-04', '2021-11-04', '2023-12-04'],
        y: [90, 40, 60],
        type: 'scatter'
    };

    var data = [trace1];

    var layout = {
        title: 'Chart',
        showlegend: false
    };

    Plotly.newPlot('myDiv', data, layout);
}

I am building an angular application. In which we need to create on click buttons for zooming in and zooming out for plotly chart. We can zoom in zoom out in plotly chart using buttons on hoverable mode bar but this is not required for our application. We want to zoom in and zoom out the chart using user created on click buttons. Is there a way to trigger actions of hoverable mode bar zoom in and out using on click buttons? if not then what are the other ways to do? any suggestion is appreciated.

Code in .ts file

basicChart() {

    var trace1 = {
        x: ['2020-10-04', '2021-11-04', '2023-12-04'],
        y: [90, 40, 60],
        type: 'scatter'
    };

    var data = [trace1];

    var layout = {
        title: 'Chart',
        showlegend: false
    };

    Plotly.newPlot('myDiv', data, layout);
}
Share Improve this question edited Jan 20, 2020 at 8:30 ankitkanojia 3,1224 gold badges24 silver badges37 bronze badges asked Jan 20, 2020 at 4:45 VikramVikram 931 silver badge6 bronze badges 4
  • Please add your existing code to give users a place to start. – sfarbota Commented Jan 20, 2020 at 4:53
  • @sfarbota I have added the .ts file code. – Vikram Commented Jan 20, 2020 at 5:03
  • 1 You cannot trigger native Plotly events, but you can provide new X and Y ranges for your chart and apply Plotly.relayout. Can't your users a mouse for zooming in? – Mark Commented Jan 28, 2020 at 20:27
  • Yes I did it this way only. @Mark – Vikram Commented Jan 30, 2020 at 9:07
Add a ment  | 

1 Answer 1

Reset to default 4

I had this same problem and ended up just using the DOM to click the modebar.

zoom-functions-example.js

const plot = Plotly.newPlot('myDiv', data, layout)
let zoomIn = function(plot){
  plot.querySelector('a[data-attr="zoom"][data-val="in"]').click()
},
let zoomOut = function(plot){
  plot.querySelector('a[data-attr="zoom"][data-val="out"]').click()
},
let resetView = function(plot){
  plot.querySelector('a[data-attr="zoom"][data-val="reset"]').click()
},

CSS to hide modebar:

.modebar-container{
  display: none;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论