将javascript变量传递到融合图表构造函数中
Passing a javascript variable into a fusion chart constructor
除了在图表中加载数据外,其他一切都正常。当我查看页面源时,它显示的是ajaxData,而不是变量包含的数据数组。
我试过JSON。Stringify,这个。,以及一些其他方法。我在fusion chart网站上找不到任何用AJAX加载数据的例子,他们所有的例子都涉及到将javascript放在PHP等服务器端代码中,这是我宁愿避免的。我只想用ajax加载数据并将其转储到图表中。
$.ajax($assetsSearchUrl, {
dataType: "text",
accepts: {text: "application/json"},
success: loadChart
});
function loadChart(ajaxData)
{
var ageGroupChart = new FusionCharts({
type: 'pie2d',
renderAt: 'chartId',
width: '400',
height: '275',
dataFormat: 'json',
theme: 'ocean',
dataSource: {
"chart": {
"caption": "Assets",
....
"showLegend": "0"
},
data : ajaxData
}
}).render();
}
没有动态数据的工作示例。
FusionCharts.ready(function () {
var ageGroupChart = new FusionCharts({
type: 'pie2d',
renderAt: 'chart-container',
width: '450',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Split of Visitors by Age Group",
....
"legendItemFontColor": '#666666'
},
"data": [
{
"label": "Teenage",
"value": "1250400"
},
{
"label": "Senior",
"value": "491000"
}
]
}
}).render();
});
我想我得到了你所希望的。我使用JSON.parse
将json文件字符串转换为对象,不需要PHP
。希望这能有所帮助!
var jsonUrl = "https://api.myjson.com/bins/1jq6x";
FusionCharts.ready(function() {
$.ajax(jsonUrl, {
dataType: "text",
accepts: {
text: "application/json"
},
success: loadChart
});
function loadChart(jsonString) {
var ageGroupChart = new FusionCharts({
type: 'pie2d',
renderAt: 'chart-container',
width: '450',
height: '300',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Split of Visitors by Age Group",
"legendItemFontColor": '#666666'
},
"data": JSON.parse(jsonString)
}
}).render();
}
});
JSFiddle
相关文章:
- ES6构造函数返回基类的实例
- 使用Google Visualization动态调用构造函数
- javascript中对象构造函数中的var属性与this.properties
- 理解typescript中的构造函数接口
- 为什么构造函数不是构造函数
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 拦截对构造函数的调用
- 使用闭包共享构造函数参数
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 从js引擎的角度来看闭包和构造函数是如何工作的
- 如何使用此从对象访问构造函数
- Javascript:为什么是构造函数's __proto__属性Empty(){}
- 当一个重要的构造函数参数丢失时应该发生什么
- Chrome Javascript日期构造函数错误
- 如何使用构造函数's的输出,以便将值插入到对象中
- 为什么在调用父构造函数时在[]中发送数据
- 构造函数函数闭包变量
- ES6类是否与构造函数相同
- 构造函数中的事件处理程序与构造函数外的事件处理函数的行为不同
- 将javascript变量传递到融合图表构造函数中