javascript - Radar Chart (chart.js 2.5.0) Creating 2nd layer using database values -
i'm using chartjs create radar chart show scores of modules. working 1 module when enter 2nd module data placed in same dataset causing labels double. value points not changing name keeping first modules point label having different values.
this function used create graph, if data undefined creates chart if not adds data dataset.
<script src=//code.jquery.com/jquery-3.2.1.min.js></script> <script src=https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.5.0/chart.bundle.js></script> <input id=mod_code value=set08108><button id='go'>go</button> <canvas id="mychart" width="300" height="300"></canvas> <script> var data; $(function(){ $('#go').click(function(){ $.ajax({url:'output.php', data {mod_code:$('#mod_code').val()},datatype:'json', success:function(d){ if(data===undefined) data = {labels:[],datasets:[{label:$('#mod_code').val()+' results',data:[],}]}; else { data.datasets.push({label:$('#mod_code').val()+' results',data:[],}); } for(var i=0;i<d.length;i++){ data.labels.push(d[i][0]); data.datasets[0].data.push(parsefloat(d[i][1])); } var ctx = document.getelementbyid("mychart"); var mychart = new chart(ctx, { type: 'radar', data:data, options:{ responsive:false, scale:{ticks:{beginatzero: true}}}}); }}); }) }); </script>
this the php file gets data based on module code database, converts json format dataset.
<?php if(!array_key_exists('mod_code',$_request)){ print "<form><input name=mod_code></form>"; exit(); } $con = new mysqli(''); if (mysqli_connect_errno()) { printf("connection failed: %s\n", mysqli_connect_error()); exit(); } $sql = " select que_code, 100*avg(case when res_valu in (4,5) 1 else 0 end) v ins_res mod_code = ? group que_code order que_code "; $stmt = $con->prepare($sql) or die($con->error); $stmt->bind_param('s',$_request['mod_code']) or die('bind error'); $stmt->execute(); $res = $stmt->get_result() or die('get_result failed: '.$con->error); print json_encode($res->fetch_all());
Comments
Post a Comment