2016-10-25 579 views
0

js很新,但尋求一些幫助在一個頁面上呈現兩個圖表(chart.js)。無法獲得第二張圖表來顯示。我認爲通過創建用不同的畫布ID來分離腳本,每個圖表都會單獨呈現。目前,只有'早上'圖表被呈現。謝謝Chart.js在一個頁面上呈現兩個圖表。

<div class="container-profile"> 
     <div class="container-week"> 
      <div class="col s12 m5"> 
       <div class="card-panel teal"> 
        <h1>MORNING</h1> 
       </div> 
      </div> 

      <div style="width:90%;"> 
       <canvas id="morningChart" class="morningChart" width="400" height="400"></canvas> 
      </div> 
     </div> 

     <div class="container-week"> 
      <div class="col s12 m5"> 
       <div class="card-panel teal"> 
        <h1>EVENING</h1> 
       </div> 
      </div> 

      <div style="width:90%;"> 
       <canvas id="eveningChart" class="eveningChart" width="400" height="400"></canvas> 
      </div> 
     </div> 
</div> 

<script type="text/javascript"> 

    Chart.defaults.global.defaultFontColor = '#27AAE1' 
    Chart.defaults.global.defaultFontFamily = 'Source+Sans+Pro'; 
    Chart.defaults.global.defaultFontSize = 40; 
    Chart.defaults.global.title.fontStyle = 'lighter'; 
    Chart.defaults.global.title.text = "Weekly brushing morning"; 
    Chart.defaults.global.legend.display = false; 

    const CHART = document.getElementById("morningChart"); 

    var morningChart = new Chart(CHART, { 
     type: 'line', 
     data: { 
      labels: ["MON", "TUE", "WED", "THU", "FR", "SAT", "SUN"], 
      datasets: [ 
       { 
        label: "Weekly brushing", 
        fill: true, 
        /* lineTension: 0.2, */ 
        backgroundColor: "rgba(39,170,225,0.2)", 
        borderColor: "rgba(39,170,225,100)", 
        borderCapStyle: 'butt', 
        borderDash: [], 
        borderDashOffset: 0.0, 
        borderJoinStyle: 'miter', 
        pointBorderColor: "rgba(39,170,225,100)", 
        pointBackgroundColor: "#fff", 
        pointBorderWidth: 5, 
        pointHoverRadius: 5, 
        pointHoverBackgroundColor: "rgba(75,192,192,1)", 
        pointHoverBorderColor: "rgba(220,220,220,1)", 
        pointHoverBorderWidth: 2, 
        pointRadius: 1, 
        pointHitRadius: 10, 
        data: {{morning_weekly}}, 
        spanGaps: false, 
       } 
      ] 
     }, 
     options: { 
      scales: { 
       xAxes: [{ 
        gridLines: { 
         show: true, 
         color: "rgba(39,170,225,0.2)", 
         zeroLineColor: "rgba(39,170,225,1)" 
        }, 
        scaleLabel: { 
         display: true, 
         fontFamily: "Helvetica", 
         fontColor: "#27AAE1" 
        }, 
       }], 
       yAxes: [{ 
        gridLines: { 
         show: true, 
         color: "rgba(39,170,225,0.2)", 
         zeroLineColor: "rgba(39,170,225,1)" 
        }, 
        ticks: { 
         beginAtZero: true, 
         max: 6, 
         min: 0, 
         stepSize: 0.5 
        }, 
       }] 
      } 
     } 
    }); 
</script> 

<script type="text/javascript"> 

    Chart.defaults.global.defaultFontColor = '#27AAE1' 
    Chart.defaults.global.defaultFontFamily = 'Source+Sans+Pro'; 
    Chart.defaults.global.defaultFontSize = 40; 
    Chart.defaults.global.title.fontStyle = 'lighter'; 
    Chart.defaults.global.title.text = "Weekly brushing morning"; 
    Chart.defaults.global.legend.display = false; 

    const CHART = document.getElementById("eveningChart"); 

    var eveningChart = new Chart(CHART, { 
     type: 'line', 
     data: { 
      labels: ["MON", "TUE", "WED", "THU", "FR", "SAT", "SUN"], 
      datasets: [ 
       { 
        label: "Weekly brushing", 
        fill: true, 
        /* lineTension: 0.2, */ 
        backgroundColor: "rgba(39,170,225,0.2)", 
        borderColor: "rgba(39,170,225,100)", 
        borderCapStyle: 'butt', 
        borderDash: [], 
        borderDashOffset: 0.0, 
        borderJoinStyle: 'miter', 
        pointBorderColor: "rgba(39,170,225,100)", 
        pointBackgroundColor: "#fff", 
        pointBorderWidth: 5, 
        pointHoverRadius: 5, 
        pointHoverBackgroundColor: "rgba(75,192,192,1)", 
        pointHoverBorderColor: "rgba(220,220,220,1)", 
        pointHoverBorderWidth: 2, 
        pointRadius: 1, 
        pointHitRadius: 10, 
        data: {{morning_weekly}}, 
        spanGaps: false, 
       } 
      ] 
     }, 
     options: { 
      scales: { 
       xAxes: [{ 
        gridLines: { 
         show: true, 
         color: "rgba(39,170,225,0.2)", 
         zeroLineColor: "rgba(39,170,225,1)" 
        }, 
        scaleLabel: { 
         display: true, 
         fontFamily: "Helvetica", 
         fontColor: "#27AAE1" 
        }, 
       }], 
       yAxes: [{ 
        gridLines: { 
         show: true, 
         color: "rgba(39,170,225,0.2)", 
         zeroLineColor: "rgba(39,170,225,1)" 
        }, 
        ticks: { 
         beginAtZero: true, 
         max: 6, 
         min: 0, 
         stepSize: 0.5 
        }, 
       }] 
      } 
     } 
    }); 
</script> 

回答

相關問題