D3.js学习记录 - 数据类型【转】【新】

时间:2023-03-08 15:56:32
D3.js学习记录 - 数据类型【转】【新】

1、变量

JAVASCRIPT的变量是一种类型宽松的语言。定义变量不用指定数据类型。而且还是动态可变的。

var value = 100;
value = 99.9999;
value = false;
value = "This can't possibly work.";
value = "Argh, it does work! No errorzzzz!";

2、数组

从0开始

var percentages = [ 0.55, 0.32, 0.91 ];
var names = [ "Ernie", "Bert", "Oscar" ];

percentages[1]  //Returns 0.32
names[1]        //Returns "Bert"

3、对象

var fruit = {
    kind: "grape",
    color: "red",
    quantity: 12,
    tasty: true
};
fruit.kind      //Returns "grape"
fruit.color     //Returns "red"
fruit.quantity  //Returns 12
fruit.tasty     //Returns true

4、对象数组

var fruits = [
    {
        kind: "grape",
        color: "red",
        quantity: 12,
        tasty: true
    },
    {
        kind: "kiwi",
        color: "brown",
        quantity: 98,
        tasty: true
    },
    {
        kind: "banana",
        color: "yellow",
        quantity: 0,
        tasty: true
    }
];fruits[0].kind      ==  "grape"
fruits[0].color     ==  "red"
fruits[0].quantity  ==  12
fruits[0].tasty     ==  true

fruits[1].kind      ==  "kiwi"
fruits[1].color     ==  "brown"
fruits[1].quantity  ==  98
fruits[1].tasty     ==  true

fruits[2].kind      ==  "banana"
fruits[2].color     ==  "yellow"
fruits[2].quantity  ==  0
fruits[2].tasty     ==  true

5、JSON

JSON基本上是一个特定的语法。语法优化使用(显然)用JavaScript和AJAX请求,这就是为什么你会看到大量的基于网络的API,吐出的数据为JSON。它的速度更快,更容易比XML解析与JavaScript,D3var jsonFruit = {
    "kind": "grape",
    "color": "red",
    "quantity": 12,
    "tasty": true
};

6、GeoJSON

GeoJSON是可以存储地理空间(通常为经度/纬度坐标)点,但形状(如线和多边形)和其他空间的功能。如果你有大量的地理数据,它是值得解析它变成最好采用与D3 GeoJSON GeoJSON是格式。 var geodata = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [ 150.1282427, -24.471803 ]
            },
            "properties": {
                "type": "town"
            }
        }
    ]
};

原文链接:http://blog.csdn.net/tianxuzhang/article/details/11367251