简体   繁体   English

AmCharts-使用dataprovider设置最大值轴

[英]AmCharts - set max value axis using dataprovider

I'm trying to set max value axis using dataprovider, since I'm dynamically loading data in my bar chart I need to see the "progress" on one of the bars compared to the one that is supposed to be the total. 我正在尝试使用dataprovider设置最大值轴,因为我正在动态加载条形图中的数据,因此我需要查看其中一个条上的“进度”,而不是应该视为总数的条。

How do I achieve this? 我该如何实现?

I' tried with: 我尝试过:

"valueAxes": [
                {
                    "id": "ValueAxis-1",
                    "stackType": "regular",
                    "maximum": myDataProviderAttribute
                }

But no luck. 但是没有运气。

Any suggestion will be much apreciated. 任何建议都将不胜感激。

I've submited a ticket to AmCharts support and got this feedback: 我已向AmCharts支持人员提交了票证,并获得了以下反馈:

You can set max before the chart is initialized based on the data you have, inside addInitHandler. 您可以在addInitHandler内部根据所拥有的数据在图表初始化之前设置max。 Here is an example for a simple column chart: 这是一个简单柱形图的示例:

AmCharts.addInitHandler(function(chart) {

  // find data maximum:
  var min = chart.dataProvider[0].visits;
  for (var i in chart.dataProvider) {
    if (chart.dataProvider[i].visits > max) {
        max = chart.dataProvider[i].visits;
    }
  }
  // set axes max based on value above:
  chart.valueAxes[0].maximum = max  + 100;
  chart.valueAxes[0].strictMinMax = true;
});

You may need to use strictMinMax as above to enforce the value: https://docs.amcharts.com/3/javascriptcharts/ValueAxis#strictMinMax 您可能需要使用上面的strictMinMax来强制执行该值: https ://docs.amcharts.com/3/javascriptcharts/ValueAxis#strictMinMax

Example of setting minimum inside addInitHandler: 在addInitHandler中设置最小值的示例:

https://codepen.io/team/amcharts/pen/b4be8cb4e3c073909860720e0909a876?editors=1010 https://codepen.io/team/amcharts/pen/b4be8cb4e3c073909860720e0909a876?editors=1010

If you refresh the data, or use live data, then before you animate or validate the chart to show the updated data, you should recalculate the max and, if you want the axis to change then use chart.valueAxes[0].maximum = max; 如果刷新数据或使用实时数据,则在对图表进行动画处理或验证以显示更新的数据之前,应重新计算最大值,如果要更改轴,请使用chart.valueAxes [0] .maximum =最大 where max is something you calculate based on data input. 其中max是您根据数据输入计算得出的值。

Here is an example: 这是一个例子:

function loop() {
    var data = generateChartData();
    chart.valueAxes[0].maximum = max;

    // refresh data:
    chart.animateData(data, {
        duration: 1000,
        complete: function () {
            setTimeout(loop, 2000);
        }
    });
}

The lines above were used in this example: 在此示例中使用了以上几行:

https://codepen.io/team/amcharts/pen/d0d5d03cfdcc2cc256e28ec52ad8b95c/?editors=1010 https://codepen.io/team/amcharts/pen/d0d5d03cfdcc2cc256e28ec52ad8b95c/?editors=1010

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

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