2017-09-27 2 views
0

어떤 이유로 인해 동일한 페이지에서 여러 chart.js 차트를로드하는 방법을 알 수 없습니다. 하나는 제대로로드 할 수 있지만, 더 추가하면로드되지 않습니다. 내가 뭘 잘못하고 있는지에 대한 어떤 생각?여러 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를 전환하거나 고유 한 사용자 ID를

어쩌면 초 처음에 id="myChart"id="myChart2"를 사용할 수 있도록하는 데 필요한 자바 스크립트

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

이다, 그러나 당신이 필요합니다 두 번째 차트를 대상으로하는 다른 함수를 만듭니다.

클래스를 사용하여 ID를 전환하고 두 차트 모두 동일한 옵션을 공유하는 경우 Javascript를 통해 ID를 전환 할 수 있습니다.

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

예, 감사합니다. 왠지 내 머리가 어젯밤에 작동하지 않았기 때문에 간단한 대답에 감사드립니다. – Branduo

+0

걱정하지 않아도됩니다. :) – Deano

1

여러 요소에 동일한 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>