d3js v5x軸棒グラフ

ベリス

多くのデータセットを試しましたが、理由がわかりません。data_histo.csvに問題があります。x軸が逆になっているように見えるため、バーを表示できません。data_histo2.csvまたはdata_histo3.csv、すべてが良いです。

説明がいいかも!

<!DOCTYPE html>
<html>

  <head>
    <script src="https://unpkg.com/[email protected]/dist/d3.min.js"></script>
  </head>

  <body>


  <svg class="histogramme" width="960" height="600"></svg>

  <script>

//select
let svgHisto = d3.select(".histogramme")

//dimension
  let margin = {top: 20, right: 10, bottom: 20, left: 80}
  let width = +svgHisto.attr("width") - margin.left - margin.right
  let height = +svgHisto.attr("height") - margin.top - margin.bottom;

  let g1 = svgHisto.append("g")
      .attr("transform",`translate(${margin.left}, ${margin.top})`);

//data
  d3.csv("data_histo.csv").then(function(data) {
    console.log(data);

//define x and y axis
    let x = d3.scaleLinear();
    let y = d3.scaleBand();

x.domain(d3.extent(data, function(d) { return d.value0; })).nice()
     .rangeRound([0, width]);
    y.domain(data.map(function(d) { return d.libreg; }))
     .rangeRound([0, height]).padding(0.1);

//add x axis
    g1.append("g")
        .attr("class", "axis x_axis")
        .attr("transform",`translate(0,${height})`)
        .call(d3.axisBottom(x));

//add y axis
    g1.append("g")
        .attr("class", "axis y_axis")
        .call(d3.axisLeft(y));

//bar chart
  g1.selectAll(".bar1")
      .data(data)
      .enter().append("rect")
        .attr("class", "bar bar1")
        .attr("x", function(d) {return x(Math.min(0,d.value0)); })
        .attr("y", function(d) { return y(d.libreg) + 10; })
        .attr("width", 0)
        .attr("height", y.bandwidth() - 20);

//animate
      g1.selectAll(".bar1")
      .transition()
      .duration(1000)
      .delay(function(d,i){return i*100})
      .attr("width", function(d) { return x(d.value0); });

  });

  </script>

  </body>

</html>

data_histo.csv

"codreg","libreg","year0","value0","year1","value1"
"03","Guyane",2009,4,2014,4.6
"04","La Réunion",2009,8.2,2014,9.8
"11","Île-de-France",2009,12.6,2014,13.9
"01","Guadeloupe",2009,13.3,2014,15.8
"32","Hauts-de-France",2009,14.7,2014,16.1
"02","Martinique",2009,14.7,2014,17.6
"44","Grand Est",2009,16.5,2014,18
"84","Auvergne-Rhône-Alpes",2009,16.8,2014,18.3
"52","Pays de la Loire",2009,17.1,2014,18.6
"28","Normandie",2009,17.2,2014,19
"53","Bretagne",2009,18.5,2014,20.2
"24","Centre-Val de Loire",2009,18.7,2014,20.4
"27","Bourgogne-Franche-Comté",2009,18.8,2014,20.7
"76","Occitanie",2009,19.3,2014,20.8
"93","Provence-Alpes-Côte d''Azur",2009,19.5,2014,21.3
"94","Corse",2009,20.2,2014,21.5
"75","Nouvelle-Aquitaine",2009,20.2,2014,21.8

ここに画像の説明を入力してくださいdata_histo2.csv

"codreg","libreg","year0","value0","year1","value1"
"84","Auvergne-Rhône-Alpes",2013,39.1,2017,41.7
"27","Bourgogne-Franche-Comté",2013,42.3,2017,44.4
"53","Bretagne",2013,39.6,2017,44.7
"24","Centre-Val de Loire",2013,40.5,2017,46.8
"94","Corse",2013,24.2,2017,30.8
"44","Grand Est",2013,41.3,2017,45.4
"01","Guadeloupe",2013,55.5,2017,56.5
"03","Guyane",2013,33.1,2017,33.2
"32","Hauts-de-France",2013,45.8,2017,47.3
"11","Île-de-France",2013,40.1,2017,42.6
"02","Martinique",2013,52.5,2017,50.2
"28","Normandie",2013,42.6,2017,46.2
"75","Nouvelle-Aquitaine",2013,40,2017,44.4
"76","Occitanie",2013,40.3,2017,43.7
"52","Pays de la Loire",2013,40.6,2017,45.8
"93","Provence-Alpes-Côte d''Azur",2013,38.5,2017,42.6
"04","La Réunion",2013,54.2,2017,54.6
"06","Mayotte",2013,,2017,

