ユーザーフィルターに応じて、D3js棒グラフのx軸を更新しようとしています(部分的に機能しています)。棒は実際には変化していますが、うまく機能していません。どこに問題があるのかよくわからないので、助けが必要です。
コードのこの部分では、棒グラフを更新しています
function update(selectedGroup) {
svg.selectAll("rect").remove()
var groups = d3.map(dataFilter, function(d){return(d.group)}).keys();
x.domain(groups);
var dataFilter = result.filter(function(d){return d.group==selectedGroup});
console.log(dataFilter);
var rectG=rectangulos(dataFilter)
}
この動作が見られるのは比較的簡単な理由があります。
When the domain of the scale x
is all the groups x.bandwidth()
is small. But when the domain of x
is only one value, x.bandwidth()
is large. In both cases, the first band starts in the same location.
Next we have a nested scale here xSubgroup
- the range of this is equal to x.bandwidth()
. When the domain of x
changes, we need to update the range of xSubgroup
. If we don't do this, the bars will still be very thin and start at the beginning of the axis (as the bars' bandwidth aren't changing even if the group's bandwidth does). You don't update the sub scale's range, but we need to do that:
x.domain(groups);
xSubgroup.range([0, x.bandwidth()])
With this we get the update we're looking for.
ただし、軸ラベルは変更されません。明示的に更新しない限り、スケールを更新しても軸は更新されません。メソッドチェーンを分割しg
、軸を保持するための参照を保存します。
var xAxis = svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
xAxis.selectAll("text")
...
これで軸を更新できます。テキストのスタイルも引き継いでいます。更新関数を使用して軸とデータのすべての入力/終了/更新を行うことでコードを簡略化できます-ここでは、初期設定と更新関数の両方が重複しているという点でいくつかの重複があります。
使用する軸を更新するには:
// Call the axis again to update
xAxis.call(d3.axisBottom(x))
xAxis.selectAll("text")
.style("text-anchor", "end")
.attr("font-size", "55px")
.attr("y", "-7")
.attr("x", "-7")
.attr("transform", "rotate(-90)");
私が正しく理解していれば、これは望ましい動作を与えます、更新されたplunkr
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加