2017-09-27 22 views
0

何らかの理由で、複数のchart.jsグラフを同じページに読み込む方法がわかりません。私はちょうどうまく読み込むために1つを得ることができますが、私がさらに追加すると、それらのどれも読み込まれません。私が間違ってやっていることについてのどんな考えですか?複数のchart.jsグラフを同じページにロードしようとしています

<div class="game-cards col-lg-5"> 
     <div class="chart-container"> 
      <canvas id="myChart" width="500" height="500"></canvas> 
     </div> 

     <div class="right-info"> 
      <h4>Iowa State vs Iowa</h4> 
      <h5 id="time-channel">11:00 AM - Channel Goes here</h5> 
      <div class="total-points-live"> 
       <h5>Total Points Bet</h5> 
       <h5 id="point-total">20,000</h5> 
       <p class="bet-button">Click To Place Bet</p> 
      </div> 
     </div> 
     <div> 
      <input class="bet-input" type="text" name="betAmount" placeholder="Wager Amount"> 
     </div> 
    </div> 
    <div class="game-cards col-lg-5"> 
     <div class="chart-container"> 
      <canvas id="myChart" width="500" height="500"></canvas> 
     </div> 

     <div class="right-info"> 
      <h4>Iowa State vs Iowa</h4> 
      <h5 id="time-channel">11:00 AM - Channel Goes here</h5> 
      <div class="total-points-live"> 
       <h5>Total Points Bet</h5> 
       <h5 id="point-total">20,000</h5> 
       <p class="bet-button">Click To Place Bet</p> 
      </div> 
     </div> 
     <div> 
      <input class="bet-input" type="text" name="betAmount" placeholder="Wager Amount"> 
     </div> 
    </div> 

これは、あなたがたぶん最初に、第二のためid="myChart2"ためid="myChart"を使用するクラスでIDを切り替えたり、

あなたのIDを一意にする必要があり、まだあなたがする必要がありますJavascriptを

window.onload = function() { 
     var ctx = document.getElementById("myChart").getContext('2d'); 

     var myChart = new Chart(ctx, { 
      type: 'doughnut', 
      data: { 
      labels: ["Iowa", "Iowa State"], 
      datasets: [{ 
       backgroundColor: [ 
       "#CC0000", 
       "#F1BE48", 
       ], 
       data: [2000, 9000] 
      }] 
      }, 
      options: { 
       responsive: true 
      , maintainAspectRatio: false 
      } 
     }); 
    } 

答えて

1

です2番目のグラフをターゲットにする別の関数を作成します。

クラスを使用してIDを切り替えることができ、両方のグラフが同じオプションを共有している場合は両方ともJavascript経由でターゲティングできます。

var ctx = document.getElementsByClassName("myChart").getContext('2d'); 
+0

はい、ありがとうございます!何らかの理由で私の脳が昨晩働いていなかった、簡単な答えに感謝します。 – Branduo

+0

心配しないで、幸せに助けてください:) – Deano

1

複数の要素に同じIDを使用しないでください。 IDはユニークである必要があります!あなたの例では

、異なるIDに変更して - 多分firstChartとsecondChart: - あなたがいないので、あなたが各チャートへの参照を必要としない場合

