时间序列折线图未显示

时间:2019-04-12 07:38:58

标签: javascript chart.js

我想在此处显示图表https://www.chartjs.org/samples/latest/scales/time/financial.html

我编写了以下代码来显示图表,但是运行代码时,发生以下错误:

  

Chart.min.js?ver = 1.0.0:7未捕获的错误:此方法不是   已实现:找不到适配器,或者适配器不完整   提供了集成。       在ri.oi(Chart.min.js?ver = 1.0.0:7)       在i.update(Chart.min.js?ver = 1.0.0:7)       在Chart.min.js?ver = 1.0.0:7       在Object.each(Chart.min.js?ver = 1.0.0:7)       在Object.update(Chart.min.js?ver = 1.0.0:7)       在ni.updateLayout(Chart.min.js?ver = 1.0.0:7)       在ni.update(Chart.min.js?ver = 1.0.0:7)       在ni.construct(Chart.min.js?ver = 1.0.0:7)       在新的ni(Chart.min.js?ver = 1.0.0:7)       在initializeCustomerFlowChart(reports.js?ver = 1.0.0:83)

/**
 * JS Code for the admin-panel/contact/reports.
 * 
 * It handles initializtion of the customer flow chart.
 */

'use strict';

jQuery(document).ready(function() {

	console.log('admin-panel/contact/reports');

	// Initialize the custome flow chart.
	initializeCustomerFlowChart();

});

function initializeCustomerFlowChart() {

	var dataSet = [];	
	for(var i = 0 ; i < 20; i++) {		
		var date = new Date();
		date = date.setDate( date.getDate() + i);
		dataSet.push({ t: date, y: i.toString() });
	}	

	var data = {
		datasets: [
			{
				label: 'Customer Flow',
				backgroundColor: 'rgba(255,0,0, 0.5)',
				borderColor: 'rgb(255,0,0)',
				data: dataSet,
				type: 'line',
				pointRadius : 0,
				fill: false,
				lineTension: 0,
				broderWidth: 2				
			}
		]
	};

	var options = {
		scales: {
			xAxes: [{
				type: 'time',
				distribution: 'series',
				ticks: {
					source: 'data',
					autoSkip: true
				}
			}],
			yAxes: [{
				scaleLabel: {
					display: true,
					labelString: 'No. Of Customer'
				}
			}]
		},
		tooltips: {
			intersect: false,
			mode: 'index',
			callbacks: {
				label: function(tooltipItem, myData) {
					var label = myData.datasets[tooltipItem.datasetIndex].label || '';
					if (label) {
						label += ': ';
					}
					label += parseFloat(tooltipItem.value).toFixed(2);
					return label;
				}
			}
		}
	};

	var config = {
		type: 'bar',
		data: data, 
		options: options
	};	

	var context = document.getElementById('contact-reports-customer-flow-chart').getContext('2d');
	var chart = new Chart(context, config);

}
<canvas id="contact-reports-customer-flow-chart"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>

1 个答案:

答案 0 :(得分:3)

您使用的是过时的图表库,或者不包含适当的图表库,所以我不确定您的实际代码中使用了什么。下面的代码为我工作。只需添加ChartJs捆绑文件即可加载所有依赖项。

/**
 * JS Code for the admin-panel/contact/reports.
 * 
 * It handles initializtion of the customer flow chart.
 */

'use strict';

jQuery(document).ready(function() {

    console.log('admin-panel/contact/reports');

    // Initialize the custome flow chart.
    initializeCustomerFlowChart();

});

function initializeCustomerFlowChart() {

    var dataSet = [];   
    for(var i = 0 ; i < 20; i++) {      
        var date = new Date();
        date = date.setDate( date.getDate() + i);
        dataSet.push({ t: date, y: i.toString() });
    }   

    var data = {
        datasets: [
            {
                label: 'Customer Flow',
                backgroundColor: 'rgba(255,0,0, 0.5)',
                borderColor: 'rgb(255,0,0)',
                data: dataSet,
                type: 'line',
                pointRadius : 0,
                fill: false,
                lineTension: 0,
                broderWidth: 2              
            }
        ]
    };

    var options = {
        scales: {
            xAxes: [{
                type: 'time',
                distribution: 'series',
                ticks: {
                    source: 'data',
                    autoSkip: true
                }
            }],
            yAxes: [{
                scaleLabel: {
                    display: true,
                    labelString: 'No. Of Customer'
                }
            }]
        },
        tooltips: {
            intersect: false,
            mode: 'index',
            callbacks: {
                label: function(tooltipItem, myData) {
                    var label = myData.datasets[tooltipItem.datasetIndex].label || '';
                    if (label) {
                        label += ': ';
                    }
                    label += parseFloat(tooltipItem.value).toFixed(2);
                    return label;
                }
            }
        }
    };

    var config = {
        type: 'bar',
        data: data, 
        options: options
    };  

    var context = document.getElementById('contact-reports-customer-flow-chart').getContext('2d');
    var chart = new Chart(context, config);

}
<canvas id="contact-reports-customer-flow-chart"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>

希望这可以解决问题。