如何使用Chartist js相对拉伸同一张图中具有不同索引数量的两个系列?

hitautodestruct

序幕

使用Chartist,我知道有一个选项可以“填充”两个不同的数据集,这些数据集没有相同的项目数量,例如以下示例:

var chart = new Chartist.Line('.ct-chart', {
  labels: [1, 2, 3, 4, 5, 6, 7],
  series: [
    [5, 5, 10, 8, 7, 5, 4],
    [10, 15, null, 12, null, null, null]
  ]
}, {
  lineSmooth: Chartist.Interpolation.cardinal({
    fillHoles: true,
  })
});
<script src="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<link href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css" rel="stylesheet"/>
<div class="ct-chart"></div>

请注意,在该示例中,第二个系列将仅显示直到最后一个值都没有的行null(在索引4处)。

有没有办法让宪章制作相对于第一个序列的第二个序列,扩展第二个序列线以匹配第一个序列,直到最长序列的最后一个值?

用户名

您可以编写一些帮助程序来修改数据数组:

  • trimNullsnull从数组的开头和结尾删除所有值:

    [10, 15, null, 12, null, null, null] -> [10, 15, null, 12]
    
  • stretch 接受一个数组和所需的长度,然后将原始值分配给新索引:

    [10, 15, null, 12] -> 7 -> [10, null, 15, null, null, null, 12]
    
  • alignSeries 列出一系列数据,并将每个数据系列与最广泛的数据对齐。

    [ [ 1, 2, 3, 4], [1, 4], [null, 1, 4] ] -> 
      [ [ 1, 2, 3, 4], [1, null, null, 4], [1, null, null, 4] ]
    

在一个正在运行的示例中,包括旧行以供参考:

const trimNulls = (data) => {
  // Find the first non-null value
  const start = data.findIndex(y => y !== null);
  // Find the last non-null value
  const end = data.length - Array.from(data).reverse()
                                 .findIndex(y => y !== null);
  // Return the elements between those boundaries                               
  return data.slice(start, end);
}

const stretch = (data, newLength) => {
  // Create array of required number of nulls
  const stretched = Array(newLength).fill(null);
  // Determine the stretch factor
  const s = newLength / data.length;
  // For every value we want to keep, place it
  // at its new, stretched index
  data.forEach((y, x) => {
    stretched[Math.ceil(x * s)] = y;
  });
  // Return the newly created array
  return stretched;
}

// Takes a list of series and aligns their starts and ends, stretching 
// in between
const alignSeries = series => {
  // Trim each series
  const trimmed = series.map(trimNulls);
  // Find the longest one
  const maxLength = Math.max(...trimmed.map(t => t.length));
  // Stretch all series to the longest length and return
  return trimmed.map(t => stretch(t, maxLength));
};

var chart = new Chartist.Line('.ct-chart', {
  labels: [1, 2, 3, 4, 5, 6, 7],
  series: [
    ...alignSeries([
      [5, 5, 10, 8, 7, 5, 4],
      [10, 15, null, 12, null, null, null]
    ]),
    // For reference
    [10, 15, null, 12, null, null, null]
  ]
}, {
  lineSmooth: Chartist.Interpolation.cardinal({
    fillHoles: true,
  })
});
<script src="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
<link href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css" rel="stylesheet"/>
<div class="ct-chart"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Excel的同一张图中显示两个范围相同但频率不同的数据系列?

如何将具有相同架构但权重不同的两个keras模型加载到一张图中?

如何在chartist.js中使用负数

如何在chartist.js中使用插件?

PostgreSQL-它会在同一张表上使用两个索引吗?

如何从orientDB的同一张表中检索两个不同边的数据?

如何使用Seaborn在同一张图上绘制两个小提琴系列图?

如何从同一张表中查询同一列但条件不同的两个不同的总和?

如何在同一张图中用条形图表示两个时间序列之间的差异?

如何从具有不同WHERE条件的同一张表的2个不同列中减去2个值?

如何在一张图中以相同的颜色但线条样式不同绘制两个pandas DataFrame?

如何使用C和gnuplot在一张图中打印两个或多个图

如何在同一图表中在xAxis中绘制具有不同数据时间的两个系列

当两个外键指向同一张表时如何使用Factory boy

如何使用 tensorboardX 在同一张图表上获得两个标量?

如何根据同一张表的引用输出两次具有不同值的列?

如何转换MySQL查询的数组结果以在chartist.js中使用

如何在chartist.js中使用带时间戳的数据?

如何在同一行的相对两侧具有两个项目

如何使用ggplot在一张图表上为两个不同的数据框着色?

如何在R的同一张图中将不同月份绘制为不同系列

如何从Flask发送两个系列以由Chartist.js绘制

如何使用Epplus将两个具有不同类型图表的系列放置在控制图中?

如何在同一张表中的同一模板上显示来自两个不同Django模型的实例?

使用ggplot2在一张图中绘制两个单独的条形图

SQLAlchemy如何为同一张表定义两个模型

如何合并同一张表中的两个hasMany关系?

如何在一张图中显示具有不同比例轴的多个雷达图?

如何从具有不同条件的同一张表中获取行