简体   繁体   English

自定义缩放到Google折线图

[英]Custom zoom to Google line chart

I've being using the dragToZoom explorer function to add zoom functionality to my line charts. 我正在使用dragToZoom资源管理器功能为我的折线图添加缩放功能。

explorer: { 
    actions: ['dragToZoom', 'rightClickToReset'],
    axis: 'horizontal',
    keepInBounds: true,
    maxZoomIn: 4.0
}

Fiddle example here . 这里的小提琴示例。

I also wanted to add a custom zoom where the user would choose a start date and the chart would be zoomed into the period [start date; 我还想添加一个自定义缩放,用户可以选择开始日期,图表将缩放到期间[开始日期; current date]. 当前的日期]。

I saw that the Annotation Charts offer a method called setVisibleChartRange(start, end) that could do it. 我看到Annotation Charts提供了一个名为setVisibleChartRange(start, end) ,可以做到这一点。 However, I tried them on my application and they are not as view pleasing and customizable as Line Charts are (legends, borders, etc). 但是,我在我的应用程序上尝试了它们,它们不像Line Charts那样令人愉悦和可定制(传说,边框等)。

Is there any way of changing the visible range on line charts? 有没有办法改变折线图上的可见范围?

The best way to do it without using Annotation Charts was following WhiteHat's tip on the comment, adding a CharRangeFilter and changing its state. 不使用注释图表的最佳方法是遵循WhiteHat对注释的提示,添加CharRangeFilter并更改其状态。

As mentioned in Google Developers page , the draw() method needs to be called after changing the state: 如Google Developers 页面中所述 ,需要在更改状态后调用draw()方法:

The rule of thumb is to perform any change you need directly on the specific ControlWrapper (or ChartWrapper ) instance: for example, change a control option or state via setOption() and setState() respectively, and call its draw() method afterward. 经验法则是直接在特定的ControlWrapper (或ChartWrapper )实例上执行您需要的任何更改:例如,分别通过setOption()setState()更改控件选项或状态,然后调用其draw()方法。

var dash = new google.visualization.Dashboard(document.getElementById('dashboard'));

var chart = new google.visualization.ChartWrapper({
    chartType: 'ComboChart',
    containerId: 'chart_div',
    options: {
        legend: { 
          position: 'bottom', 
          alignment: 'center', 
          textStyle: {
            fontSize: 12
          }
      },
      explorer: {
          actions: ['dragToZoom', 'rightClickToReset'],
          axis: 'horizontal',
          keepInBounds: true
      },
      hAxis: {
          title: 'X'
      },
      pointSize: 3,
      vAxis: {
          title: 'Y'
      }
  }
});

var control = new google.visualization.ControlWrapper({
    controlType: 'ChartRangeFilter',
    containerId: 'control_div',
    options: {
        filterColumnIndex: 0,
        ui: {
            chartOptions: {
                height: 50,
                chartArea: {
                    width: '75%'
                }
            }
        }
    }
});

dash.bind([control], [chart]);

dash.draw(data);

// example of a new date set up
setTimeout(function () {        
    control.setState({range: {
        start: new Date(2016, 6,1),
      end: new Date()
  }});
  control.draw();
}, 3000);

I created a working example on JSFiddle . 我在JSFiddle上创建了一个工作示例。

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

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