我需要你的帮助来学习如何动态地将数据从json文件实现到Chartjs。我需要动态填写的字段是[root@ jasperreports-server-cp-6.4.2]# ./ctlscript.sh restart
Using CATALINA_BASE: /opt/jasperreports-server-cp-6.4.2/apache-tomcat
Using CATALINA_HOME: /opt/jasperreports-server-cp-6.4.2/apache-tomcat
Using CATALINA_TMPDIR: /opt/jasperreports-server-cp-6.4.2/apache-tomcat/temp
Using JRE_HOME: /opt/jasperreports-server-cp-6.4.2/java
Using CLASSPATH: /opt/jasperreports-server-cp-6.4.2/apache-tomcat/bin/bootstrap.jar:/opt/jasperreports-server-cp-6.4.2/apache-tomcat/bin/tomcat-juli.jar
Using CATALINA_PID: /opt/jasperreports-server-cp-6.4.2/apache-tomcat/temp/catalina.pid
Tomcat stopped.
/opt/jasperreports-server-cp-6.4.2/apache-tomcat/scripts/ctl.sh : tomcat stopped
waiting for server to shut down.... done
server stopped
/opt/jasperreports-server-cp-6.4.2/postgresql/scripts/ctl.sh : postgresql stopped
waiting for server to start.... done
server started
/opt/jasperreports-server-cp-6.4.2/postgresql/scripts/ctl.sh : postgresql started at port 5432
Using CATALINA_BASE: /opt/jasperreports-server-cp-6.4.2/apache-tomcat
Using CATALINA_HOME: /opt/jasperreports-server-cp-6.4.2/apache-tomcat
Using CATALINA_TMPDIR: /opt/jasperreports-server-cp-6.4.2/apache-tomcat/temp
Using JRE_HOME: /opt/jasperreports-server-cp-6.4.2/java
Using CLASSPATH: /opt/jasperreports-server-cp-6.4.2/apache-tomcat/bin/bootstrap.jar:/opt/jasperreports-server-cp-6.4.2/apache-tomcat/bin/tomcat-juli.jar
Using CATALINA_PID: /opt/jasperreports-server-cp-6.4.2/apache-tomcat/temp/catalina.pid
Tomcat started.
/opt/jasperreports-server-cp-6.4.2/apache-tomcat/scripts/ctl.sh : tomcat started
和labels
。
data
答案 0 :(得分:1)
我使用$.ajax()
来获取JSON数据,并将数据保存在两个数组中,然后您可以将这些数组用于图表标签和数据。希望这有帮助!
var lbl = [];
var dta = [];
$.ajax({
url: "test.json",
dataType: 'json',
async: false,
success: function(data) {
$.each(data, function(i, field){
lbl.push(field.EFICAZ_TAB_ITEM_ID
);
dta.push(field.EFICAZ_PERCENTS);
});
}
});
$.getJSON("test.json", function(result){
$.each(result, function(i, field){
lbl.push(field.EFICAZ_TAB_ITEM_ID
);
dta.push(field.EFICAZ_PERCENTS);
});
});
var ctx = document.getElementById("myCanvas").getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: lbl,
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: dta,
}]
},
// Configuration options go here
options: {}
});
答案 1 :(得分:1)
这是示例
document.addEventListener('DOMContentLoaded', function(){
var chartData = [
{
"EFICAZ_TAB_ITEM_ID":1,
"EFICAZ_PERCENTS":21
},
{
"EFICAZ_TAB_ITEM_ID":2,
"EFICAZ_PERCENTS":55
},
{
"EFICAZ_TAB_ITEM_ID":3,
"EFICAZ_PERCENTS":32
}
]
var labels = [];
var values = [];
chartData.forEach(function(el,key){
labels.push(el.EFICAZ_TAB_ITEM_ID);
values.push(el.EFICAZ_PERCENTS);
})
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: labels,
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: values,
}]
},
// Configuration options go here
options: {}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>