window.onload = function() { 
 
    var ctx = document.getElementById("firstChart").getContext('2d'); 
 

 
    var firstChart = new Chart(ctx, { 
 
    type: 'doughnut', 
 
    data: { 
 
     labels: ["Iowa", "Iowa State"], 
 
     datasets: [{ 
 
     backgroundColor: [ 
 
      "#CC0000", 
 
      "#F1BE48", 
 
     ], 
 
     data: [2000, 9000] 
 
     }] 
 
    }, 
 
    options: { 
 
     responsive: true, 
 
     maintainAspectRatio: false 
 
    } 
 
    }); 
 

 
    var ctx2 = document.getElementById("secondChart").getContext('2d'); 
 

 
    var secondChart = new Chart(ctx2, { 
 
    type: 'doughnut', 
 
    data: { 
 
     labels: ["Iowa", "Iowa State"], 
 
     datasets: [{ 
 
     backgroundColor: [ 
 
      "#CC0000", 
 
      "#F1BE48", 
 
     ], 
 
     data: [2000, 9000] 
 
     }] 
 
    }, 
 
    options: { 
 
     responsive: true, 
 
     maintainAspectRatio: false 
 
    } 
 
    }); 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.bundle.min.js"></script> 
 
<div class="game-cards col-lg-5"> 
 
    <div class="chart-container"> 
 
    <canvas id="firstChart" width="500" height="500"></canvas> 
 
    </div> 
 

 
    <div class="right-info"> 
 
    <h4>Iowa State vs Iowa</h4> 
 
    <h5 id="time-channel">11:00 AM - Channel Goes here</h5> 
 
    <div class="total-points-live"> 
 
     <h5>Total Points Bet</h5> 
 
     <h5 id="point-total">20,000</h5> 
 
     <p class="bet-button">Click To Place Bet</p> 
 
    </div> 
 
    </div> 
 
    <div> 
 
    <input class="bet-input" type="text" name="betAmount" placeholder="Wager Amount"> 
 
    </div> 
 
</div> 
 
<div class="game-cards col-lg-5"> 
 
    <div class="chart-container"> 
 
    <canvas id="secondChart" width="500" height="500"></canvas> 
 
    </div> 
 

 
    <div class="right-info"> 
 
    <h4>Iowa State vs Iowa</h4> 
 
    <h5 id="time-channel">11:00 AM - Channel Goes here</h5> 
 
    <div class="total-points-live"> 
 
     <h5>Total Points Bet</h5> 
 
     <h5 id="point-total">20,000</h5> 
 
     <p class="bet-button">Click To Place Bet</p> 
 
    </div> 
 
    </div> 
 
    <div> 
 
    <input class="bet-input" type="text" name="betAmount" placeholder="Wager Amount"> 
 
    </div> 
 
</div>

か後でそれらを変更したい - すべてのチャートに同じクラスを使用します。

window.onload = function() { 
 
    var charts = document.getElementsByClassName("piechart"); 
 

 
    for (chart of charts) { 
 
    var ctx = chart.getContext('2d'); 
 

 
    new Chart(ctx, { 
 
     type: 'doughnut', 
 
     data: { 
 
     labels: ["Iowa", "Iowa State"], 
 
     datasets: [{ 
 
      backgroundColor: [ 
 
      "#CC0000", 
 
      "#F1BE48", 
 
      ], 
 
      data: [2000, 9000] 
 
     }] 
 
     }, 
 
     options: { 
 
     responsive: true, 
 
     maintainAspectRatio: false 
 
     } 
 
    }); 
 
    } 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.bundle.min.js"></script> 
 
<div class="game-cards col-lg-5"> 
 
    <div class="chart-container"> 
 
    <canvas id="firstChart" class="piechart" width="500" height="500"></canvas> 
 
    </div> 
 

 
    <div class="right-info"> 
 
    <h4>Iowa State vs Iowa</h4> 
 
    <h5 id="time-channel">11:00 AM - Channel Goes here</h5> 
 
    <div class="total-points-live"> 
 
     <h5>Total Points Bet</h5> 
 
     <h5 id="point-total">20,000</h5> 
 
     <p class="bet-button">Click To Place Bet</p> 
 
    </div> 
 
    </div> 
 
    <div> 
 
    <input class="bet-input" type="text" name="betAmount" placeholder="Wager Amount"> 
 
    </div> 
 
</div> 
 
<div class="game-cards col-lg-5"> 
 
    <div class="chart-container"> 
 
    <canvas id="secondChart" class="piechart" width="500" height="500"></canvas> 
 
    </div> 
 

 
    <div class="right-info"> 
 
    <h4>Iowa State vs Iowa</h4> 
 
    <h5 id="time-channel">11:00 AM - Channel Goes here</h5> 
 
    <div class="total-points-live"> 
 
     <h5>Total Points Bet</h5> 
 
     <h5 id="point-total">20,000</h5> 
 
     <p class="bet-button">Click To Place Bet</p> 
 
    </div> 
 
    </div> 
 
    <div> 
 
    <input class="bet-input" type="text" name="betAmount" placeholder="Wager Amount"> 
 
    </div> 
 
</div>

関連する問題