gpt4 book ai didi

javascript - 将Chart.JS的DataSource设置为Array

转载 作者:行者123 更新时间:2023-12-01 00:45:48 26 4
gpt4 key购买 nike

早上 - 我正在 try catch 数组的值。意思是,我想忽略文本 MonthlySaleAmount 但捕获数值。这就是我的数组的样子

(19) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {MonthlySaleAmount: "45364"}
1: {MonthlySaleAmount: "242421"}
2: {MonthlySaleAmount: "37654"}
3: {MonthlySaleAmount: "139038"}

并使用chart.js,我尝试将数据设置为data: information,但这在我的图表上显示没有数据

最佳答案

我必须使用图表JS并遇到这个问题,这很简单

 data = value.x.map(function (a) { return a.x; }); 

如果我记得的话,其中 x 是值的名称

尝试使用console.log来尝试

这是我填充图表实例的完整代码

 function GetGeometriaRegistos() {

$.ajax({
url: '/Geometrias/GetAll',
dataType: "json",
method: "get",
data: { id: $('#MatrizId').val()},
success: function (response) {

var isBlocked = response.isBlocked;
var grafico = response.grafico;

if (isBlocked) {
$('.blocked-overlay').fadeIn('slow');
}

let data;
var graficos = '';
var dataLabels = [];

componentes = grafico.map(function (a) { return a.componente; });

$.each(grafico, function (index, value) {
graficos += '<div class="frame">';
graficos += '<div class="content">';
graficos += '<canvas id="chart' + index + '" class="d-block w-100"></canvas>';
graficos += '</div>';
graficos += '</div>';
$('#content').html(graficos);
})

$.each(grafico, function (index, value) {

console.log(value);
data = value.x.map(function (a) { return a.x; });

$.each(data, function (index, value) {

dataLabels[index] = 'X' + (index + 1);

});


var ctx = document.getElementById('chart' + index).getContext('2d');

var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: data,
datasets: [{
label: value.componente,
data: data,
borderColor: ['rgba(54, 162, 235, 1)'],
backgroundColor: 'blue',
borderWith: 1,
fill: false,
pointRadius: 5,
pointHoverRadius: 10
}],
},
options: {
responsive: true,
responsiveAnimationDuration: 400,
maintainAspectRatio: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 0.5,
},
scaleLabel: {
display: true,
labelString: 'Y'
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Médias'
}
}]
},
annotation: {
drawTime: 'beforeDatasetsDraw',
annotations: [{
id: 'hline3',
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: value.toleranciaInferior,
borderColor: 'red',
borderWidth: 1,
label: {
backgroundColor: "red",
enabled: true
},
},
{
id: 'hline2',
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: value.toleranciaSuperior,
borderColor: 'red',
borderWidth: 1,
label: {
backgroundColor: "red",
enabled: true
}
}],

}
}
});
});
},
error: function (response) {
swal("Erro", response.responseText, "error");
}
});
}

这也是为多个图形而不仅仅是一个而设计的

关于javascript - 将Chart.JS的DataSource设置为Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57377269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com