我正在尝试使用查询从数据库数据创建条形图:
$myquery = "select column_nm, column_vl
from
(
select 'Head of Department' column_nm, COUNT(`team_role`) column_vl
from team WHERE team_role = 'HOD'
union all
select 'Supervisor', COUNT(team_role)
from team WHERE team_role='Supervisor'
union all
select 'Checker', COUNT(team_role)
from team WHERE team_role='Checker'
union all
select 'Squad Leader', COUNT(team_role)
from team WHERE team_role='Squad Leader'
union all
select 'Team Member', COUNT(team_role)
from team WHERE team_role='Team Member'
) d";
以下是我从此查询中获得的信息:
| column_nm | value_nm |
| Head of Department | 3 |
| Supervisor | 9 |
| Checker | 34 |
| Squad Leader | 26 |
| Team Member | 24 |
但是当谈到条形图时,y比例的最大数量是9.这是我在条形图中得到的结果:
如果我删除主管列,那么它工作正常,系统显示正确的数字。有什么问题,如何解决? 和条形码的代码:
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatPercent = d3.format("100");
//var formatPercent = 4
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>"+ d.column_nm +":</strong> <span style='color:red'>" + d.column_vl + "</span>";
})
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 + ")");
svg.call(tip);
d3.json("scoreboard_data.php", function(error, data) {
x.domain(data.map(function(d) { return d.column_nm; }));
y.domain([0, d3.max(data, function(d) { return d.column_vl; })]);
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("Score");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.column_nm); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.column_vl); })
.attr("height", function(d) { return height - y(d.column_vl); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
});
function type(d) {
d.column_vl = +d.column_vl;
return d;
}
</script>
如何更改y比例以显示所有值中的最大数字?谢谢
答案 0 :(得分:2)
您的域名......
y.domain([0, d3.max(data, function(d) {
return d.column_vl;
})]);
...设置正确。
因此,这只留下一个解释:您的数据中包含字符串,而不是数字。
如果我们将您的数字视为字符串,则很容易显示。您可以看到“最大”将是"9"
(作为字符串,而不是数字):
var data = ["3", "9", "34", "26", "24"];
console.log("The maximum among those 'numbers' is: " + d3.max(data))
<script src="https://d3js.org/d3.v4.min.js"></script>
此外,如果我们删除supervisor
(即“9”),则新的最大值将为“34”,这与您描述的行为相符。
<强>解决方案:强>
使用数据中的循环或域中的一元运算符将字符串转换为数字:
y.domain([0, d3.max(data, function(d) {
return +d.column_vl;
// ^----- unary operator here
})]);