使用chart.js数据表中的用户输入表创建折线图

问题描述 投票:0回答:1

我正在获取用户使用Java脚本创建的用户输入表,以在其中添加数据。我从表中获取数据并尝试使用chartjs库绘制图表,但无法绘制图表。我尝试了以下操作,但是我在表的第一列中获取了画布ID。我有点难过。任何帮助表示赞赏。

 <html>
 <head>
 <title>Index File</title   

</head>
 <body>
<script src="dist/js/chartjs/Chart.min.js"></script>
<div id="POItablediv">
<p>
<input type="button" id="bt" value="Submit Data" onclick="submit()" />
</p>
<input type="button" onclick="insRow()" id="addPOIbutton" value="Add values"/><br/><br/>
<table id="POITable" border="1">
<thead>
    <tr>
        <td>WEEK NO</td>
        <td>alpha</td>
        <td>beta</td>
        <td>Delete?</td>

    </tr>
    </thead>
    <tbody>
    <tr>
        <td>1</td>
        <td><input size=25 type="text" id="latbox"/></td>
        <td><input size=25 type="text" id="lngbox"/></td>
        <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td>

    </tr>
    <tbody>
</table>
<canvas id="pieChart"></canvas> 

<script type="text/javascript">

    function deleteRow(row)
    {
        var i=row.parentNode.parentNode.rowIndex;
        if(i>1){
        document.getElementById('POITable').deleteRow(i);
        }

    }


    function insRow()
    {

        var x=document.getElementById('POITable');
        var new_row = x.rows[1].cloneNode(true);
        var len = x.rows.length;
        new_row.cells[0].innerHTML = len;

        var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
        inp1.id += len;
        inp1.value = '';
        var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
        inp2.id += len;
        inp2.value = '';
        x.appendChild( new_row );
    }
   function submit() 
   {
     var table = document.getElementById("POITable")
     var tableLen = table.rows.length           
     var data = {labels: [], alpha: [], beta: []
   }

   for (var i = 1; i < tableLen; i++) 
  {
     data.labels.push(table.rows[i].cells[0].innerText)
     data.alpha.push(table.rows[i].cells[1].innerText.replace(',',''))
     data.beta.push(table.rows[i].cells[2].innerText)
  }
    var canvasP = document.getElementById("pieChart")
    var ctxP = canvasP.getContext('2d')
    var myPieChart = new Chart(ctxP, {type: 'line',
    data: 
    { 
        labels: data.labels,
       datasets: [{data: data.alpha,}]
    },
      options: {
      legend: {
      display: true,
      position: "right"
            }
            }
            });
             }

    </script>
javascript html chart.js
1个回答
0
投票

您是否在dist文件夹中包含chartjs库?您可以尝试使用CDN链接-

 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.