最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - chart.js, after each update, avoid scrolling to top position - Stack Overflow

matteradmin1PV0评论

I am using react to render 2 charts, a line chart and bar chart stack vertically. When I scroll to look at barChart below, after each update, chartjs, forces my scroll to (0,0). I have seen some implementation that does not move the scroll's position after each update.

Please help

          <canvas id="lineChart"  onLoad={ load() }>
            Your browser does not support canvas, please upgrade to latest browser
          </canvas>

          <canvas id="barChart"  onLoad={ load() }>
            Your browser does not support canvas, please upgrade to latest browser
          </canvas>

   //to load chart.js
    var myChart = new Chart($("#barChart")[0], {
        type: 'bar',
        data: {
            labels: props.labels,
            datasets: [{
                label: props.label,
                data: props.data,
                backgroundColor: props.labels.map(l => props.color) ,
                borderColor: props.labels.map(l => props.borderColor) ,
                borderWidth: 0.01
            }]
        },
        options: {
          scales: {
            yAxes: [{
              ticks: {beginAtZero: true}
            }]
        }
       }
    });

I am using react to render 2 charts, a line chart and bar chart stack vertically. When I scroll to look at barChart below, after each update, chartjs, forces my scroll to (0,0). I have seen some implementation that does not move the scroll's position after each update.

Please help

          <canvas id="lineChart"  onLoad={ load() }>
            Your browser does not support canvas, please upgrade to latest browser
          </canvas>

          <canvas id="barChart"  onLoad={ load() }>
            Your browser does not support canvas, please upgrade to latest browser
          </canvas>

   //to load chart.js
    var myChart = new Chart($("#barChart")[0], {
        type: 'bar',
        data: {
            labels: props.labels,
            datasets: [{
                label: props.label,
                data: props.data,
                backgroundColor: props.labels.map(l => props.color) ,
                borderColor: props.labels.map(l => props.borderColor) ,
                borderWidth: 0.01
            }]
        },
        options: {
          scales: {
            yAxes: [{
              ticks: {beginAtZero: true}
            }]
        }
       }
    });
Share Improve this question asked Oct 8, 2016 at 18:21 vdj4yvdj4y 2,6692 gold badges23 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

I had a similar issue when destroying a chart and recreating it. My workaround was to get the scroll position before destruction and reapply it after creation. For example in jQuery:

var pos = $(document).scrollTop();
if (chart != undefined)
chart.destroy();

chart = new Chart(ctx, chartOptions);
$(document).scrollTop(pos);
Post a comment

comment list (0)

  1. No comments so far