`d3.json('dc_test_case.json', function(error, data) {
var data = data;
var ndx = crossfilter(data);
homeDim = ndx.dimension(function(d){
return d.Inning;
});
nameDim = ndx.dimension(function(d){
if(d.Inning === "England batting"){
return d.Name;
}
});
runName = nameDim.group().reduceSum(function(d) {
if(d.Inning === "England batting") {
return Number(d.Runs);
}
});
var England_batting = dc.barChart("#England_batting");
England_batting.width(700)
.height(480)
.gap(30)
.yAxisLabel(".", 50)
.xAxisLabel("U", 50)
.dimension(nameDim)
.group(runName)
.x(d3.scale.ordinal().domain(nameDim))
.xUnits(dc.units.ordinal)
.y(d3.scale.linear().domain([0, 200]))
.elasticY(true)
.renderHorizontalGridLines(true)
.renderTitle(true)
.brushOn(true);
dc.renderAll();
});
英格兰击球的条形图不正确,有数据错误。我想我不应该使用reduceSum来分组数据。我不知道应该使用什么......任何人都可以解决这个问题...
此代码的json如下所示......
[
{
"Name": "ME Trescothick",
"Out": "c Hussey b Symonds",
"Runs": "41",
"Minutes": "55",
"Balls": "37",
"4s": "5",
"6s": "",
"SR": "110.81",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d3a"
},
{
"Name": "KP Pietersen",
"Out": "c Hayden b Clarke",
"Runs": "34",
"Minutes": "19",
"Balls": "18",
"4s": "3",
"6s": "1",
"SR": "188.88",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d3b"
},
{
"Name": "A Flintoff",
"Out": "c Symonds b Kasprowicz",
"Runs": "6",
"Minutes": "9",
"Balls": "5",
"4s": "",
"6s": "",
"SR": "120.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d3c"
},
{
"Name": "GO Jones†",
"Out": "c Kasprowicz b McGrath",
"Runs": "19",
"Minutes": "17",
"Balls": "14",
"4s": "4",
"6s": "",
"SR": "135.71",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d3d"
},
{
"Name": "MP Vaughan*",
"Out": "c Ponting b Symonds",
"Runs": "",
"Minutes": "2",
"Balls": "1",
"4s": "",
"6s": "",
"SR": "0.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d3e"
},
{
"Name": "PD Collingwood",
"Out": "c Ponting b McGrath",
"Runs": "46",
"Minutes": "28",
"Balls": "26",
"4s": "5",
"6s": "2",
"SR": "176.92",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d3f"
},
{
"Name": "AJ Strauss",
"Out": "b Gillespie",
"Runs": "18",
"Minutes": "15",
"Balls": "16",
"4s": "1",
"6s": "",
"SR": "112.50",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d40"
},
{
"Name": "VS Solanki",
"Out": "c Hussey b McGrath",
"Runs": "9",
"Minutes": "5",
"Balls": "5",
"4s": "1",
"6s": "",
"SR": "180.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d41"
},
{
"Name": "J Lewis",
"Out": "not out",
"Runs": "",
"Minutes": "1",
"Balls": "",
"4s": "",
"6s": "",
"SR": "-",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d42"
},
{
"Name": "Extras",
"Out": "(lb 1, w 3, nb 2)",
"Runs": "6",
"Minutes": "",
"Balls": "",
"4s": "",
"6s": "",
"SR": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d43"
},
{
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England batting",
"id": "569aa1f87e655d7e6af61d44",
"Total": "Total",
"Total-D": "(8 wickets; 20 overs; 79 mins)",
"R": "179",
"RPO": "(8.95 runs per over)"
},
{
"Name": "GD McGrath",
"Runs": "31",
"4s": "4",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia bowling",
"id": "569aa1f87e655d7e6af61d45",
"Over": "4",
"Maiden": "",
"Wickets": "3",
"Economy": "7.75",
"0s": "10"
},
{
"Name": "B Lee",
"Runs": "31",
"4s": "4",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia bowling",
"id": "569aa1f87e655d7e6af61d46",
"Over": "3",
"Maiden": "",
"Wickets": "",
"Economy": "10.33",
"0s": "6"
},
{
"Name": "MS Kasprowicz",
"Runs": "28",
"4s": "3",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia bowling",
"id": "569aa1f87e655d7e6af61d47",
"Over": "3",
"Maiden": "",
"Wickets": "1",
"Economy": "9.33",
"0s": "2"
},
{
"Name": "JN Gillespie",
"Runs": "49",
"4s": "6",
"6s": "2",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia bowling",
"id": "569aa1f87e655d7e6af61d48",
"Over": "4",
"Maiden": "",
"Wickets": "1",
"Economy": "12.25",
"0s": "5"
},
{
"Name": "A Symonds",
"Runs": "14",
"4s": "1",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia bowling",
"id": "569aa1f87e655d7e6af61d49",
"Over": "3",
"Maiden": "",
"Wickets": "2",
"Economy": "4.66",
"0s": "7"
},
{
"Name": "MJ Clarke",
"Runs": "25",
"4s": "1",
"6s": "1",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia bowling",
"id": "569aa1f87e655d7e6af61d4a",
"Over": "3",
"Maiden": "",
"Wickets": "1",
"Economy": "8.33",
"0s": "4"
},
{
"Name": "AC Gilchrist†",
"Out": "c Pietersen b Gough",
"Runs": "15",
"Minutes": "11",
"Balls": "14",
"4s": "3",
"6s": "",
"SR": "107.14",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d4b"
},
{
"Name": "ML Hayden",
"Out": "c Pietersen b Gough",
"Runs": "6",
"Minutes": "12",
"Balls": "4",
"4s": "1",
"6s": "",
"SR": "150.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d4c"
},
{
"Name": "A Symonds",
"Out": "c Pietersen b Lewis",
"Runs": "",
"Minutes": "5",
"Balls": "2",
"4s": "",
"6s": "",
"SR": "0.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d4d"
},
{
"Name": "MEK Hussey",
"Out": "c Flintoff b Gough",
"Runs": "1",
"Minutes": "6",
"Balls": "6",
"4s": "",
"6s": "",
"SR": "16.66",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d4e"
},
{
"Name": "RT Ponting*",
"Out": "c Solanki b Lewis",
"Runs": "",
"Minutes": "6",
"Balls": "3",
"4s": "",
"6s": "",
"SR": "0.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d4f"
},
{
"Name": "DR Martyn",
"Out": "c Trescothick b Lewis",
"Runs": "4",
"Minutes": "6",
"Balls": "4",
"4s": "1",
"6s": "",
"SR": "100.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d50"
},
{
"Name": "B Lee",
"Out": "c Harmison b Collingwood",
"Runs": "15",
"Minutes": "29",
"Balls": "20",
"4s": "1",
"6s": "",
"SR": "75.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d51"
},
{
"Name": "MS Kasprowicz",
"Out": "not out",
"Runs": "3",
"Minutes": "16",
"Balls": "5",
"4s": "",
"6s": "",
"SR": "60.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d52"
},
{
"Name": "GD McGrath",
"Out": "b Harmison",
"Runs": "5",
"Minutes": "9",
"Balls": "12",
"4s": "",
"6s": "",
"SR": "41.66",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d53"
},
{
"Name": "Extras",
"Out": "(b 1, lb 2, w 1, nb 2)",
"Runs": "6",
"Minutes": "",
"Balls": "",
"4s": "",
"6s": "",
"SR": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d54"
},
{
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia-batting",
"id": "569aa1f87e655d7e6af61d55",
"Total": "Total",
"Total-D": "(all out; 14.3 overs; 64 mins)",
"R": "79",
"RPO": "(5.44 runs per over)"
},
{
"Name": "D Gough",
"Runs": "16",
"4s": "3",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England bowling",
"id": "569aa1f87e655d7e6af61d56",
"Over": "3",
"Maiden": "",
"Wickets": "3",
"Economy": "5.33",
"0s": "13"
},
{
"Name": "J Lewis",
"Runs": "24",
"4s": "3",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England bowling",
"id": "569aa1f87e655d7e6af61d57",
"Over": "4",
"Maiden": "",
"Wickets": "4",
"Economy": "6.00",
"0s": "14"
},
{
"Name": "SJ Harmison",
"Runs": "13",
"4s": "2",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England bowling",
"id": "569aa1f87e655d7e6af61d58",
"Over": "2.3",
"Maiden": "",
"Wickets": "1",
"Economy": "5.20",
"0s": "9"
},
{
"Name": "MJ Clarke",
"Out": "c †Jones b Lewis",
"Runs": "",
"Minutes": "1",
"Balls": "1",
"4s": "",
"6s": "",
"SR": "0.00",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d59"
},
{
"Name": "JN Gillespie",
"Out": "c Trescothick b Collingwood",
"Runs": "24",
"Minutes": "18",
"Balls": "18",
"4s": "4",
"6s": "",
"SR": "133.33",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "Australia batting",
"id": "569aa1f87e655d7e6af61d5a"
},
{
"Name": "A Flintoff",
"Runs": "15",
"4s": "2",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England bowling",
"id": "569aa1f87e655d7e6af61d5b",
"Over": "3",
"Maiden": "",
"Wickets": "",
"Economy": "5.00",
"0s": "10"
},
{
"Name": "PD Collingwood",
"Runs": "8",
"4s": "",
"6s": "",
"Home": "England",
"Away": "Australia",
"Date": "Jun 13, 2005",
"Ground": "Southampton",
"Series": "Australia tour of England and Scotland",
"Match_No": "Only T20I",
"Winning_info": "England won by 100 runs",
"Inning": "England bowling",
"id": "569aa1f87e655d7e6af61d75",
"Over": "2",
"Maiden": "",
"Wickets": "2",
"Economy": "4.00",
"0s": "6"
}
]

答案 0 :(得分:0)
切换到以下内容,我认为:
runName = nameDim.group().reduceSum(function(d) {
if(d.Inning === "England batting") {
return Number(d.Runs);
} else {
return 0;
}
});
问题是,如果澳大利亚正在击球,您将返回undefined,15 + undefined
评估为NaN
。
另外:请在将来创建一个工作示例。我能够只关注这一个,但我无法检查我的解决方案是否真的有效。如果你创建一个有效的例子,你就更有可能获得帮助。