【问题标题】:Set DataSource Of Chart.JS To Array [duplicate]将 Chart.JS 的数据源设置为数组
【发布时间】:2019-08-06 13:31:15
【问题描述】:

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

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

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

【问题讨论】:

  • array.map(a => +a.MonthlySaleAmount)
  • jquery 没有被标记
  • for(var item in arr) { arr[item].MonthlySaleAmount = +arr[item].MonthlySaleAmount }
  • @adiga - 如果我做信息 = array.map(a => +a.MonthlySaleAmount);控制台显示错误:Uncaught ReferenceError: array is not defined
  • 当然,您需要将array 替换为存储您发布的数组的任何变量

标签: javascript chart.js chart.js2


【解决方案1】:

我不得不使用图表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");
                }
            });
        }

这也适用于多个图形,而不仅仅是一个

【讨论】:

  • 你发布的函数是内置函数吗?
  • 对不起,它只是一个在页面加载时调用并发出ajax请求并构建图表的函数,整个过程在ajax请求的成功函数中。该图表是在第一个 foreach 的 html 中注入的,因为我正在构建许多图表,但是您可以看到 var myChart = new Chart 的定义时间以及我如何使用我的数据设置选项。另外我正在使用chartjs的注释插件,如果你需要使用已经定义了选项,但你必须下载js文件fort chart-js-annotations
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2019-09-23
  • 2014-01-14
相关资源
最近更新 更多