AmCharts-比较缺少值的数据集 [英] AmCharts - Compare dataSets with missing values

查看:106
本文介绍了AmCharts-比较缺少值的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AmCharts来显示两个数据集,这些数据集表示每两种文件类型(pdf和xls)的下载次数.该图表与以下位置可用的图表相似 https://www.amcharts.com/demos/multiple-data-sets/,但我将recalculateToPercents选项设置为从不",以始终显示比较系列的实际下载次数.我正在按小时计费.

I'm using AmCharts to display two datasets which represent number of downloads per two file types: pdf and xls. The chart is similar to the one available at https://www.amcharts.com/demos/multiple-data-sets/, but I set recalculateToPercents option to 'never' to always display actual downloads count for compared series. I'm using hourly period.

我遇到的问题是,在特定时间内第一个文件类型(主数据集)没有下载的情况下,即使第二个文件类型的下载量超过0,也不会显示第二个文件类型的值.第二种文件类型.

The problem I encounter is that in situations when there are no downloads in specific hour for first file type (the main dataSet), the value for second file type is not displayed either, even when there are more than 0 downloads of the second file type.

amCharts是否可以显示主数据集中不存在的日期的比较系列的值?

Is it possible for amCharts to display values of compared series for dates that are not present in the main dataSet?

推荐答案

使用此微型插件,如果在图表配置中设置了syncDataTimestamps: true,它将对其进行预处理并添加丢失的空"数据点指向主数据集,以便显示所有数据集中的所有数据点,即使它们的时间戳不重叠:

Use this mini-plugin, which if syncDataTimestamps: true is in your chart config is set, will pre-process your data and will add missing "empty" data points to main data set so that all data points from all data sets are shown, even if their timestamps do not overlap:

/**
 * amCharts plugin: sync timestamps of the data sets
 * ---------------
 * Will work only if syncDataTimestamps is set to true in chart config
 */
AmCharts.addInitHandler(function(chart) {

  // check if plugin is enabled
  if (chart.syncDataTimestamps !== true)
    return;

  // go thorugh all data sets and collect all the different timestamps
  var dates = {};
  for (var i = 0; i < chart.dataSets.length; i++) {
    var ds = chart.dataSets[i];
    for (var x = 0; x < ds.dataProvider.length; x++) {
      var date = ds.dataProvider[x][ds.categoryField];
      if (dates[date.getTime()] === undefined)
        dates[date.getTime()] = {};
      dates[date.getTime()][i] = ds.dataProvider[x];
    }
  }

  // iterate through data sets again and fill in the blanks
  for (var i = 0; i < chart.dataSets.length; i++) {
    var ds = chart.dataSets[i];
    var dp = [];
    for (var ts in dates) {
      if (!dates.hasOwnProperty(ts))
        continue;
      var row = dates[ts];
      if (row[i] === undefined) {
        row[i] = {};
        var d = new Date();
        d.setTime(ts);
        row[i][ds.categoryField] = d;
      }
      dp.push(row[i]);
    }
    dp.sort(function(a,b){
      return new Date(a[ds.categoryField]) - new Date(b[ds.categoryField]);
    });
    ds.dataProvider = dp;
  }

}, ["stock"]);

这取自此amCharts演示

这篇关于AmCharts-比较缺少值的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