【问题标题】:Google Timeline Chart Go to position on redraw谷歌时间线图表转到重绘位置
【发布时间】:2023-03-25 03:15:01
【问题描述】:

绘制图表时,它会滚动到顶部,但我试图让图表在重绘时保持在同一位置。因为我看不到源代码,所以我尝试获取滚动位置并在重绘时滚动到它,但它不起作用。有人可以帮助我还是有更好的方法。谢谢。

 <div class="row">
    <div class="col-12">
      <div id="timelineDiv">
        <div class="text-center">
          <p class="mes">{{timelinecontr.mess}}</p>
        </div>
        <div class="col-12">
          <div id="timeline" class="chart"></div>
        </div>
      </div>
    </div>
  </div>

#timelineDiv{
  overflow-x: scroll;
  overflow-y: scroll;
  white-space: nowrap;
  border: 13px solid #bed5cd;
  width: 100%;
  margin: 0 auto;
  position: relative;
  background-color: deepskyblue;
  height: 550px;
}

timelinecontr.pos = $('#timeline div').scrollTop(); // position
      $('#timeline div').scrollTop(timelinecontr.pos) // this is called on chart redraw

【问题讨论】:

    标签: javascript jquery html svg google-visualization


    【解决方案1】:

    需要等到图表绘制完成后才能设置滚动位置

    在绘制图表之前,获取滚动位置

    当图表的'ready' 事件触发时,设置滚动位置

    var rowHeight = 42;
    var timelinePos;
    google.visualization.events.addListener(chart, 'ready', function () {
      $('#timeline').scrollTop(timelinePos);
    });
    
    function drawChart() {
      timelinePos = $('#timeline').scrollTop();
      chart.draw(dataTable, {
        height: (dataTable.getNumberOfRows() * rowHeight) + rowHeight
      });
    }
    

    注意:如果图表上没有设置height选项,时间线将使用自己的滚动条


    请参阅以下工作 sn-p...

    google.charts.load('current', {
      callback: function () {
        var container = document.getElementById('timeline');
        var chart = new google.visualization.Timeline(container);
        var dataTable = new google.visualization.DataTable();
        dataTable.addColumn({ type: 'string', id: 'Room' });
        dataTable.addColumn({ type: 'string', id: 'Name' });
        dataTable.addColumn({ type: 'date', id: 'Start' });
        dataTable.addColumn({ type: 'date', id: 'End' });
        dataTable.addRows([
          [ '1', 'A', new Date(2011, 3, 30), new Date(2012, 2, 4) ],
          [ '2', 'B', new Date(2012, 2, 4),  new Date(2013, 3, 30) ],
          [ '3', 'C', new Date(2013, 3, 30), new Date(2014, 2, 4) ],
          [ '4', 'D', new Date(2014, 2, 4),  new Date(2015, 2, 4) ],
          [ '5', 'E', new Date(2015, 3, 30), new Date(2016, 2, 4) ],
          [ '6', 'F', new Date(2016, 2, 4),  new Date(2017, 2, 4) ],
          [ '7', 'G', new Date(2017, 2, 4),  new Date(2018, 2, 4) ],
          [ '8', 'H', new Date(2018, 2, 4),  new Date(2019, 2, 4) ],
          [ '9', 'I', new Date(2019, 2, 4),  new Date(2020, 2, 4) ],
          [ '0', 'J', new Date(2020, 2, 4),  new Date(2021, 2, 4) ]
        ]);
    
        var rowHeight = 42;
        var timelinePos;
        google.visualization.events.addListener(chart, 'ready', function () {
          $('#timeline').scrollTop(timelinePos);
        });
    
        function drawChart() {
          timelinePos = $('#timeline').scrollTop();
          chart.draw(dataTable, {
            height: (dataTable.getNumberOfRows() * rowHeight) + rowHeight
          });
        }
    
        $('#draw-chart').on('click', drawChart);
        $(window).resize(drawChart);
        drawChart();
      },
      packages: ['timeline']
    });
    #timeline {
      overflow-x: scroll;
      overflow-y: scroll;
      white-space: nowrap;
      border: 13px solid #bed5cd;
      width: 100%;
      margin: 0 auto;
      position: relative;
      background-color: deepskyblue;
      height: 350px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="timeline"></div>
    <button id="draw-chart">Draw Chart</button>

    【讨论】:

    • 似乎工作正常,在图表最初绘制后,滚动到某个位置并按下绘制图表按钮 - 滚动位置保持在按下按钮之前的位置
    • 你说得对,我没有正确阅读此内容,并希望它在绘制后滚动。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多