关于javascript:从JSON数据而不是CSV分组条形图

grouped bar chart from JSON data instead of CSV

我正在尝试重新创建"分组条形图"示例,但是我宁愿使用JSON数据也不使用CSV:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var data = [
  {
   "State":"CA",
   "Under 5 Years": 2704659,
   "5 to 13 Years": 4499890,
   "14 to 17 Years": 2159981,
   "18 to 24 Years": 3853788,
   "25 to 44 Years": 10604510,
   "45 to 64 Years": 8819342,
   "65 Years and Over": 4114496
  },
  {
   "State":"TX",
   "Under 5 Years": 2027307,
   "5 to 13 Years": 3277946,
   "14 to 17 Years": 1420518,
   "18 to 24 Years": 2454721,
   "25 to 44 Years": 7017731,
   "45 to 64 Years": 5656528,
   "65 Years and Over": 2472223
  },
  {
   "State":"NY",
   "Under 5 Years": 1208495,
   "5 to 13 Years": 2141490,
   "14 to 17 Years": 1058031,
   "18 to 24 Years": 1999120,
   "25 to 44 Years": 5355235,
   "45 to 64 Years": 5120254,
   "65 Years and Over": 2607672
  },
  {
   "State":"FL",
   "Under 5 Years": 1140516,
   "5 to 13 Years": 1938695,
   "14 to 17 Years": 925060,
   "18 to 24 Years": 1607297,
   "25 to 44 Years": 4782119,
   "45 to 64 Years": 4746856,
   "65 Years and Over": 3187797
  },
  {
   "State":"IL",
   "Under 5 Years": 894368,
   "5 to 13 Years": 1558919,
   "14 to 17 Years": 725973,
   "18 to 24 Years": 1311479,
   "25 to 44 Years": 3596343,
   "45 to 64 Years": 3239173,
   "65 Years and Over": 1575308
  },
  {
   "State":"PA",
   "Under 5 Years": 737462,
   "5 to 13 Years": 1345341,
   "14 to 17 Years": 679201,
   "18 to 24 Years": 1203944,
   "25 to 44 Years": 3157759,
   "45 to 64 Years": 3414001,
   "65 Years and Over": 1910571
  }
]

这是我尝试重新创建的示例:

https://bl.ocks.org/mbostock/3887051

任何帮助将不胜感激,我确实是一个JavaScript新手。


因此,我只需删除示例的CSV部分就可以解决此问题。 这是链接到本地d3.v3.js文件的静态版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<meta charset="utf-8">
<script type="text/javascript" src="file:///C:/Users/userName/d3/d3.v3.min.js">
<style>

body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.bar {
  fill: steelblue;
}

.x.axis path {
  display: none;
}

</style>
<body>
<script type="text/javascript">

var data = [
  {
   "State":"CA",
   "Under 5 Years": 2704659,
   "5 to 13 Years": 4499890,
   "14 to 17 Years": 2159981,
   "18 to 24 Years": 3853788,
   "25 to 44 Years": 10604510,
   "45 to 64 Years": 8819342,
   "65 Years and Over": 4114496
  },
  {
   "State":"TX",
   "Under 5 Years": 2027307,
   "5 to 13 Years": 3277946,
   "14 to 17 Years": 1420518,
   "18 to 24 Years": 2454721,
   "25 to 44 Years": 7017731,
   "45 to 64 Years": 5656528,
   "65 Years and Over": 2472223
  },
  {
   "State":"NY",
   "Under 5 Years": 1208495,
   "5 to 13 Years": 2141490,
   "14 to 17 Years": 1058031,
   "18 to 24 Years": 1999120,
   "25 to 44 Years": 5355235,
   "45 to 64 Years": 5120254,
   "65 Years and Over": 2607672
  },
  {
   "State":"FL",
   "Under 5 Years": 1140516,
   "5 to 13 Years": 1938695,
   "14 to 17 Years": 925060,
   "18 to 24 Years": 1607297,
   "25 to 44 Years": 4782119,
   "45 to 64 Years": 4746856,
   "65 Years and Over": 3187797
  },
  {
   "State":"IL",
   "Under 5 Years": 894368,
   "5 to 13 Years": 1558919,
   "14 to 17 Years": 725973,
   "18 to 24 Years": 1311479,
   "25 to 44 Years": 3596343,
   "45 to 64 Years": 3239173,
   "65 Years and Over": 1575308
  },
  {
   "State":"PA",
   "Under 5 Years": 737462,
   "5 to 13 Years": 1345341,
   "14 to 17 Years": 679201,
   "18 to 24 Years": 1203944,
   "25 to 44 Years": 3157759,
   "45 to 64 Years": 3414001,
   "65 Years and Over": 1910571
  }
]

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x0 = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var x1 = d3.scale.ordinal();

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.ordinal()
    .range(["#98abc5","#8a89a6","#7b6888","#6b486b","#a05d56","#d0743c","#ff8c00"]);

var xAxis = d3.svg.axis()
    .scale(x0)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(d3.format(".2s"));

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform","translate(" + margin.left +"," + margin.top +")");

var ageNames = d3.keys(data[0]).filter(function(key) { return key !=="State"; });

data.forEach(function(d) {
    d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
  });

x0.domain(data.map(function(d) { return d.State; }));
x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })]);

svg.append("g")
      .attr("class","x axis")
      .attr("transform","translate(0," + height +")")
      .call(xAxis);

svg.append("g")
      .attr("class","y axis")
      .call(yAxis)
      .append("text")
      .attr("transform","rotate(-90)")
      .attr("y", 6)
      .attr("dy",".71em")
      .style("text-anchor","end")
      .text("Population");

var state = svg.selectAll(".state")
      .data(data)
      .enter().append("g")
      .attr("class","state")
      .attr("transform", function(d) { return"translate(" + x0(d.State) +",0)"; });

state.selectAll("rect")
      .data(function(d) { return d.ages; })
      .enter().append("rect")
      .attr("width", x1.rangeBand())
      .attr("x", function(d) { return x1(d.name); })
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .style("fill", function(d) { return color(d.name); });

var legend = svg.selectAll(".legend")
      .data(ageNames.slice().reverse())
      .enter().append("g")
      .attr("class","legend")
      .attr("transform", function(d, i) { return"translate(0," + i * 20 +")"; });

legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);

legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9)
      .attr("dy",".35em")
      .style("text-anchor","end")
      .text(function(d) { return d; });


</body>
</html>

这是JS Bin版本:http://jsbin.com/nuyipikaye/edit?html,js,output

感谢您的所有帮助!


通过将阵列转换为所需格式,可以轻松实现此驱动:

要进行转换,您可以使用:

1
2
3
4
5
6
7
8
9
for(var x in data){
var currData = [];    
for(var y in data[x]){
         currData.push(data[x][y]);
     }
dataArr.push(currData);
}

var tdata = d3.transpose(dataArr);

然后使用:http://bl.ocks.org/3532324

D3:从json对象创建分组的条形图

JSON D3.JS中的分组条形图

D3.js:如何从分组条形图中的JSON读取数据