ここに画像の説明を入力してください

これが私のコードです:https//plnkr.co/edit/B8qEQ4dlUdRHhkQvzjZx?p = preview

アンドリューリード

コードには2つの問題があります。

D3は、csv / tsv / dsvエントリをテキストとして解析します。したがって、csvをロードすると、次のような行が表示されます。

{
  "codreg": "03",
  "libreg": "Guyane",
  "year0": "2009",
  "value0": "4",
  "year1": "2014",
  "value1": "4.6"
}

When you set your scale with extent, you aren't using the numerical extent. You could coerce your data to a number like so:

data.forEach(function(d) {
  d.value0 = +d.value0;
})

Secondly, if you do this you'll notice some peculiar behavior in the placement of the bars:

ここに画像の説明を入力してください

You can see that the bars start to the left of the plot area. The reason is that you are using a linear scale, and plot the start of the bars like so:

.attr("x", function(d) {return x(Math.min(0,d.value0)); })

You want your bars to start at x(4) - which is where the x value that marks the interception with the y axis. Instead you are using x(0), which will naturally be to the left of where you want. This works in your second example, because x(0) also happens to be the x value that intercepts the y axis. Instead, you can simply use:

.attr("x",0)

This marks the left edge of the plot area, which is likely where you want all your bars to be anchored to.

これがフォークされたプランカーです。

注意すべき点の1つは、最短のバーが常に非表示になることです。開始点と終了点は同じになります。これは、スケールの範囲が最小値から最大値に変化するためです。プロット領域の左側の境界を示す最小値が最短バーの値です。これを変更するには、スケールのドメインを変更します。おそらく最初の値として0を使用し、次にd3.maxを使用して最上位の値を見つけます。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

d3jsの棒グラフの棒高さの問題

分類Dev

d3js双方向水平棒グラフ

分類Dev

scaleTimeを使用したD3JS棒グラフ

分類Dev

d3js v3: Selecting a Node on click

分類Dev

D3js d.x is undefined

分類Dev

Sort Div Whit d3js html5

分類Dev

棒グラフd3jsの横にカウントを追加

分類Dev

D3JSを使用して棒グラフをプロットする

分類Dev

D3js縦棒グラフの列間の大きなギャップ

分類Dev

d3js積み上げ棒グラフが更新されない

分類Dev

D3jsでのx軸のラベルのフォーマット

分類Dev

C3.js棒グラフ:X軸ラベルの設定

分類Dev

日付をx軸とするC3.js棒グラフ

分類Dev

x軸のカスタムD3`tickValue`-横棒グラフ

分類Dev

x軸d3jsを更新しています

分類Dev

d3jsグラフに画像を追加できますか?

分類Dev

D3JS折れ線グラフ反転問題

分類Dev

d3jsグラフに線が表示されない

分類Dev

d3jsマルチグラフ自動更新

分類Dev

D3js折れ線グラフの構成

分類Dev

C3棒グラフ-カスタムX軸ラベル

分類Dev

d3jsグループ化された棒グラフの切り替え凡例

分類Dev

D3.js棒グラフY軸の問題

分類Dev

Chart.js棒グラフのx軸の中心に棒を配置する方法は?

分類Dev

壊れた D3js コード ダイアグラム

分類Dev

d3 js v4棒グラフを下(x軸)から上にアニメーション化する方法は?

分類Dev

棒グラフの上にドットを表示するd3jsマスク

分類Dev

データ値を使用してd3js棒グラフに背景色を追加する

分類Dev

タイムスケールにわたるD3JSスタック棒グラフ

Related 関連記事

ホットタグ

アーカイブ