Vega-lite heatmap "transform" isn't working right - elasticsearch

1) There's a X - coordinate and Y - coordinate. There's a Z - value. Z needs to calculate AVG.
"data": {
"values": [
{"x": 0, "y": 0, "z": 0},
{"x": 1, "y": 0, "z": 1},
{"x": 2, "y": 0, "z": 5},
{"x": 2, "y": 0, "z": 10},
{"x": 4, "y": 0, "z": 16},
{"x": 0, "y": 1, "z": 1},
{"x": 1, "y": 1, "z": 2},
{"x": 2, "y": 1, "z": 5},
{"x": 3, "y": 1, "z": 10},
{"x": 5, "y": 1, "z": 26}
]},
If the data:
"encoding": {
"x": {"field": "x", "type": "ordinal", "title": "X"},
"y": {"field": "y", "type": "ordinal", "title": "Y"} },
then AVG that's right:
Vega Editor
If I use "transform" with "groupby" :
"transform": [ {"window": [{"op": "count", "field": "x", "as": "x2"}], "groupby": ["y"]}],
"encoding": {
"x": {"field": "x2", "type": "ordinal", "title": "X"},
"y": {"field": "y", "type": "ordinal", "title": "Y"}
},
then AVG it's not true:
Vega Editor

The transform is doing exactly what you're telling it to do:
{"window": [{"op": "count", "field": "x", "as": "x2"}], "groupby": ["y"]}
It groups your data by y. Schematically it looks like this:
y=0: x: [0, 1, 2, 2, 4]
z: [0, 1, 5, 10, 16]
y=1: x: [0, 1, 2, 3, 5]
z: [1, 2, 5, 10, 26]
Then it does a sliding window and assigns the count to x2:
y=0: x: [0, 1, 2, 2, 4]
z: [0, 1, 5, 10, 16]
x2: [1, 2, 3, 4, 5]
y=1: x: [0, 1, 2, 3, 5]
z: [1, 2, 5, 10, 26]
x2: [1, 2, 3, 4, 5]
As you can see, when you create a plot of y vs x2, there is now a single z value at each location, and this is correctly reflected in the chart that you show above.
If you want different behavior, you should specify a different transform.

Related

Vega-lite heatmap add text to a variable

"data": {
"values": [
{"x": 0, "y": 0, "z": 0},
{"x": 1, "y": 0, "z": 1},
{"x": 2, "y": 0, "z": 5},
{"x": 2, "y": 0, "z": 10},
{"x": 4, "y": 0, "z": 16},
{"x": 0, "y": 1, "z": 1},
{"x": 1, "y": 1, "z": 2},
{"x": 2, "y": 1, "z": 5},
{"x": 3, "y": 1, "z": 10},
{"x": 5, "y": 1, "z": 26}
]},
How to add text to a variable, so like "AVG:"?
Vega Editor
You can use a calculate transform to create specifically-formatted text to display in a text mark. For example (vega editor):
{
"data": {
"values": [
{"x": 0, "y": 0, "z": 0},
{"x": 1, "y": 0, "z": 1},
{"x": 2, "y": 0, "z": 5},
{"x": 2, "y": 0, "z": 10},
{"x": 4, "y": 0, "z": 16},
{"x": 0, "y": 1, "z": 1},
{"x": 1, "y": 1, "z": 2},
{"x": 2, "y": 1, "z": 5},
{"x": 3, "y": 1, "z": 10},
{"x": 5, "y": 1, "z": 26}
]
},
"transform": [
{
"aggregate": [{"op": "mean", "field": "z", "as": "z"}],
"groupby": ["x", "y"]
},
{"calculate": "'avg: ' + datum.z", "as": "z"}
],
"mark": "text",
"encoding": {
"x": {"field": "x", "type": "ordinal"},
"y": {"field": "y", "type": "ordinal"},
"text": {"field": "z", "type": "nominal"}
},
"width": 300,
"height": 100
}

MongoDB Too slow at find by id query, for just 100000 items?

I have a collection with 100000(999996) documents and I'm using Laravel for integration,
When I want to find a document by its id, it take too long to respond. (something like >80ms)
I'm wondering why it must take too long for just 100k items?
and _id field is indexed.
what configurations I should set for better performance.
be aware I have some embeds in my document that embedded by Laravel model.
example of one of the documents:
{
"_id": ObjectId("5d88a2857c254f0256737f43"),
"name": "blcy",
"coach": null,
"line_up": 1,
"sponsor_id": "5d131dd37c254f1d007d01f0",
"last_online": ISODate("2019-09-23T10:46:29.000Z"),
"stadium": 1,
"active_stadium": 1,
"doctor": 1,
"active_doctor": 1,
"trainer": 1,
"coach_assistant": 1,
"active_coach_assistant": 1,
"fitness_coach": 1,
"active_fitness_coach": 1,
"leader": 1,
"active_leader": 1,
"executive_manager": 1,
"active_executive_manager": 1,
"strategy": 1,
"attack_tactic": 1,
"defence_tactic": 1,
"ticket_price": 5,
"user_id": "5d88a2847c254f0256737f42",
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"kit": {
"bg_color": 10,
"fg_color": 20,
"fg": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f44")
},
"logo": {
"bg_color": 10,
"fg_color": 20,
"fg": 1,
"bg": 11,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f45")
},
"players": [
{
"name": "بردیا رادمان",
"player_number": 7,
"age": 22,
"post": "G",
"talent": 2,
"shoot": 6,
"pass": 2,
"speed": 5,
"strength": 5,
"readiness": 100,
"technique": 6,
"head": 6,
"real_power": 83,
"power": 30,
"level": 2,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f46"),
"in_lineup": true,
"position": "G"
},
{
"name": "سعید قاضی",
"player_number": 1,
"age": 19,
"post": "G",
"talent": 5,
"shoot": 2,
"pass": 2,
"speed": 6,
"strength": 5,
"readiness": 100,
"technique": 6,
"head": 2,
"real_power": 69,
"power": 23,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f47")
},
{
"name": "علیرضا چلبی",
"player_number": 47,
"age": 19,
"post": "G",
"talent": 4,
"shoot": 4,
"pass": 3,
"speed": 4,
"strength": 3,
"readiness": 100,
"technique": 4,
"head": 3,
"real_power": 60,
"power": 21,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f48")
},
{
"name": "آذین اوستا",
"player_number": 52,
"age": 25,
"post": "G",
"talent": 4,
"shoot": 5,
"pass": 6,
"speed": 2,
"strength": 6,
"readiness": 100,
"technique": 2,
"head": 4,
"real_power": 91,
"power": 25,
"level": 2,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f49")
},
{
"name": "یاسین صفوی",
"player_number": 28,
"age": 20,
"post": "D",
"talent": 4,
"shoot": 5,
"pass": 4,
"speed": 3,
"strength": 4,
"readiness": 100,
"technique": 3,
"head": 4,
"real_power": 68,
"power": 23,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f4a"),
"in_lineup": true,
"position": "D"
},
{
"name": "خشایار نوبخت",
"player_number": 36,
"age": 17,
"post": "D",
"talent": 2,
"shoot": 6,
"pass": 5,
"speed": 3,
"strength": 5,
"readiness": 100,
"technique": 3,
"head": 3,
"real_power": 75,
"power": 25,
"level": 2,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f4b"),
"in_lineup": true,
"position": "D"
},
{
"name": "شجاع سحاب",
"player_number": 18,
"age": 23,
"post": "D",
"talent": 3,
"shoot": 4,
"pass": 2,
"speed": 2,
"strength": 3,
"readiness": 100,
"technique": 5,
"head": 4,
"real_power": 53,
"power": 20,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f4c"),
"in_lineup": true,
"position": "D"
},
{
"name": "رها فهمیده",
"player_number": 10,
"age": 26,
"post": "D",
"talent": 3,
"shoot": 5,
"pass": 5,
"speed": 3,
"strength": 5,
"readiness": 100,
"technique": 5,
"head": 3,
"real_power": 76,
"power": 26,
"level": 2,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f4d"),
"in_lineup": true,
"position": "D"
},
{
"name": "آذین عبدالکریمی",
"player_number": 32,
"age": 18,
"post": "D",
"talent": 2,
"shoot": 6,
"pass": 4,
"speed": 2,
"strength": 2,
"readiness": 100,
"technique": 6,
"head": 5,
"real_power": 59,
"power": 25,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f4e"),
"in_lineup": true,
"position": "D"
},
{
"name": "محمدرضا طالقانی",
"player_number": 22,
"age": 26,
"post": "D",
"talent": 5,
"shoot": 3,
"pass": 3,
"speed": 3,
"strength": 4,
"readiness": 100,
"technique": 6,
"head": 2,
"real_power": 60,
"power": 21,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f4f")
},
{
"name": "پارسا دستغیب",
"player_number": 9,
"age": 19,
"post": "D",
"talent": 1,
"shoot": 6,
"pass": 2,
"speed": 4,
"strength": 3,
"readiness": 100,
"technique": 6,
"head": 2,
"real_power": 58,
"power": 23,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f50")
},
{
"name": "فرشید پناهیان",
"player_number": 33,
"age": 26,
"post": "M",
"talent": 2,
"shoot": 2,
"pass": 6,
"speed": 2,
"strength": 4,
"readiness": 100,
"technique": 3,
"head": 6,
"real_power": 72,
"power": 23,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f51"),
"in_lineup": true,
"position": "M"
},
{
"name": "سپهر واعظ",
"player_number": 8,
"age": 26,
"post": "M",
"talent": 4,
"shoot": 4,
"pass": 3,
"speed": 3,
"strength": 2,
"readiness": 100,
"technique": 6,
"head": 6,
"real_power": 72,
"power": 24,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f52"),
"in_lineup": true,
"position": "M"
},
{
"name": "امیررضا کوشکی",
"player_number": 3,
"age": 26,
"post": "M",
"talent": 5,
"shoot": 4,
"pass": 2,
"speed": 6,
"strength": 3,
"readiness": 100,
"technique": 3,
"head": 4,
"real_power": 62,
"power": 22,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f53"),
"in_lineup": true,
"position": "M"
},
{
"name": "پدرام جنتی",
"player_number": 29,
"age": 24,
"post": "M",
"talent": 3,
"shoot": 2,
"pass": 4,
"speed": 5,
"strength": 4,
"readiness": 100,
"technique": 2,
"head": 4,
"real_power": 63,
"power": 21,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f54")
},
{
"name": "محمد علی‌آبادی",
"player_number": 13,
"age": 17,
"post": "M",
"talent": 1,
"shoot": 2,
"pass": 5,
"speed": 3,
"strength": 3,
"readiness": 100,
"technique": 4,
"head": 4,
"real_power": 68,
"power": 21,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f55")
},
{
"name": "سهیل فانی",
"player_number": 19,
"age": 25,
"post": "M",
"talent": 1,
"shoot": 2,
"pass": 3,
"speed": 4,
"strength": 4,
"readiness": 100,
"technique": 4,
"head": 5,
"real_power": 65,
"power": 22,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f56")
},
{
"name": "شهرام گنجی",
"player_number": 24,
"age": 17,
"post": "S",
"talent": 4,
"shoot": 6,
"pass": 3,
"speed": 6,
"strength": 2,
"readiness": 100,
"technique": 2,
"head": 2,
"real_power": 69,
"power": 21,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f57"),
"in_lineup": true,
"position": "S"
},
{
"name": "امیرمحمد سرافراز",
"player_number": 2,
"age": 21,
"post": "S",
"talent": 4,
"shoot": 6,
"pass": 2,
"speed": 5,
"strength": 4,
"readiness": 100,
"technique": 5,
"head": 5,
"real_power": 90,
"power": 27,
"level": 2,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f58"),
"in_lineup": true,
"position": "S"
},
{
"name": "فربد ارسباران",
"player_number": 20,
"age": 25,
"post": "S",
"talent": 2,
"shoot": 2,
"pass": 2,
"speed": 5,
"strength": 3,
"readiness": 100,
"technique": 4,
"head": 4,
"real_power": 65,
"power": 20,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f59")
},
{
"name": "فرشید الهام",
"player_number": 21,
"age": 24,
"post": "S",
"talent": 2,
"shoot": 4,
"pass": 3,
"speed": 6,
"strength": 2,
"readiness": 100,
"technique": 3,
"head": 6,
"real_power": 78,
"power": 24,
"level": 2,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f5a")
},
{
"name": "ساسان طریقت",
"player_number": 35,
"age": 17,
"post": "S",
"talent": 5,
"shoot": 2,
"pass": 5,
"speed": 4,
"strength": 2,
"readiness": 100,
"technique": 5,
"head": 6,
"real_power": 74,
"power": 24,
"level": 1,
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f5b")
}
],
"league_id": "5d88a2857c254f0256737f5d",
"league_name": "league-0-1",
"transactions": [
{
"amount": 120000,
"type": "coin",
"cash": 0,
"coin": 120000,
"description": "1",
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f5e")
},
{
"amount": 100,
"type": "cash",
"cash": 100,
"coin": 120000,
"description": "1",
"updated_at": ISODate("2019-09-23T10:46:29.000Z"),
"created_at": ISODate("2019-09-23T10:46:29.000Z"),
"_id": ObjectId("5d88a2857c254f0256737f5f")
}
],
"cash": 100,
"coin": 120000,
"fixtures": [
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f025673800f",
"guest_id": "5d88a2857c254f0256737f43",
"week_day": 1,
"weather_type": "sunny",
"home_goal": 0,
"guest_goal": 0,
"friendly": false,
"fixture_id": "5d89dff87c254f36ee3ae9ab",
"updated_at": ISODate("2019-09-24T09:20:56.000Z"),
"created_at": ISODate("2019-09-24T09:20:56.000Z"),
"_id": ObjectId("5d89dff87c254f36ee3ae9ad")
},
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f0256737f61",
"guest_id": "5d88a2857c254f0256737f43",
"week_day": 2,
"weather_type": "rainy",
"home_goal": 0,
"guest_goal": 0,
"friendly": false,
"fixture_id": "5d89dff87c254f36ee3ae9c0",
"updated_at": ISODate("2019-09-24T09:20:56.000Z"),
"created_at": ISODate("2019-09-24T09:20:56.000Z"),
"_id": ObjectId("5d89dff87c254f36ee3ae9c2")
},
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f0256737f43",
"guest_id": "5d88a2857c254f0256737f7e",
"week_day": 3,
"weather_type": "sunny",
"home_goal": 0,
"guest_goal": 0,
"friendly": false,
"fixture_id": "5d89dff87c254f36ee3ae9c6",
"updated_at": ISODate("2019-09-24T09:20:56.000Z"),
"created_at": ISODate("2019-09-24T09:20:56.000Z"),
"_id": ObjectId("5d89dff87c254f36ee3ae9c7")
},
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f0256737f9b",
"guest_id": "5d88a2857c254f0256737f43",
"week_day": 4,
"weather_type": "sunny",
"home_goal": 0,
"guest_goal": 0,
"friendly": false,
"fixture_id": "5d89dff87c254f36ee3ae9d5",
"updated_at": ISODate("2019-09-24T09:20:56.000Z"),
"created_at": ISODate("2019-09-24T09:20:56.000Z"),
"_id": ObjectId("5d89dff87c254f36ee3ae9d7")
},
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f0256737f43",
"guest_id": "5d88a2857c254f0256737fb8",
"week_day": 5,
"weather_type": "rainy",
"home_goal": null,
"guest_goal": null,
"friendly": false,
"fixture_id": "5d89dff87c254f36ee3ae9e1",
"updated_at": ISODate("2019-09-24T09:20:57.000Z"),
"created_at": ISODate("2019-09-24T09:20:57.000Z"),
"_id": ObjectId("5d89dff97c254f36ee3ae9e2")
},
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f0256737fd5",
"guest_id": "5d88a2857c254f0256737f43",
"week_day": 6,
"weather_type": "sunny",
"home_goal": null,
"guest_goal": null,
"friendly": false,
"fixture_id": "5d89dff97c254f36ee3ae9ea",
"updated_at": ISODate("2019-09-24T09:20:57.000Z"),
"created_at": ISODate("2019-09-24T09:20:57.000Z"),
"_id": ObjectId("5d89dff97c254f36ee3ae9ec")
},
{
"league_name": "league-0-1",
"home_id": "5d88a2857c254f0256737f43",
"guest_id": "5d88a2857c254f0256737ff2",
"week_day": 7,
"weather_type": "sunny",
"home_goal": null,
"guest_goal": null,
"friendly": false,
"fixture_id": "5d89dff97c254f36ee3ae9fc",
"updated_at": ISODate("2019-09-24T09:20:57.000Z"),
"created_at": ISODate("2019-09-24T09:20:57.000Z"),
"_id": ObjectId("5d89dff97c254f36ee3ae9fd")
}
]
}
Finding a team like this:
$team = Team::find($team['team_id']);
EDITED:
chunk methods and also cant help me cause i just want to find one document not getting all documents in one row.
Thanks.

Label only 10 N in D3 graph nodes

I have a force-directed graph in D3.js similar to this example.
With the data source for nodes having an additional key:value called connect. I only want to display node labels that have the top 10 count by the connect value. How should I go about it?
Thank you!
The associated sample data source this as below.
{
"nodes": [
{"name": "Myriel", "company": 1, "connect":10},
{"name": "Napoleon", "company": 1, "connect":1},
{"name": "Mlle.Baptistine", "company": 1, "connect":3},
{"name": "Mme.Magloire", "company": 1, "connect":3},
{"name": "CountessdeLo", "company": 1, "connect":1},
{"name": "Geborand", "company": 1, "connect":1},
{"name": "Champtercier", "company": 1, "connect":1},
{"name": "Cravatte", "company": 1, "connect":1},
{"name": "Count", "company": 1, "connect":1},
{"name": "OldMan", "company": 1, "connect":1},
{"name": "Labarre", "company": 2, "connect":1},
{"name": "Valjean", "company": 2, "connect":1},
{"name": "Marguerite", "company": 3, "connect":3},
{"name": "Mme.deR", "company": 2, "connect":1},
{"name": "Isabeau", "company": 2, "connect":1},
{"name": "Gervais", "company": 2, "connect":1},
{"name": "Tholomyes", "company": 3, "connect":1},
{"name": "Listolier", "company": 3, "connect":1},
{"name": "Fameuil", "company": 3, "connect":1},
{"name": "Blacheville", "company": 3, "connect":1},
{"name": "Favourite", "company": 3, "connect":1},
{"name": "Dahlia", "company": 3, "connect":1},
{"name": "Zephine", "company": 3, "connect":1},
{"name": "Fantine", "company": 3, "connect":1},
{"name": "Mme.Thenardier", "company": 4, "connect":1},
{"name": "Thenardier", "company": 4, "connect":1},
{"name": "Cosette", "company": 5, "connect":1},
{"name": "Javert", "company": 4, "connect":1},
{"name": "Fauchelevent", "company": 0, "connect":1},
{"name": "Bamatabois", "company": 2, "connect":1},
{"name": "Perpetue", "company": 3, "connect":1},
{"name": "Simplice", "company": 2, "connect":1},
{"name": "Scaufflaire", "company": 2, "connect":1},
{"name": "Woman1", "company": 2, "connect":1},
{"name": "Judge", "company": 2, "connect":1},
{"name": "Champmathieu", "company": 2, "connect":1},
{"name": "Brevet", "company": 2, "connect":1},
{"name": "Chenildieu", "company": 2, "connect":1},
{"name": "Cochepaille", "company": 2, "connect":1},
{"name": "Pontmercy", "company": 4, "connect":1},
{"name": "Boulatruelle", "company": 6, "connect":1},
{"name": "Eponine", "company": 4, "connect":1},
{"name": "Anzelma", "company": 4, "connect":1},
{"name": "Woman2", "company": 5, "connect":1},
{"name": "MotherInnocent", "company": 0, "connect":1},
{"name": "Gribier", "company": 0, "connect":1},
{"name": "Jondrette", "company": 7, "connect":1},
{"name": "Mme.Burgon", "company": 7, "connect":1},
{"name": "Gavroche", "company": 8, "connect":1},
{"name": "Gillenormand", "company": 5, "connect":1},
{"name": "Magnon", "company": 5, "connect":1},
{"name": "Mlle.Gillenormand", "company": 5, "connect":5},
{"name": "Mme.Pontmercy", "company": 5, "connect":1},
{"name": "Mlle.Vaubois", "company": 5, "connect":1},
{"name": "Lt.Gillenormand", "company": 5, "connect":1},
{"name": "Marius", "company": 8, "connect":1},
{"name": "BaronessT", "company": 5, "connect":1},
{"name": "Mabeuf", "company": 8, "connect":1},
{"name": "Enjolras", "company": 8, "connect":1},
{"name": "Combeferre", "company": 8, "connect":1},
{"name": "Prouvaire", "company": 8, "connect":1},
{"name": "Feuilly", "company": 8, "connect":1},
{"name": "Courfeyrac", "company": 8, "connect":1},
{"name": "Bahorel", "company": 8, "connect":1},
{"name": "Bossuet", "company": 8, "connect":1},
{"name": "Joly", "company": 8, "connect":1},
{"name": "Grantaire", "company": 8, "connect":1},
{"name": "MotherPlutarch", "company": 9, "connect":1},
{"name": "Gueulemer", "company": 4, "connect":1},
{"name": "Babet", "company": 4, "connect":1},
{"name": "Claquesous", "company": 4, "connect":1},
{"name": "Montparnasse", "company": 4, "connect":1},
{"name": "Toussaint", "company": 5, "connect":1},
{"name": "Child1", "company": 10, "connect":1},
{"name": "Child2", "company": 10, "connect":1},
{"name": "Brujon", "company": 4, "connect":1},
{"name": "Mme.Hucheloup", "company": 8, "connect":1}
],
"links": [
{"source": 1, "target": 0, "value": 1},
{"source": 2, "target": 0, "value": 8},
{"source": 3, "target": 0, "value": 10},
{"source": 3, "target": 2, "value": 6},
{"source": 4, "target": 0, "value": 1},
{"source": 5, "target": 0, "value": 1},
{"source": 6, "target": 0, "value": 1},
{"source": 7, "target": 0, "value": 1},
{"source": 8, "target": 0, "value": 2},
{"source": 9, "target": 0, "value": 1},
{"source": 11, "target": 10, "value": 1},
{"source": 11, "target": 3, "value": 3},
{"source": 11, "target": 2, "value": 3},
{"source": 11, "target": 0, "value": 5},
{"source": 12, "target": 11, "value": 1},
{"source": 13, "target": 11, "value": 1},
{"source": 14, "target": 11, "value": 1},
{"source": 15, "target": 11, "value": 1},
{"source": 17, "target": 16, "value": 4},
{"source": 18, "target": 16, "value": 4},
{"source": 18, "target": 17, "value": 4},
{"source": 19, "target": 16, "value": 4},
{"source": 19, "target": 17, "value": 4},
{"source": 19, "target": 18, "value": 4},
{"source": 20, "target": 16, "value": 3},
{"source": 20, "target": 17, "value": 3},
{"source": 20, "target": 18, "value": 3},
{"source": 20, "target": 19, "value": 4},
{"source": 21, "target": 16, "value": 3},
{"source": 21, "target": 17, "value": 3},
{"source": 21, "target": 18, "value": 3},
{"source": 21, "target": 19, "value": 3},
{"source": 21, "target": 20, "value": 5},
{"source": 22, "target": 16, "value": 3},
{"source": 22, "target": 17, "value": 3},
{"source": 22, "target": 18, "value": 3},
{"source": 22, "target": 19, "value": 3},
{"source": 22, "target": 20, "value": 4},
{"source": 22, "target": 21, "value": 4},
{"source": 23, "target": 16, "value": 3},
{"source": 23, "target": 17, "value": 3},
{"source": 23, "target": 18, "value": 3},
{"source": 23, "target": 19, "value": 3},
{"source": 23, "target": 20, "value": 4},
{"source": 23, "target": 21, "value": 4},
{"source": 23, "target": 22, "value": 4},
{"source": 23, "target": 12, "value": 2},
{"source": 23, "target": 11, "value": 9},
{"source": 24, "target": 23, "value": 2},
{"source": 24, "target": 11, "value": 7},
{"source": 25, "target": 24, "value": 13},
{"source": 25, "target": 23, "value": 1},
{"source": 25, "target": 11, "value": 12},
{"source": 26, "target": 24, "value": 4},
{"source": 26, "target": 11, "value": 31},
{"source": 26, "target": 16, "value": 1},
{"source": 26, "target": 25, "value": 1},
{"source": 27, "target": 11, "value": 17},
{"source": 27, "target": 23, "value": 5},
{"source": 27, "target": 25, "value": 5},
{"source": 27, "target": 24, "value": 1},
{"source": 27, "target": 26, "value": 1},
{"source": 28, "target": 11, "value": 8},
{"source": 28, "target": 27, "value": 1},
{"source": 29, "target": 23, "value": 1},
{"source": 29, "target": 27, "value": 1},
{"source": 29, "target": 11, "value": 2},
{"source": 30, "target": 23, "value": 1},
{"source": 31, "target": 30, "value": 2},
{"source": 31, "target": 11, "value": 3},
{"source": 31, "target": 23, "value": 2},
{"source": 31, "target": 27, "value": 1},
{"source": 32, "target": 11, "value": 1},
{"source": 33, "target": 11, "value": 2},
{"source": 33, "target": 27, "value": 1},
{"source": 34, "target": 11, "value": 3},
{"source": 34, "target": 29, "value": 2},
{"source": 35, "target": 11, "value": 3},
{"source": 35, "target": 34, "value": 3},
{"source": 35, "target": 29, "value": 2},
{"source": 36, "target": 34, "value": 2},
{"source": 36, "target": 35, "value": 2},
{"source": 36, "target": 11, "value": 2},
{"source": 36, "target": 29, "value": 1},
{"source": 37, "target": 34, "value": 2},
{"source": 37, "target": 35, "value": 2},
{"source": 37, "target": 36, "value": 2},
{"source": 37, "target": 11, "value": 2},
{"source": 37, "target": 29, "value": 1},
{"source": 38, "target": 34, "value": 2},
{"source": 38, "target": 35, "value": 2},
{"source": 38, "target": 36, "value": 2},
{"source": 38, "target": 37, "value": 2},
{"source": 38, "target": 11, "value": 2},
{"source": 38, "target": 29, "value": 1},
{"source": 39, "target": 25, "value": 1},
{"source": 40, "target": 25, "value": 1},
{"source": 41, "target": 24, "value": 2},
{"source": 41, "target": 25, "value": 3},
{"source": 42, "target": 41, "value": 2},
{"source": 42, "target": 25, "value": 2},
{"source": 42, "target": 24, "value": 1},
{"source": 43, "target": 11, "value": 3},
{"source": 43, "target": 26, "value": 1},
{"source": 43, "target": 27, "value": 1},
{"source": 44, "target": 28, "value": 3},
{"source": 44, "target": 11, "value": 1},
{"source": 45, "target": 28, "value": 2},
{"source": 47, "target": 46, "value": 1},
{"source": 48, "target": 47, "value": 2},
{"source": 48, "target": 25, "value": 1},
{"source": 48, "target": 27, "value": 1},
{"source": 48, "target": 11, "value": 1},
{"source": 49, "target": 26, "value": 3},
{"source": 49, "target": 11, "value": 2},
{"source": 50, "target": 49, "value": 1},
{"source": 50, "target": 24, "value": 1},
{"source": 51, "target": 49, "value": 9},
{"source": 51, "target": 26, "value": 2},
{"source": 51, "target": 11, "value": 2},
{"source": 52, "target": 51, "value": 1},
{"source": 52, "target": 39, "value": 1},
{"source": 53, "target": 51, "value": 1},
{"source": 54, "target": 51, "value": 2},
{"source": 54, "target": 49, "value": 1},
{"source": 54, "target": 26, "value": 1},
{"source": 55, "target": 51, "value": 6},
{"source": 55, "target": 49, "value": 12},
{"source": 55, "target": 39, "value": 1},
{"source": 55, "target": 54, "value": 1},
{"source": 55, "target": 26, "value": 21},
{"source": 55, "target": 11, "value": 19},
{"source": 55, "target": 16, "value": 1},
{"source": 55, "target": 25, "value": 2},
{"source": 55, "target": 41, "value": 5},
{"source": 55, "target": 48, "value": 4},
{"source": 56, "target": 49, "value": 1},
{"source": 56, "target": 55, "value": 1},
{"source": 57, "target": 55, "value": 1},
{"source": 57, "target": 41, "value": 1},
{"source": 57, "target": 48, "value": 1},
{"source": 58, "target": 55, "value": 7},
{"source": 58, "target": 48, "value": 7},
{"source": 58, "target": 27, "value": 6},
{"source": 58, "target": 57, "value": 1},
{"source": 58, "target": 11, "value": 4},
{"source": 59, "target": 58, "value": 15},
{"source": 59, "target": 55, "value": 5},
{"source": 59, "target": 48, "value": 6},
{"source": 59, "target": 57, "value": 2},
{"source": 60, "target": 48, "value": 1},
{"source": 60, "target": 58, "value": 4},
{"source": 60, "target": 59, "value": 2},
{"source": 61, "target": 48, "value": 2},
{"source": 61, "target": 58, "value": 6},
{"source": 61, "target": 60, "value": 2},
{"source": 61, "target": 59, "value": 5},
{"source": 61, "target": 57, "value": 1},
{"source": 61, "target": 55, "value": 1},
{"source": 62, "target": 55, "value": 9},
{"source": 62, "target": 58, "value": 17},
{"source": 62, "target": 59, "value": 13},
{"source": 62, "target": 48, "value": 7},
{"source": 62, "target": 57, "value": 2},
{"source": 62, "target": 41, "value": 1},
{"source": 62, "target": 61, "value": 6},
{"source": 62, "target": 60, "value": 3},
{"source": 63, "target": 59, "value": 5},
{"source": 63, "target": 48, "value": 5},
{"source": 63, "target": 62, "value": 6},
{"source": 63, "target": 57, "value": 2},
{"source": 63, "target": 58, "value": 4},
{"source": 63, "target": 61, "value": 3},
{"source": 63, "target": 60, "value": 2},
{"source": 63, "target": 55, "value": 1},
{"source": 64, "target": 55, "value": 5},
{"source": 64, "target": 62, "value": 12},
{"source": 64, "target": 48, "value": 5},
{"source": 64, "target": 63, "value": 4},
{"source": 64, "target": 58, "value": 10},
{"source": 64, "target": 61, "value": 6},
{"source": 64, "target": 60, "value": 2},
{"source": 64, "target": 59, "value": 9},
{"source": 64, "target": 57, "value": 1},
{"source": 64, "target": 11, "value": 1},
{"source": 65, "target": 63, "value": 5},
{"source": 65, "target": 64, "value": 7},
{"source": 65, "target": 48, "value": 3},
{"source": 65, "target": 62, "value": 5},
{"source": 65, "target": 58, "value": 5},
{"source": 65, "target": 61, "value": 5},
{"source": 65, "target": 60, "value": 2},
{"source": 65, "target": 59, "value": 5},
{"source": 65, "target": 57, "value": 1},
{"source": 65, "target": 55, "value": 2},
{"source": 66, "target": 64, "value": 3},
{"source": 66, "target": 58, "value": 3},
{"source": 66, "target": 59, "value": 1},
{"source": 66, "target": 62, "value": 2},
{"source": 66, "target": 65, "value": 2},
{"source": 66, "target": 48, "value": 1},
{"source": 66, "target": 63, "value": 1},
{"source": 66, "target": 61, "value": 1},
{"source": 66, "target": 60, "value": 1},
{"source": 67, "target": 57, "value": 3},
{"source": 68, "target": 25, "value": 5},
{"source": 68, "target": 11, "value": 1},
{"source": 68, "target": 24, "value": 1},
{"source": 68, "target": 27, "value": 1},
{"source": 68, "target": 48, "value": 1},
{"source": 68, "target": 41, "value": 1},
{"source": 69, "target": 25, "value": 6},
{"source": 69, "target": 68, "value": 6},
{"source": 69, "target": 11, "value": 1},
{"source": 69, "target": 24, "value": 1},
{"source": 69, "target": 27, "value": 2},
{"source": 69, "target": 48, "value": 1},
{"source": 69, "target": 41, "value": 1},
{"source": 70, "target": 25, "value": 4},
{"source": 70, "target": 69, "value": 4},
{"source": 70, "target": 68, "value": 4},
{"source": 70, "target": 11, "value": 1},
{"source": 70, "target": 24, "value": 1},
{"source": 70, "target": 27, "value": 1},
{"source": 70, "target": 41, "value": 1},
{"source": 70, "target": 58, "value": 1},
{"source": 71, "target": 27, "value": 1},
{"source": 71, "target": 69, "value": 2},
{"source": 71, "target": 68, "value": 2},
{"source": 71, "target": 70, "value": 2},
{"source": 71, "target": 11, "value": 1},
{"source": 71, "target": 48, "value": 1},
{"source": 71, "target": 41, "value": 1},
{"source": 71, "target": 25, "value": 1},
{"source": 72, "target": 26, "value": 2},
{"source": 72, "target": 27, "value": 1},
{"source": 72, "target": 11, "value": 1},
{"source": 73, "target": 48, "value": 2},
{"source": 74, "target": 48, "value": 2},
{"source": 74, "target": 73, "value": 3},
{"source": 75, "target": 69, "value": 3},
{"source": 75, "target": 68, "value": 3},
{"source": 75, "target": 25, "value": 3},
{"source": 75, "target": 48, "value": 1},
{"source": 75, "target": 41, "value": 1},
{"source": 75, "target": 70, "value": 1},
{"source": 75, "target": 71, "value": 1},
{"source": 76, "target": 64, "value": 1},
{"source": 76, "target": 65, "value": 1},
{"source": 76, "target": 66, "value": 1},
{"source": 76, "target": 63, "value": 1},
{"source": 76, "target": 62, "value": 1},
{"source": 76, "target": 48, "value": 1},
{"source": 76, "target": 58, "value": 1}
]
}
Here is a modified version of Mike Bostock's example which only displays labels of top 10 nodes:
var width = 960,
height = 500
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(0.05)
.distance(100)
.charge(-100)
.size([width, height]);
var json = {"nodes":[{"name":"Myriel","group":1},{"name":"Napoleon","group":1},{"name":"Mlle.Baptistine","group":1},{"name":"Mme.Magloire","group":1},{"name":"CountessdeLo","group":1},{"name":"Geborand","group":1},{"name":"Champtercier","group":1},{"name":"Cravatte","group":1},{"name":"Count","group":1},{"name":"OldMan","group":1},{"name":"Labarre","group":2},{"name":"Valjean","group":2},{"name":"Marguerite","group":3},{"name":"Mme.deR","group":2},{"name":"Isabeau","group":2},{"name":"Gervais","group":2},{"name":"Tholomyes","group":3},{"name":"Listolier","group":3},{"name":"Fameuil","group":3},{"name":"Blacheville","group":3},{"name":"Favourite","group":3},{"name":"Dahlia","group":3},{"name":"Zephine","group":3},{"name":"Fantine","group":3},{"name":"Mme.Thenardier","group":4},{"name":"Thenardier","group":4},{"name":"Cosette","group":5},{"name":"Javert","group":4},{"name":"Fauchelevent","group":0},{"name":"Bamatabois","group":2},{"name":"Perpetue","group":3},{"name":"Simplice","group":2},{"name":"Scaufflaire","group":2},{"name":"Woman1","group":2},{"name":"Judge","group":2},{"name":"Champmathieu","group":2},{"name":"Brevet","group":2},{"name":"Chenildieu","group":2},{"name":"Cochepaille","group":2},{"name":"Pontmercy","group":4},{"name":"Boulatruelle","group":6},{"name":"Eponine","group":4},{"name":"Anzelma","group":4},{"name":"Woman2","group":5},{"name":"MotherInnocent","group":0},{"name":"Gribier","group":0},{"name":"Jondrette","group":7},{"name":"Mme.Burgon","group":7},{"name":"Gavroche","group":8},{"name":"Gillenormand","group":5},{"name":"Magnon","group":5},{"name":"Mlle.Gillenormand","group":5},{"name":"Mme.Pontmercy","group":5},{"name":"Mlle.Vaubois","group":5},{"name":"Lt.Gillenormand","group":5},{"name":"Marius","group":8},{"name":"BaronessT","group":5},{"name":"Mabeuf","group":8},{"name":"Enjolras","group":8},{"name":"Combeferre","group":8},{"name":"Prouvaire","group":8},{"name":"Feuilly","group":8},{"name":"Courfeyrac","group":8},{"name":"Bahorel","group":8},{"name":"Bossuet","group":8},{"name":"Joly","group":8},{"name":"Grantaire","group":8},{"name":"MotherPlutarch","group":9},{"name":"Gueulemer","group":4},{"name":"Babet","group":4},{"name":"Claquesous","group":4},{"name":"Montparnasse","group":4},{"name":"Toussaint","group":5},{"name":"Child1","group":10},{"name":"Child2","group":10},{"name":"Brujon","group":4},{"name":"Mme.Hucheloup","group":8}],"links":[{"source":1,"target":0,"value":1},{"source":2,"target":0,"value":8},{"source":3,"target":0,"value":10},{"source":3,"target":2,"value":6},{"source":4,"target":0,"value":1},{"source":5,"target":0,"value":1},{"source":6,"target":0,"value":1},{"source":7,"target":0,"value":1},{"source":8,"target":0,"value":2},{"source":9,"target":0,"value":1},{"source":11,"target":10,"value":1},{"source":11,"target":3,"value":3},{"source":11,"target":2,"value":3},{"source":11,"target":0,"value":5},{"source":12,"target":11,"value":1},{"source":13,"target":11,"value":1},{"source":14,"target":11,"value":1},{"source":15,"target":11,"value":1},{"source":17,"target":16,"value":4},{"source":18,"target":16,"value":4},{"source":18,"target":17,"value":4},{"source":19,"target":16,"value":4},{"source":19,"target":17,"value":4},{"source":19,"target":18,"value":4},{"source":20,"target":16,"value":3},{"source":20,"target":17,"value":3},{"source":20,"target":18,"value":3},{"source":20,"target":19,"value":4},{"source":21,"target":16,"value":3},{"source":21,"target":17,"value":3},{"source":21,"target":18,"value":3},{"source":21,"target":19,"value":3},{"source":21,"target":20,"value":5},{"source":22,"target":16,"value":3},{"source":22,"target":17,"value":3},{"source":22,"target":18,"value":3},{"source":22,"target":19,"value":3},{"source":22,"target":20,"value":4},{"source":22,"target":21,"value":4},{"source":23,"target":16,"value":3},{"source":23,"target":17,"value":3},{"source":23,"target":18,"value":3},{"source":23,"target":19,"value":3},{"source":23,"target":20,"value":4},{"source":23,"target":21,"value":4},{"source":23,"target":22,"value":4},{"source":23,"target":12,"value":2},{"source":23,"target":11,"value":9},{"source":24,"target":23,"value":2},{"source":24,"target":11,"value":7},{"source":25,"target":24,"value":13},{"source":25,"target":23,"value":1},{"source":25,"target":11,"value":12},{"source":26,"target":24,"value":4},{"source":26,"target":11,"value":31},{"source":26,"target":16,"value":1},{"source":26,"target":25,"value":1},{"source":27,"target":11,"value":17},{"source":27,"target":23,"value":5},{"source":27,"target":25,"value":5},{"source":27,"target":24,"value":1},{"source":27,"target":26,"value":1},{"source":28,"target":11,"value":8},{"source":28,"target":27,"value":1},{"source":29,"target":23,"value":1},{"source":29,"target":27,"value":1},{"source":29,"target":11,"value":2},{"source":30,"target":23,"value":1},{"source":31,"target":30,"value":2},{"source":31,"target":11,"value":3},{"source":31,"target":23,"value":2},{"source":31,"target":27,"value":1},{"source":32,"target":11,"value":1},{"source":33,"target":11,"value":2},{"source":33,"target":27,"value":1},{"source":34,"target":11,"value":3},{"source":34,"target":29,"value":2},{"source":35,"target":11,"value":3},{"source":35,"target":34,"value":3},{"source":35,"target":29,"value":2},{"source":36,"target":34,"value":2},{"source":36,"target":35,"value":2},{"source":36,"target":11,"value":2},{"source":36,"target":29,"value":1},{"source":37,"target":34,"value":2},{"source":37,"target":35,"value":2},{"source":37,"target":36,"value":2},{"source":37,"target":11,"value":2},{"source":37,"target":29,"value":1},{"source":38,"target":34,"value":2},{"source":38,"target":35,"value":2},{"source":38,"target":36,"value":2},{"source":38,"target":37,"value":2},{"source":38,"target":11,"value":2},{"source":38,"target":29,"value":1},{"source":39,"target":25,"value":1},{"source":40,"target":25,"value":1},{"source":41,"target":24,"value":2},{"source":41,"target":25,"value":3},{"source":42,"target":41,"value":2},{"source":42,"target":25,"value":2},{"source":42,"target":24,"value":1},{"source":43,"target":11,"value":3},{"source":43,"target":26,"value":1},{"source":43,"target":27,"value":1},{"source":44,"target":28,"value":3},{"source":44,"target":11,"value":1},{"source":45,"target":28,"value":2},{"source":47,"target":46,"value":1},{"source":48,"target":47,"value":2},{"source":48,"target":25,"value":1},{"source":48,"target":27,"value":1},{"source":48,"target":11,"value":1},{"source":49,"target":26,"value":3},{"source":49,"target":11,"value":2},{"source":50,"target":49,"value":1},{"source":50,"target":24,"value":1},{"source":51,"target":49,"value":9},{"source":51,"target":26,"value":2},{"source":51,"target":11,"value":2},{"source":52,"target":51,"value":1},{"source":52,"target":39,"value":1},{"source":53,"target":51,"value":1},{"source":54,"target":51,"value":2},{"source":54,"target":49,"value":1},{"source":54,"target":26,"value":1},{"source":55,"target":51,"value":6},{"source":55,"target":49,"value":12},{"source":55,"target":39,"value":1},{"source":55,"target":54,"value":1},{"source":55,"target":26,"value":21},{"source":55,"target":11,"value":19},{"source":55,"target":16,"value":1},{"source":55,"target":25,"value":2},{"source":55,"target":41,"value":5},{"source":55,"target":48,"value":4},{"source":56,"target":49,"value":1},{"source":56,"target":55,"value":1},{"source":57,"target":55,"value":1},{"source":57,"target":41,"value":1},{"source":57,"target":48,"value":1},{"source":58,"target":55,"value":7},{"source":58,"target":48,"value":7},{"source":58,"target":27,"value":6},{"source":58,"target":57,"value":1},{"source":58,"target":11,"value":4},{"source":59,"target":58,"value":15},{"source":59,"target":55,"value":5},{"source":59,"target":48,"value":6},{"source":59,"target":57,"value":2},{"source":60,"target":48,"value":1},{"source":60,"target":58,"value":4},{"source":60,"target":59,"value":2},{"source":61,"target":48,"value":2},{"source":61,"target":58,"value":6},{"source":61,"target":60,"value":2},{"source":61,"target":59,"value":5},{"source":61,"target":57,"value":1},{"source":61,"target":55,"value":1},{"source":62,"target":55,"value":9},{"source":62,"target":58,"value":17},{"source":62,"target":59,"value":13},{"source":62,"target":48,"value":7},{"source":62,"target":57,"value":2},{"source":62,"target":41,"value":1},{"source":62,"target":61,"value":6},{"source":62,"target":60,"value":3},{"source":63,"target":59,"value":5},{"source":63,"target":48,"value":5},{"source":63,"target":62,"value":6},{"source":63,"target":57,"value":2},{"source":63,"target":58,"value":4},{"source":63,"target":61,"value":3},{"source":63,"target":60,"value":2},{"source":63,"target":55,"value":1},{"source":64,"target":55,"value":5},{"source":64,"target":62,"value":12},{"source":64,"target":48,"value":5},{"source":64,"target":63,"value":4},{"source":64,"target":58,"value":10},{"source":64,"target":61,"value":6},{"source":64,"target":60,"value":2},{"source":64,"target":59,"value":9},{"source":64,"target":57,"value":1},{"source":64,"target":11,"value":1},{"source":65,"target":63,"value":5},{"source":65,"target":64,"value":7},{"source":65,"target":48,"value":3},{"source":65,"target":62,"value":5},{"source":65,"target":58,"value":5},{"source":65,"target":61,"value":5},{"source":65,"target":60,"value":2},{"source":65,"target":59,"value":5},{"source":65,"target":57,"value":1},{"source":65,"target":55,"value":2},{"source":66,"target":64,"value":3},{"source":66,"target":58,"value":3},{"source":66,"target":59,"value":1},{"source":66,"target":62,"value":2},{"source":66,"target":65,"value":2},{"source":66,"target":48,"value":1},{"source":66,"target":63,"value":1},{"source":66,"target":61,"value":1},{"source":66,"target":60,"value":1},{"source":67,"target":57,"value":3},{"source":68,"target":25,"value":5},{"source":68,"target":11,"value":1},{"source":68,"target":24,"value":1},{"source":68,"target":27,"value":1},{"source":68,"target":48,"value":1},{"source":68,"target":41,"value":1},{"source":69,"target":25,"value":6},{"source":69,"target":68,"value":6},{"source":69,"target":11,"value":1},{"source":69,"target":24,"value":1},{"source":69,"target":27,"value":2},{"source":69,"target":48,"value":1},{"source":69,"target":41,"value":1},{"source":70,"target":25,"value":4},{"source":70,"target":69,"value":4},{"source":70,"target":68,"value":4},{"source":70,"target":11,"value":1},{"source":70,"target":24,"value":1},{"source":70,"target":27,"value":1},{"source":70,"target":41,"value":1},{"source":70,"target":58,"value":1},{"source":71,"target":27,"value":1},{"source":71,"target":69,"value":2},{"source":71,"target":68,"value":2},{"source":71,"target":70,"value":2},{"source":71,"target":11,"value":1},{"source":71,"target":48,"value":1},{"source":71,"target":41,"value":1},{"source":71,"target":25,"value":1},{"source":72,"target":26,"value":2},{"source":72,"target":27,"value":1},{"source":72,"target":11,"value":1},{"source":73,"target":48,"value":2},{"source":74,"target":48,"value":2},{"source":74,"target":73,"value":3},{"source":75,"target":69,"value":3},{"source":75,"target":68,"value":3},{"source":75,"target":25,"value":3},{"source":75,"target":48,"value":1},{"source":75,"target":41,"value":1},{"source":75,"target":70,"value":1},{"source":75,"target":71,"value":1},{"source":76,"target":64,"value":1},{"source":76,"target":65,"value":1},{"source":76,"target":66,"value":1},{"source":76,"target":63,"value":1},{"source":76,"target":62,"value":1},{"source":76,"target":48,"value":1},{"source":76,"target":58,"value":1}]};
// Let's add a random "connect" value to nodes:
json.nodes = json.nodes
.map(n => { n.connect = Math.floor(Math.random() * 100) + 1; return n; })
// Let's get a list of the top 10 nodes based on the value of "connect":
// By the way, the sort would modify the nodes order of the json (which is
// important to draw the force layout) and thus we create a deep copy of the
// nodes to sort them:
var top10nodes = JSON.parse(JSON.stringify(json.nodes))
.sort( function(a, b) { return b.connect - a.connect; })
.slice(0, 9)
.map(n => n.name);
// And we include a "top10" boolean on each node to able to later filter them
// when drawing the label:
json.nodes = json.nodes.map( function(d) {
if (top10nodes.indexOf(d.name) > -1) d.top10 = true;
return d;
});
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link");
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("image")
.attr("xlink:href", "https://github.com/favicon.ico")
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
node.filter( function(d) { return d.top10; }).append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
});
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
}
<script src="//d3js.org/d3.v3.min.js"></script>
The idea is to include a top10 field on each node representing the fact that the connect field of the element has its value in the top 10 biggest:
var json = {"nodes":[{"name":"Myriel","group":1},{"name":"Napoleon","group":1}, ...
// Let's add a random "connect" value to nodes to simulate your dataset:
// {nodes: [{name: "Myriel", group: 1, connect: 66}, ... :
json.nodes = json.nodes
.map(n => { n.connect = Math.floor(Math.random() * 100) + 1; return n; })
// Let's get a list of the top 10 nodes based on the value of "connect":
// By the way, the sort would modify the nodes order of the json (which is
// important to draw the force layout) and thus we create a deep copy of the
// nodes to sort them:
// ["MotherPlutarch", "Simplice", "Fantine", "Gueulemer", "CountessdeLo", "Count", "Perpetue", "Champtercier", "Mme.Pontmercy"]
var top10nodes = JSON.parse(JSON.stringify(json.nodes))
.sort( function(a, b) { return b.connect - a.connect; })
.slice(0, 9)
.map(n => n.name);
// And we include a "top10" boolean on each node to able to later filter them
// when drawing the label:
json.nodes = json.nodes.map( function(d) {
if (top10nodes.indexOf(d.name) > -1) d.top10 = true;
return d;
});
This way the in the definition of labels, we can filter only nodes for which this top10 field is true:
node.filter( function(d) { return d.top10; }).append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });

D3.js stacked layout - for repeated X values

Currently d3.layout.stack calculates the stacked-Y value based on the layers or series given.
Now I would like to generate stacked layout by both "X" value and by series. Anyway I am having 2-dimensional array of equal size.
For eg if my given data is,
var layers = [
{
"name": "apples",
"values": [
{ "x": 0, "y": 91},
{ "x": 1, "y": 290},
{ "x": 1, "y": 190},
{ "x": null, "y": null}
]
},
{
"name": "oranges",
"values": [
{ "x": 0, "y": 9},
{ "x": 1, "y": 49},
{ "x": 1, "y": 50},
{ "x": 2, "y": 220}
]
}
];
Here I have two layers ("apple","oranges"). My "x" values are [0,1,2];But If "X " values in the same layers looks same, then Y coordinate should be calculated based on the previous X value and series. Currently Y coordinate was calculated mapped by index.
Actual output for previous data:
var stacked_actual= [
{
"name": "apples",
"values": [
{ "x": 0, "y": 91,"y0":0},
{ "x": 1, "y": 290, ,"y0":0},
{ "x": 1, "y": 190, ,"y0":0},
{ "x": null, "y": null, ,"y0":0}
]
},
{
"name": "oranges",
"values": [
{ "x": 0, "y": 9,"y0":91},
{ "x": 1, "y": 49,"y0":290},
{ "x": 1, "y": 50,"y0":190},
{ "x": 2, "y": 220,"y0":0}
]
}
];
Expected output for previous data:
var stacked_expected= [
{
"name": "apples",
"values": [
{ "x": 0, "y": 91,"y0":0},
{ "x": 1, "y": 290 ,"y0":0},
{ "x": 1, "y": 190,"y0":190},
{ "x": null, "y": null ,"y0":0}
]
},
{
"name": "oranges",
"values": [
{ "x": 0, "y": 9,"y0":91},
{ "x": 1, "y": 49,"y0":480},
{ "x": 1, "y": 50,"y0":520},
{ "x": 2, "y": 220,"y0":0}
]
}
];
Is there any way to achieve this model?
Pls check the reference image
enter link description here

Modeling complex JSON in Ruby

I have a piece of JSON that is the boxscore of an NBA game as in an example below. Are there any strategies to turn these data into a sane Ruby model (maybe using serializers)? When I pull in these data from a remote API, there are lots of nested data within this JSON such as the Individual Player statistics, Team Statistics, team data, etc. Any solution on tools to use to deal with this JSON with sane Ruby modeling is preferred. Shouldn't matter but I'll be trying to work this modeling into a ruby gem, not a rails app.
Here's an example of one JSON blob I'm dealing with:
[
{
"game_length": "2:38",
"location": "Sleep Train Arena",
"game_time": "10:00 PM",
"season_type": "Regular",
"game_id": 19123,
"scores_by_quarter": [
{
"scores": {
"quarter": "1",
"away_team_score": 23,
"home_team_score": 24
}
},
{
"scores": {
"quarter": "2",
"away_team_score": 27,
"home_team_score": 21
}
},
{
"scores": {
"quarter": "3",
"away_team_score": 23,
"home_team_score": 27
}
},
{
"scores": {
"quarter": "4",
"away_team_score": 21,
"home_team_score": 22
}
},
{
"scores": {
"quarter": "OT",
"away_team_score": 10,
"home_team_score": 4
}
}
],
"team_stats": [
{
"team": {
"team_id": 101,
"team_name": "LA Clippers",
"listings": [
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 0,
"field_goals_attempted": 5,
"turnovers": 1,
"total_rebounds": 3,
"blocks": 0,
"steals": 1,
"assists": 1,
"field_goals_made": 2,
"offensive_rebounds": 1,
"player_first_name": "Jared",
"three_points_made": 1,
"player_last_name": "Dudley",
"points": 5,
"personal_fouls": 4,
"three_points_attempted": 3,
"minutes_played": 37,
"technical_fouls": 0,
"player_position": "F",
"player_id": 1292,
"started": "Yes",
"seconds_played": 16,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 10,
"freethrows_made": 7,
"field_goals_attempted": 15,
"turnovers": 4,
"total_rebounds": 12,
"blocks": 0,
"steals": 0,
"assists": 1,
"field_goals_made": 7,
"offensive_rebounds": 2,
"player_first_name": "Blake",
"three_points_made": 0,
"player_last_name": "Griffin",
"points": 21,
"personal_fouls": 4,
"three_points_attempted": 0,
"minutes_played": 38,
"technical_fouls": 1,
"player_position": "F",
"player_id": 1590,
"started": "Yes",
"seconds_played": 57,
"defensive_rebounds": 10
}
},
{
"listing": {
"freethrows_attempted": 5,
"freethrows_made": 0,
"field_goals_attempted": 9,
"turnovers": 1,
"total_rebounds": 15,
"blocks": 9,
"steals": 0,
"assists": 2,
"field_goals_made": 5,
"offensive_rebounds": 6,
"player_first_name": "DeAndre",
"three_points_made": 0,
"player_last_name": "Jordan",
"points": 10,
"personal_fouls": 1,
"three_points_attempted": 0,
"minutes_played": 40,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1487,
"started": "Yes",
"seconds_played": 14,
"defensive_rebounds": 9
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 4,
"field_goals_attempted": 9,
"turnovers": 1,
"total_rebounds": 1,
"blocks": 0,
"steals": 1,
"assists": 2,
"field_goals_made": 4,
"offensive_rebounds": 0,
"player_first_name": "J.J.",
"three_points_made": 1,
"player_last_name": "Redick",
"points": 13,
"personal_fouls": 2,
"three_points_attempted": 4,
"minutes_played": 16,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1129,
"started": "Yes",
"seconds_played": 59,
"defensive_rebounds": 1
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 4,
"field_goals_attempted": 12,
"turnovers": 1,
"total_rebounds": 1,
"blocks": 0,
"steals": 1,
"assists": 2,
"field_goals_made": 5,
"offensive_rebounds": 0,
"player_first_name": "Darren",
"three_points_made": 1,
"player_last_name": "Collison",
"points": 15,
"personal_fouls": 1,
"three_points_attempted": 2,
"minutes_played": 40,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1578,
"started": "Yes",
"seconds_played": 7,
"defensive_rebounds": 1
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 4,
"field_goals_attempted": 22,
"turnovers": 2,
"total_rebounds": 7,
"blocks": 0,
"steals": 0,
"assists": 11,
"field_goals_made": 12,
"offensive_rebounds": 2,
"player_first_name": "Jamal",
"three_points_made": 3,
"player_last_name": "Crawford",
"points": 31,
"personal_fouls": 1,
"three_points_attempted": 7,
"minutes_played": 37,
"technical_fouls": 0,
"player_position": "G",
"player_id": 299,
"started": "No",
"seconds_played": 29,
"defensive_rebounds": 5
}
},
{
"listing": {
"freethrows_attempted": 4,
"freethrows_made": 3,
"field_goals_attempted": 5,
"turnovers": 2,
"total_rebounds": 5,
"blocks": 0,
"steals": 1,
"assists": 1,
"field_goals_made": 1,
"offensive_rebounds": 1,
"player_first_name": "Antawn",
"three_points_made": 0,
"player_last_name": "Jamison",
"points": 5,
"personal_fouls": 1,
"three_points_attempted": 0,
"minutes_played": 16,
"technical_fouls": 0,
"player_position": "F",
"player_id": 682,
"started": "No",
"seconds_played": 37,
"defensive_rebounds": 4
}
},
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 1,
"field_goals_attempted": 1,
"turnovers": 0,
"total_rebounds": 0,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 1,
"offensive_rebounds": 0,
"player_first_name": "Ryan",
"three_points_made": 0,
"player_last_name": "Hollins",
"points": 3,
"personal_fouls": 2,
"three_points_attempted": 0,
"minutes_played": 10,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1198,
"started": "No",
"seconds_played": 11,
"defensive_rebounds": 0
}
},
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 1,
"field_goals_attempted": 4,
"turnovers": 0,
"total_rebounds": 1,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 0,
"offensive_rebounds": 1,
"player_first_name": "Reggie",
"three_points_made": 0,
"player_last_name": "Bullock",
"points": 1,
"personal_fouls": 0,
"three_points_attempted": 3,
"minutes_played": 12,
"technical_fouls": 0,
"player_position": "F",
"player_id": 2408,
"started": "No",
"seconds_played": 3,
"defensive_rebounds": 0
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 1,
"turnovers": 0,
"total_rebounds": 1,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 0,
"offensive_rebounds": 0,
"player_first_name": "Willie",
"three_points_made": 0,
"player_last_name": "Green",
"points": 0,
"personal_fouls": 0,
"three_points_attempted": 1,
"minutes_played": 15,
"technical_fouls": 0,
"player_position": "G",
"player_id": 634,
"started": "No",
"seconds_played": 7,
"defensive_rebounds": 1
}
},
{
"listing": {
"player_first_name": "Chris",
"reason": "Coach's Decision",
"player_last_name": "Paul",
"status": "Did Not Play",
"player_id": 1011
}
},
{
"listing": {
"player_first_name": "Matt",
"reason": "Coach's Decision",
"player_last_name": "Barnes",
"status": "Did Not Play",
"player_id": 827
}
},
{
"listing": {
"player_first_name": "Byron",
"reason": "Coach's Decision",
"player_last_name": "Mullens",
"status": "Did Not Play",
"player_id": 1568
}
},
{
"listing": {
"player_first_name": "Maalik",
"reason": "Injured",
"player_last_name": "Wayns",
"status": "Did Not Play",
"player_id": 2344
}
}
],
"totals": {
"freethrows_attempted": 37,
"freethrows_made": 24,
"field_goals_attempted": 83,
"total_rebounds": 46,
"points_off_turnovers": 18,
"blocks": 9,
"illegal_defense": 1,
"steals": 4,
"assists": 20,
"field_goals_made": 37,
"turnoveres": 12,
"offensive_rebounds": 13,
"team_turnovers": 12,
"three_points_made": 6,
"team_rebounds": 14,
"shooting_percentages": {
"field_goal_percentage": 0.446,
"three_point_percentage": 0.3,
"freethrow_percentage": 0.649
},
"points": 104,
"personal_fouls": 16,
"three_points_attempted": 20,
"minutes_played": 265,
"technical_fouls": 2,
"seconds_played": 240,
"non_player_technicals": 0,
"defensive_rebounds": 33
}
}
},
{
"team": {
"team_id": 109,
"team_name": "Sacramento",
"listings": [
{
"listing": {
"freethrows_attempted": 1,
"freethrows_made": 0,
"field_goals_attempted": 13,
"turnovers": 1,
"total_rebounds": 6,
"blocks": 0,
"steals": 0,
"assists": 4,
"field_goals_made": 6,
"offensive_rebounds": 0,
"player_first_name": "Derrick",
"three_points_made": 0,
"player_last_name": "Williams",
"points": 12,
"personal_fouls": 3,
"three_points_attempted": 2,
"minutes_played": 32,
"technical_fouls": 0,
"player_position": "F",
"player_id": 2192,
"started": "Yes",
"seconds_played": 21,
"defensive_rebounds": 6
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 7,
"turnovers": 1,
"total_rebounds": 4,
"blocks": 1,
"steals": 0,
"assists": 0,
"field_goals_made": 3,
"offensive_rebounds": 2,
"player_first_name": "Jason",
"three_points_made": 0,
"player_last_name": "Thompson",
"points": 6,
"personal_fouls": 4,
"three_points_attempted": 0,
"minutes_played": 23,
"technical_fouls": 0,
"player_position": "F",
"player_id": 1496,
"started": "Yes",
"seconds_played": 11,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 3,
"freethrows_made": 3,
"field_goals_attempted": 19,
"turnovers": 3,
"total_rebounds": 9,
"blocks": 3,
"steals": 0,
"assists": 6,
"field_goals_made": 11,
"offensive_rebounds": 1,
"player_first_name": "DeMarcus",
"three_points_made": 0,
"player_last_name": "Cousins",
"points": 25,
"personal_fouls": 5,
"three_points_attempted": 0,
"minutes_played": 41,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1900,
"started": "Yes",
"seconds_played": 21,
"defensive_rebounds": 8
}
},
{
"listing": {
"freethrows_attempted": 3,
"freethrows_made": 3,
"field_goals_attempted": 12,
"turnovers": 2,
"total_rebounds": 4,
"blocks": 0,
"steals": 2,
"assists": 3,
"field_goals_made": 5,
"offensive_rebounds": 2,
"player_first_name": "Ben",
"three_points_made": 1,
"player_last_name": "McLemore",
"points": 14,
"personal_fouls": 3,
"three_points_attempted": 6,
"minutes_played": 34,
"technical_fouls": 0,
"player_position": "G",
"player_id": 2432,
"started": "Yes",
"seconds_played": 43,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 1,
"freethrows_made": 1,
"field_goals_attempted": 12,
"turnovers": 0,
"total_rebounds": 3,
"blocks": 0,
"steals": 0,
"assists": 7,
"field_goals_made": 4,
"offensive_rebounds": 1,
"player_first_name": "Greivis",
"three_points_made": 2,
"player_last_name": "Vasquez",
"points": 11,
"personal_fouls": 2,
"three_points_attempted": 6,
"minutes_played": 35,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1970,
"started": "Yes",
"seconds_played": 58,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 4,
"turnovers": 1,
"total_rebounds": 3,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 2,
"offensive_rebounds": 1,
"player_first_name": "Marcus",
"three_points_made": 0,
"player_last_name": "Thornton",
"points": 4,
"personal_fouls": 5,
"three_points_attempted": 2,
"minutes_played": 17,
"technical_fouls": 0,
"player_position": "G",
"player_id": 1646,
"started": "No",
"seconds_played": 26,
"defensive_rebounds": 2
}
},
{
"listing": {
"freethrows_attempted": 2,
"freethrows_made": 1,
"field_goals_attempted": 5,
"turnovers": 1,
"total_rebounds": 9,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 3,
"offensive_rebounds": 3,
"player_first_name": "Patrick",
"three_points_made": 0,
"player_last_name": "Patterson",
"points": 7,
"personal_fouls": 4,
"three_points_attempted": 0,
"minutes_played": 26,
"technical_fouls": 0,
"player_position": "F",
"player_id": 1950,
"started": "No",
"seconds_played": 59,
"defensive_rebounds": 6
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 1,
"turnovers": 2,
"total_rebounds": 1,
"blocks": 0,
"steals": 0,
"assists": 0,
"field_goals_made": 0,
"offensive_rebounds": 1,
"player_first_name": "Hamady",
"three_points_made": 0,
"player_last_name": "N`diaye",
"points": 0,
"personal_fouls": 3,
"three_points_attempted": 0,
"minutes_played": 4,
"technical_fouls": 0,
"player_position": "C",
"player_id": 1944,
"started": "No",
"seconds_played": 56,
"defensive_rebounds": 0
}
},
{
"listing": {
"freethrows_attempted": 0,
"freethrows_made": 0,
"field_goals_attempted": 2,
"turnovers": 0,
"total_rebounds": 0,
"blocks": 0,
"steals": 1,
"assists": 1,
"field_goals_made": 0,
"offensive_rebounds": 0,
"player_first_name": "Travis",
"three_points_made": 0,
"player_last_name": "Outlaw",
"points": 0,
"personal_fouls": 0,
"three_points_attempted": 1,
"minutes_played": 9,
"technical_fouls": 0,
"player_position": "F",
"player_id": 759,
"started": "No",
"seconds_played": 30,
"defensive_rebounds": 0
}
},
{
"listing": {
"player_first_name": "Chuck",
"reason": "Coach's Decision",
"player_last_name": "Hayes",
"status": "Did Not Play",
"player_id": 1044
}
},
{
"listing": {
"player_first_name": "Carl",
"reason": "Injured",
"player_last_name": "Landry",
"status": "Did Not Play",
"player_id": 1333
}
},
{
"listing": {
"player_first_name": "Ray",
"reason": "Injured",
"player_last_name": "McCallum",
"status": "Did Not Play",
"player_id": 2430
}
},
{
"listing": {
"player_first_name": "Jimmer",
"reason": "Coach's Decision",
"player_last_name": "Fredette",
"status": "Did Not Play",
"player_id": 2111
}
}
],
"totals": {
"freethrows_attempted": 10,
"freethrows_made": 8,
"field_goals_attempted": 91,
"total_rebounds": 44,
"points_off_turnovers": 15,
"blocks": 5,
"illegal_defense": 1,
"steals": 4,
"assists": 24,
"field_goals_made": 42,
"turnoveres": 15,
"offensive_rebounds": 12,
"team_turnovers": 16,
"three_points_made": 6,
"team_rebounds": 6,
"shooting_percentages": {
"field_goal_percentage": 0.462,
"three_point_percentage": 0.261,
"freethrow_percentage": 0.8
},
"points": 98,
"personal_fouls": 33,
"three_points_attempted": 23,
"minutes_played": 265,
"technical_fouls": 1,
"seconds_played": 360,
"non_player_technicals": 0,
"defensive_rebounds": 32
}
}
}
],
"away_team_final_score": 104,
"quarter": 5,
"game_date": "November 29, 2013",
"technicals": [
{
"listing": {
"flagrant": "False",
"team_id": 109,
"time_left": "8:40",
"player_first_name": "",
"quarter": 3,
"player_last_name": "",
"player_id": 0,
"def_3_sec": "True",
"bench": "False"
}
},
{
"listing": {
"flagrant": "False",
"team_id": 101,
"time_left": "6:11",
"player_first_name": "Blake",
"quarter": 3,
"player_last_name": "Griffin",
"player_id": 1590,
"def_3_sec": "False",
"bench": "False"
}
},
{
"listing": {
"flagrant": "False",
"team_id": 101,
"time_left": "5:46",
"player_first_name": "",
"quarter": 3,
"player_last_name": "",
"player_id": 0,
"def_3_sec": "True",
"bench": "False"
}
}
],
"sport": "NBA",
"visiting_team": "LA Clippers",
"status": "FINAL",
"home_team_id": 109,
"title": "LA Clippers at Sacramento",
"attendance": "17317",
"home_team_final_score": 98,
"updated_at": "2013-11-30 06:08:40 UTC",
"visiting_team_id": 101,
"home_team": "Sacramento",
"season_year": "2013",
"officials": "Tony Brothers, David Guthrie, JT Orr"
}
]
You can parse JSON data into two classes, Arrays and Hashes:
Arrays:
data = [1, 2]
data.to_json # => "[1,2]"
JSON.parse(data.to_json) # => [1, 2]
Hashes:
data = {'a' => 1}
data.to_json # => "{\"a\":1}"
JSON.parse(data.to_json) # => {"a"=>1}
Attempting to generate anything else results in an error.
Once you parse the JSON string back into a Ruby object, you'll have one of those two types acting as a wrapper or container for the data you want, so you can use normal Ruby loops to walk through the data if it's an Array, and directly access the values you want if it's a Hash.
While your sample appears complex to you, it can easily be broken down into some simple code that will extract the data. If I parse the data and store it in a variable called data:
data.first returns the hash containing the data, which, in your example is all the data. If there were multiple hashes then iterate over them using:
data.each { |hash| ... }
or:
data.map { |hash| ... }
Once you have a sub-hash you can access things in similar manners, by iterating or using hash accesses:
data.first['scores_by_quarter'].last['scores']['home_team_score']
# => 4
I had to copy your JSON into a file to be able to read it properly(note that this is not necessary, you can supply the JSON stream directly to JSON.parse as it should be properly formatted), then parse it as a JSON to an array of hashes and use OpenStruct to convert them to ruby objects, if you have a large array, then OpenStruct is not very good with performance, explanation can be found here
require 'ostruct'
require 'json'
a = JSON.parse(File.read('temp.json'))
a.map! { |team| OpenStruct.new(team) }
Setters and getters generated by OpenStruct
a.each { |x| p x.methods(false) };
[:game_length, :game_length=, :location, :location=, :game_time, :game_time=, :season_type, :season_type=, :game_id, :game_id=, :scores_by_quarter, :scores_by_quarter=, :team_stats, :team_stats=, :away_team_final_score, :away_team_final_score=, :quarter, :quarter=, :game_date, :game_date=, :technicals, :technicals=, :sport, :sport=, :visiting_team, :visiting_team=, :status, :status=, :home_team_id, :home_team_id=, :title, :title=, :attendance, :attendance=, :home_team_final_score, :home_team_final_score=, :updated_at, :updated_at=, :visiting_team_id, :visiting_team_id=, :home_team, :home_team=, :season_year, :season_year=, :officials, :officials=]

Resources