简体   繁体   English

Chart.js 缩放开始于放大

[英]Chart.js zoom start at zoomed in

I have built a line chart using Chart.js, and the Zoom plugin.我使用 Chart.js 和 Zoom 插件构建了折线图。 It all works well it Pans and Zooms nicely, however, the chart always loads with the complete range of data, I want it to start "Zoomed in" by n amount.这一切都很好地平移和缩放,但是,图表总是加载完整的数据范围,我希望它以 n 量开始“放大”。 I have tried to adjust the settings, however to no avail.我试图调整设置,但无济于事。 Any pointers at this stage would be much appreciated.在此阶段的任何指示将不胜感激。

ChartJS doesn't have setZoom method in-built. ChartJS 没有内置的 setZoom 方法。 Issue that sums up ChartJS zoom plugin limitations: https://github.com/chartjs/chartjs-plugin-zoom/issues/82总结 ChartJS 缩放插件限制的问题: https : //github.com/chartjs/chartjs-plugin-zoom/issues/82

Here is a hack way to send mouse wheel event to the chart and zoom:这是一种将鼠标滚轮事件发送到图表并进行缩放的黑客方法:

let e = document.createEvent('MouseEvents')
e.initEvent('wheel', true, true)
e.deltaY = -10e20;
setTimeout(() => {
  for(let i=0;i<70;i++)
    canvas.dispatchEvent(e);
}, 500);

Hope this helps.希望这可以帮助。

Finally worked this out.终于解决了这个问题。

In the scales options section you need to add a min integer when used with the zoom function this then still renders the graph outside of the canvas, ready for panning.在缩放选项部分中,当与缩放功能一起使用时,您需要添加一个最小整数,这仍然会在画布之外呈现图形,准备平移。

scales: {
         xAxes:[
                 {
                    type: 'time',
                    time: {
                    min: '2',
                    tooltipFormat: 'll',
                    displayFormats: {
                    quarter: 'MMM YYYY',
                }
            },

As long as your "Type" is time this worked for me.只要您的“类型”是对我有用的时间。

Here is a full answer to the partial solution above.这是上述部分解决方案的完整答案。

I will assume we have years of data in a time based chart.我假设我们在基于时间的图表中有多年的数据。 It is too much to see in a big chart.在大图表中看不到太多。 So we will open the chart showing only the most recent year of data.因此,我们将打开仅显示最近一年数据的图表。

You need to set chart options for both scales and the zoom plugin.您需要比例尺和缩放插件设置图表选项。

Firstly , we set the scales to be a minimum that is a date from a year ago.首先,我们将比例设置为最小值,即一年前的日期。 (like in the @user372531 answer above) (就像上面@user372531 的回答一样)

 scales: {
      x: {
          type: "time",
          min: oneYearAgo, 
       }

This will make the chart open and display the most recent year of data.这将使图表打开并显示最近一年的数据。 You don't need to set a max, it will default to your latest data.您无需设置最大值,它会默认为您的最新数据。

You should find your chart opens zoomed in to one year of data.您应该会发现您的图表打开后放大到一年的数据。 However you will not be able to zoom or pan out... yet!但是,您将无法缩放或平移……但是!

Secondly , set the widest limits in your zoom plugin options:其次,在缩放插件选项中设置最宽的限制:

zoom: {
        zoom: {
          wheel: {enabled: true,},
          mode: 'x',   
        },
        pan: {
          enabled: true,
          mode: 'x',
        },
        limits: {
          x: { min: oldestDataDate, max: newestDataDate },
        },
      },

(I've given my full zoom/pan options for controlling the horizontal time axis only) (我已经给出了仅用于控制水平时间轴的完整缩放/平移选项)

This will also load your chart much quicker.这也将更快地加载您的图表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM