using Indexs

27 views
Skip to first unread message

David Morrisroe

unread,
Apr 12, 2015, 3:35:26 PM4/12/15
to json...@googlegroups.com
Hi
I am working on a project that uses the json-stat webapi from the Irish Statistics office

each dataset has a number of Statistics in it in a format like

            "Statistic": {
                "label": "Statistic",
                "category": {
                    "index": {
                        "AJA01C1": 0,
                        "AJA01C2": 1
                    },
                    "label": {
                        "AJA01C1": "Cattle Price per Head (Euro)",
                        "AJA01C2": "Cattle Price per 100 Kg (Euro)"
                    },
                    "unit": {
                        "AJA01C1": {
                            "Base": "Euro"
                        },
                        "AJA01C2": {
                            "Base": "Euro"
                        }
                    }
                }
            },

each Statistic has a label such as AJA01C1
I want to access the data by the label but I can't see how to do this using the javascript
tool kit.
what I want to do is given a Statistic label I want to output a json file with all the data so I can draw graphs.
I can't find a way to do this using the javascript tool kit

I could open the file as a reguler json but I'd rather not open the dataset twice.

Thanks
David

Xavier Badosa

unread,
Apr 13, 2015, 9:50:47 AM4/13/15
to json...@googlegroups.com
First of all, thank you for letting us know that the CSO has a wonderful JSON-stat API!


> each Statistic has a label such as AJA01C1

What you ask is retrieving all the data after freezing a dimension ("Statistic") to a certain category ("AJA01C1"), or what we may call getting a "subcube" (from the original cube).

Subcubes are tricky. The JSON-stat Javascript Toolkit (JJT) method that comes closer to what you need is .Data():


I'm guessing you are using this dataset:



It has 3 dimensions (none is constant).

.Data() has being designed to retrieve a single data (448.41):

j.Dataset(0).Data({"Type of Cattle": "B200", "Year": "2000", "Statistic": "AJA01C1"}).value

but it can also retrieve a one-dimensional array, for example, the time series for type of cattle "B200" and statistic "AJA01C1" (["..", "..", 246.19, 277.69, ...]):

j.Dataset(0).Data({"Type of Cattle": "B200", "Statistic": "AJA01C1"}).map(function(a){return a.value})

So, going back to your question, you could freeze "Statistic" ("AJA01C1") and iterate by "Type of Cattle" to get the time series of cattle price per head for each type of cattle. Or you could simply iterate for type of cattle AND time, and get a single data each time.

Iterating by dimension categories is easy with JJT:

var 
    ds=j.Dataset(0),
    dtype=ds.Dimension("Type of Cattle"),
    dyear=ds.Dimension("Year"),
    type=dtype.id,
    year=dyear.id
;

for(var t=0, tlen=type.length; t<tlen; t++){
    for(var y=0, ylen=year.length; y<ylen; y++){
        console.log(
            dtype.Category(t).label + " / " +
            dyear.Category(y).label + " = " +
            ds.Data({
                "Type of Cattle": dtype.id[t],
                "Year": dyear.id[y],
                "Statistic": "AJA01C1"
            }).value
        );
    }
}

I hope this helps,

X.
Reply all
Reply to author
Forward
0 new messages