I'm having an issue with High Charts I don't understand. I'm able to download the chart from the browser but it does not display.
Here is my javascript file.
chart.js
var cpvmPartners = [];
var cpvmPlannedCPM = [];
graphData = []
$(function(){
$.getJSON('/cpvmdata', function(data) {
for(k in data){
graphData.push([data[k]['partner'],data[k]['plannedcpm']])
}
});
$(function () {
$('#cpvmSummary').highcharts({
chart: {
type: 'column'
},
title: {
text: 'World\'s largest cities per 2014'
},
subtitle: {
text: 'Source: <a href="http://en.wikipedia.org/wiki/List_of_cities_proper_by_population">Wikipedia</a>'
},
xAxis: {
type: 'category',
labels: {
rotation: -45,
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
},
yAxis: {
min: 0,
title: {
text: 'Population (millions)'
}
},
legend: {
enabled: false
},
tooltip: {
pointFormat: 'Population in 2008: <b>{point.y:.1f} millions</b>'
},
series: [{
name: 'Population',
data: graphData ,
dataLabels: {
enabled: true,
rotation: -90,
color: '#FFFFFF',
align: 'right',
format: '{point.y:.1f}', // one decimal
y: 10, // 10 pixels down from the top
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
}]
});
});
});
This is the html
<div class="container">
<!-- Portfolio Item Heading -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">CPVM Summary
<!-- <small>CPVM Summary</small> -->
</h1>
</div>
</div>
<!-- jQuery -->
<script src="js/jquery.js"></script>
<script src="js/cpvmGraphs.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- High Charts -->
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
Not sure what else to do at this point pr why it is loading in one place but not the other, any info would be greatly appreciated.
答案 0 :(得分:3)
这是因为您在DOM准备好的数据中获取数据&#39;功能,然后在另一个图表中呈现图表。您的数据获取是异步的,因此在它返回之前它不存在,但您在此之前渲染图表。
要解决此问题,请将所有图表内容放在一个单独的函数中,假设我们称之为renderChart,然后在获得JSON之后,在另一个函数中,渲染图表传递数据。
以下是文档中的示例:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'spline'
},
series: [{}]
};
$.getJSON('data.json', function(data) {
options.series[0].data = data;
var chart = new Highcharts.Chart(options);
});
});
http://www.highcharts.com/docs/working-with-data/custom-preprocessing#3