Javascript Chart.js使用MySQL中的json数据
我是Chart.js的新手,我尝试了很多不同的方法,但我似乎无法解决将数据从JSON加载到条形图的问题 我正在尝试使用最新版本的chart.js显示每月费用的图表 JSON字符串如下所示:Javascript Chart.js使用MySQL中的json数据,javascript,jquery,charts,chart.js,bar-chart,Javascript,Jquery,Charts,Chart.js,Bar Chart,我是Chart.js的新手,我尝试了很多不同的方法,但我似乎无法解决将数据从JSON加载到条形图的问题 我正在尝试使用最新版本的chart.js显示每月费用的图表 JSON字符串如下所示: [{"month":"Jan","amount":"0.00"},{"month":"Feb","amount":"0.00"},{"month":"Mar","amount":"100.00"},{"month":"Apr","amount":"0.00"},{"month":"May","amount":
[{"month":"Jan","amount":"0.00"},{"month":"Feb","amount":"0.00"},{"month":"Mar","amount":"100.00"},{"month":"Apr","amount":"0.00"},{"month":"May","amount":"0.00"},{"month":"Jun","amount":"977.00"},{"month":"Jul","amount":"0.00"},{"month":"Aug","amount":"0.00"},{"month":"Sep","amount":"0.00"},{"month":"Oct","amount":"0.00"},{"month":"Nov","amount":"0.00"},{"month":"Dec","amount":"0.00"}]
$(function () {
var chartColors = {
red: 'rgba(255, 99, 132, 1)',
blue: 'rgba(54, 162, 235, 1)',
yellow: 'rgba(255, 205, 86, 1)',
green: 'rgba(75, 192, 192, 1)',
purple: 'rgba(153, 102, 255, 1)',
orange: 'rgba(255, 159, 64, 1)',
darkgrey: 'rgba(102, 102, 102, 1)',
maroon: 'rgba(200, 112, 91, 1)',
khaki: 'rgba(190, 204, 200, 1)'
};
if( $("#ChartExpenseBar").length > 0 ){
$.ajax({
type: 'POST',
url: '/expenses/',
data: {'expense_chart': 'monthly'},
success: function(data) {
var months = [];
var amount = [];
for (var i in data) {
months.push(data[i].month);
amount.push(data[i].amount);
}
var ctx = document.getElementById("ChartExpenseBar").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
//labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
labels: months,
datasets: [{
label: 'Monthly expenses',
backgroundColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange,
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange
],
borderColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange,
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange
],
borderWidth: 1,
data: amount
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
tooltips: {
displayColors: false,
callbacks: {
// use label callback to return the desired label
label: function(tooltipItem, data) {
return "£" + tooltipItem.yLabel;
},
// remove title
title: function(tooltipItem, data) {
return;
}
}
},
legend: {
display: false
},
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
},
ticks: {
beginAtZero:true,
userCallback: function(value, index, values) {
// Convert the number to a string and splite the string every 3 charaters from the end
value = value.toString();
value = value.split(/(?=(?:...)*$)/);
// Convert the array to a string and format the output
value = value.join('.');
return '£' + value;
}
}
}]
}
}
});
},
error: function() {
alert("There is a problem with loading the chart!");
}
});
}
});
我的代码如下:
[{"month":"Jan","amount":"0.00"},{"month":"Feb","amount":"0.00"},{"month":"Mar","amount":"100.00"},{"month":"Apr","amount":"0.00"},{"month":"May","amount":"0.00"},{"month":"Jun","amount":"977.00"},{"month":"Jul","amount":"0.00"},{"month":"Aug","amount":"0.00"},{"month":"Sep","amount":"0.00"},{"month":"Oct","amount":"0.00"},{"month":"Nov","amount":"0.00"},{"month":"Dec","amount":"0.00"}]
$(function () {
var chartColors = {
red: 'rgba(255, 99, 132, 1)',
blue: 'rgba(54, 162, 235, 1)',
yellow: 'rgba(255, 205, 86, 1)',
green: 'rgba(75, 192, 192, 1)',
purple: 'rgba(153, 102, 255, 1)',
orange: 'rgba(255, 159, 64, 1)',
darkgrey: 'rgba(102, 102, 102, 1)',
maroon: 'rgba(200, 112, 91, 1)',
khaki: 'rgba(190, 204, 200, 1)'
};
if( $("#ChartExpenseBar").length > 0 ){
$.ajax({
type: 'POST',
url: '/expenses/',
data: {'expense_chart': 'monthly'},
success: function(data) {
var months = [];
var amount = [];
for (var i in data) {
months.push(data[i].month);
amount.push(data[i].amount);
}
var ctx = document.getElementById("ChartExpenseBar").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
//labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
labels: months,
datasets: [{
label: 'Monthly expenses',
backgroundColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange,
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange
],
borderColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange,
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange
],
borderWidth: 1,
data: amount
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
tooltips: {
displayColors: false,
callbacks: {
// use label callback to return the desired label
label: function(tooltipItem, data) {
return "£" + tooltipItem.yLabel;
},
// remove title
title: function(tooltipItem, data) {
return;
}
}
},
legend: {
display: false
},
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
},
ticks: {
beginAtZero:true,
userCallback: function(value, index, values) {
// Convert the number to a string and splite the string every 3 charaters from the end
value = value.toString();
value = value.split(/(?=(?:...)*$)/);
// Convert the array to a string and format the output
value = value.join('.');
return '£' + value;
}
}
}]
}
}
});
},
error: function() {
alert("There is a problem with loading the chart!");
}
});
}
});
我很可能想象自己做了一件非常愚蠢的事情,导致了一个未定义的错误,我希望有人能帮我
非常感谢您。您的图表有帖子吗 试着这样做:
$.ajax({
url: '/expenses/',
async: false,
dataType: 'json',
type: "GET",
success: function (d) {
chartData = {
labels: d.AxisLabels,
datasets: [
{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
data: d.DataSets[0]
}
]
};
max = Math.max.apply(Math, d.DataSets[0]);
steps = 10;
respondCanvas();
}
});
};
你的图表上有帖子吗 试着这样做:
$.ajax({
url: '/expenses/',
async: false,
dataType: 'json',
type: "GET",
success: function (d) {
chartData = {
labels: d.AxisLabels,
datasets: [
{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
data: d.DataSets[0]
}
]
};
max = Math.max.apply(Math, d.DataSets[0]);
steps = 10;
respondCanvas();
}
});
};
代码的最小复制似乎表明它工作正常,除了两个选项
responsive
和MaintaintAspectratio
(如果图表包含在div中,它们工作正常)。将新html文件复制并粘贴到web服务器中以查看
对示例代码所做的关键更改:
- AJAX API调用已更改为从中获取/
- 添加了虚假的成功数据
responsive
和maintaintAspectratio
似乎会导致图表“抖动”,除非图表用div包装
问题可能出在其他地方,也许是服务器响应
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.js" integrity="sha256-JG6hsuMjFnQ2spWq0UiaDRJBaarzhFbUxiUTxQDA9Lk=" crossorigin="anonymous"></script>
<div style="width:500px;">
<canvas id="ChartExpenseBar" width="200" height="200"></canvas>
</div>
<script>
$(function () {
var chartColors = {
red: 'rgba(255, 99, 132, 1)',
blue: 'rgba(54, 162, 235, 1)',
yellow: 'rgba(255, 205, 86, 1)',
green: 'rgba(75, 192, 192, 1)',
purple: 'rgba(153, 102, 255, 1)',
orange: 'rgba(255, 159, 64, 1)',
darkgrey: 'rgba(102, 102, 102, 1)',
maroon: 'rgba(200, 112, 91, 1)',
khaki: 'rgba(190, 204, 200, 1)'
};
if( $("#ChartExpenseBar").length > 0 ){
$.ajax({
type: 'GET',
url: './',
data: {'expense_chart': 'monthly'},
success: function(data) {
var months = [];
var amount = [];
// fill with fake data
data = [{"month":"Jan","amount":"0.00"},{"month":"Feb","amount":"0.00"},{"month":"Mar","amount":"100.00"},{"month":"Apr","amount":"0.00"},{"month":"May","amount":"0.00"},{"month":"Jun","amount":"977.00"},{"month":"Jul","amount":"0.00"},{"month":"Aug","amount":"0.00"},{"month":"Sep","amount":"0.00"},{"month":"Oct","amount":"0.00"},{"month":"Nov","amount":"0.00"},{"month":"Dec","amount":"0.00"}];
for (var i in data) {
months.push(data[i].month);
amount.push(data[i].amount);
}
var ctx = document.getElementById("ChartExpenseBar").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
//labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
labels: months,
datasets: [{
label: 'Monthly expenses',
backgroundColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange,
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange
],
borderColor: [
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange,
chartColors.red,
chartColors.blue,
chartColors.yellow,
chartColors.purple,
chartColors.green,
chartColors.orange
],
borderWidth: 1,
data: amount
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
tooltips: {
displayColors: false,
callbacks: {
// use label callback to return the desired label
label: function(tooltipItem, data) {
return "£" + tooltipItem.yLabel;
},
// remove title
title: function(tooltipItem, data) {
return;
}
}
},
legend: {
display: false
},
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
gridLines: {
display: false
},
ticks: {
beginAtZero:true,
userCallback: function(value, index, values) {
// Convert the number to a string and splite the string every 3 charaters from the end
value = value.toString();
value = value.split(/(?=(?:...)*$)/);
// Convert the array to a string and format the output
value = value.join('.');
return '£' + value;
}
}
}]
}
}
});
},
error: function() {
alert("There is a problem with loading the chart!");
}
});
}
});
</script>
$(函数(){
变量ChartColor={
红色:“rgba(255,99,132,1)”,
蓝色:“rgba(54162235,1)”,
黄色:“rgba(255,205,86,1)”,
绿色:“rgba(75192192,1)”,
紫色:“rgba(153102255,1)”,
橙色:“rgba(255,159,64,1)”,
darkgrey:“rgba(102102102,1)”,
褐红色:“rgba(200112,91,1)”,
卡其色:“rgba(1902042001)”
};
如果($(“#ChartExpenseBar”).length>0){
$.ajax({
键入:“GET”,
url:“./”,
数据:{'expense_chart':'monthly'},
成功:功能(数据){
var月数=[];
风险值金额=[];
//用假数据填充
数据=[{“月”:“一月”,“金额”:“0.00”},{“月”:“二月”,“金额”:“0.00”},{“月”:“三月”,“金额”:“100.00”},{“月”:“四月”,“金额”:“0.00”},{“月”:“五月”,“金额”:“0.00”},{“月”:“977.00”},{“月”:“金额”:“0.00”},{“月”:“月”:“八月”,“金额”:“0.00”},{“月”:“月”:“月”:“月”:“月”:“金额”:“月”:“月”:“月”:“月”:“月”:“月”:“月”:“金额”:“月”:“月”:“月”:“月”:“月”:“月”:“月”:“月”:“金额”:“月”:“月”:,“金额”:“0.00”},{“月份”:“十二月”,“金额”:“0.00”}];
用于(数据中的var i){
月数推送(数据[i].month);
金额.推送(数据[i].金额);
}
var ctx=document.getElementById(“ChartExpenseBar”).getContext(“2d”);
var myChart=新图表(ctx{
类型:'bar',
数据:{
//标签:[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”],
标签:月,
数据集:[{
标签:“每月费用”,
背景颜色:[
红色,
黄绿色,蓝色,
黄色,
黄绿色,紫色,
绿色,
橙色,
红色,
黄绿色,蓝色,
黄色,
黄绿色,紫色,
绿色,
黄绿色
],
边框颜色:[
红色,
黄绿色,蓝色,
黄色,
黄绿色,紫色,
绿色,
橙色,
红色,
黄绿色,蓝色,
黄色,
黄绿色,紫色,
绿色,
黄绿色
],
边框宽度:1,
数据:金额
}]
},
选项:{
回答:是的,
MaintaintAspectRatio:false,
工具提示:{
显示颜色:假,
回调:{
//使用标签回调返回所需的标签
标签:函数(工具提示项、数据){
返回“£”+工具提示项.yLabel;
},
//删除标题
标题:函数(工具提示项、数据){
返回;
}
}
},
图例:{
显示:假
},
比例:{
xAxes:[{
网格线:{
显示:假
}
}],
雅克斯:[{
网格线:{
显示:假
},
滴答声:{
贝吉纳泽罗:是的,
U