D3条形图中数据数组的格式,值数组到对象数组中

时间:2015-08-16 19:47:49

标签: javascript d3.js multidimensional-array charts

我有一个简单的交互式条形图。大多数人都喜欢Mike Bostock的http://bl.ocks.org/mbostock/3885304,但是栏上有工具提示。

我所拥有的2d数组元素的格式为“word”字符串,频率编号如...

好,5 你好,4 再见,2 阿罗哈,1 ...

我尝试将“letter:”和“frequency:”连接到2d数组元素以遵循此代码。但它无论如何都没有用。字符串连接可能会将频率编号转换为字符串类型。所以一定要确定如何使用它。

有没有办法将字符串连接到数组元素中的数字以保留类型,尤其是数字?

或者有一种方法可以将当前数组的数据更好地加载到对象数组中吗?

我将来也会使用文件。但目前阵列感谢您的建议......

// The new data variable.
var data = [
  {letter: "A", frequency: .08167},
  {letter: "B", frequency: .01492},
  {letter: "C", frequency: .02780},
  {letter: "D", frequency: .04253},
  {letter: "E", frequency: .12702},
  {letter: "F", frequency: .02288},
  {letter: "G", frequency: .02022},
  {letter: "H", frequency: .06094},
  {letter: "I", frequency: .06973},
  {letter: "J", frequency: .00153},
  {letter: "K", frequency: .00747},
  {letter: "L", frequency: .04025},
  {letter: "M", frequency: .02517},
  {letter: "N", frequency: .06749},
  {letter: "O", frequency: .07507},
  {letter: "P", frequency: .01929},
  {letter: "Q", frequency: .00098},
  {letter: "R", frequency: .05987},
  {letter: "S", frequency: .06333},
  {letter: "T", frequency: .09056},
  {letter: "U", frequency: .02758},
  {letter: "V", frequency: .01037},
  {letter: "W", frequency: .02465},
  {letter: "X", frequency: .00150},
  {letter: "Y", frequency: .01971},
  {letter: "Z", frequency: .00074}
];

// The following code was contained in the callback function.
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);

此部分必须针对我的数组格式进行更改:

<?php
require_once('header.php');
load_header('title', 'meta description'); // loads also MENU
?>

// Unique content of the page

<?php
// footer settings
$load_contact_info = true;

// load footer
require_once('footer.php');
load_footer($load_contact_info);
?>

0 个答案:

没有答案