Edit this page

Maintain Pan and Zoom State on Rebind

Normally, the Kendo UI Chart resets the axis range after the data source fetches new data. If users pan or zoom the Chart, they will see the initial viewport after the new data is loaded.

The example below demonstrates how to store and load the axis range. Changes are detected in the drag and zoom events. The axis range is restored in the dataBound event.

Example
    <button id="rebind">Rebind Chart</button>
    <div id="chart"></div>
    <script>
      // Sample data
      var data = [];
      for (var i = 0; i < 100; i++) {
        var val = Math.round(Math.random() * 10);
        data.push({
          category: "C" + i,
          value: val
        });
      }

      function createChart() {
        var axisMin = 0;
        var axisMax = 10;

        function updateRange(e) {
          axisMin = e.axisRanges.axis.min;
          axisMax = e.axisRanges.axis.max;
        }

        function restoreRange(e) {
          e.sender.options.categoryAxis.min = axisMin;
          e.sender.options.categoryAxis.max = axisMax;
        }

        $("#chart").kendoChart({
          renderAs: "canvas",
          dataSource: {
            data: data
          },
          categoryAxis: {
            name: "axis",
            min: axisMin,
            max: axisMax,
            labels: {
              rotation: "auto"
            }
          },
          series: [{
            type: "column",
            field: "value",
            categoryField: "category"
          }],
          pannable: {
            lock: "y"
          },
          zoomable: {
            mousewheel: {
              lock: "y"
            },
            selection: {
              lock: "y"
            }
          },
          zoom: updateRange,
          drag: updateRange,
          dataBound: restoreRange
        });
      }

      $("#rebind").click(function() {
        $("#chart").data("kendoChart").dataSource.read();
      });

      $(document).ready(createChart);
    </script>

See Also

For more runnable examples on Kendo UI Charts, browse the How To documentation folder.

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy