从AmCharts获取范围值

时间:2018-09-06 18:50:02

标签: javascript amcharts

我有一个AmCharts正在工作,这是代码:

var chart = AmCharts.makeChart("chartdiv", {
    "type": "serial",
    "theme": "light",
    "precision":"3",
    "decimalSeparator": ",",
    "thousandsSeparator": ".",
    "legend": {
        "horizontalGap": 10,
        "maxColumns": 3,
        "position": "top",
        "useGraphSettings": false,
        "markerSize": 10,
        "marginTop": 20,
        "autoMargins":true,
        "forceWidth":true,
        "valueAlign":"right",
        "labelWidth":85,
        "valueWidth":85
    },
    "dataProvider": response,
    "synchronizeGrid":true,
    "valueAxes": [{
        "axisAlpha": 0,
        "position": "left",
        "title": "Activos"
    }],
    /*"startDuration": 0.5,*/
    "graphs": graphs,
    "chartScrollbar": {},
    "chartCursor": {
        "cursorPosition": "mouse"
    },
    "categoryField": "diahora",
    "categoryAxis": {
        /*"parseDates": true,*/
        "gridPosition": "start",
        "axisColor": "#DADADA",
        /*"minorGridEnabled": true*/
    },
    "export": {
        "enabled": true,
        "position": "top-right",
        "class": "export-main",
        "menu": [ {
            "title": "Exportar el gráfico a archivo de imagen",
            "label": "Descargar",
            "menu": [ "PNG", "JPG", "PDF" ]
        }]
    }
});

我用ajax调用它:

$.get(url, function(response){

所以,我需要获取缩放后在浏览器上呈现的图形X范围的2个值,知道吗?

例如,当图形开始时,轴x的范围是0和100。如果我缩放到某个范围(例如40和60),则需要检索这些值(40和60),因为我将使用它们用于页面中的其他内容。

那么,在每次ajax调用之后如何保存这些范围?

1 个答案:

答案 0 :(得分:1)

AmCharts提供了zoomed事件,该事件为您提供开始和结束索引/值。您可以使用它并存储所需的值,例如

AmCharts.makeChart("chartdiv", {
  // ...
  listeners: [{
    event: "zoomed",
    method: function(e) {
     //e.startIndex and e.endIndex gives you the indicies in the dataProvider
     //e.startValue and e.endValue gives you the start and end values
     //e.startDate and e.endDate gives you the start and end dates if you're using parseDates
    }
  }]
});

请注意,zoomed在初始加载时也会被调用。如果您不想捕获初始的开始/结束值,则可能需要先跟踪第一次加载,然后再将这些值存储在后续缩放事件中。