未捕获的类型错误:无法读取 vega.js 中未定义的属性“标记类型”
Uncaught TypeError: Cannot read property 'marktype' of undefined in vega.js
我用vega写了一个词云.js来处理频率的单词。在附加到规范之前,单词集如下所示:
[{"文本":"真的","值":40},{"文本":"人","值":10}]
但是当它附加到规范时,它看起来像这样。
{
"name":"wordcloud",
"width":698.181818,
"height":198.181818,
"padding":{
"top":0,
"bottom":0,
"left":0,
"right":0
},
"data":[
{
"name":"table",
"values":"[{'"text'":'"really'",'"value'":40},{'"text'":'"people'",'"value'":10}]",
"transform":[
{
"type":"wordcloud",
"text":"data.text",
"font":"Helvetica Neue",
"fontSize":"data.value",
"rotate":{
"random":[-90,-45,0,45,90]
}
}
]
}
],
"marks":[
{
"type":"text",
"from":{
"data":"table"
},
"properties":{
"enter":{
"x":{
"field":"x"
},
"y":{
"field":"y"
},
"angle":{
"field":"angle"
},
"align":{
"value":"center"
},
"baseline":{
"value":"alphabetic"
},
"font":{
"field":"font"
},
"fontSize":{
"field":"fontSize"
},
"text":{
"field":"data.text"
}
},
"update":{
"fill":{
"value":"#f48fb1"
}
},
"hover":{
"fill":{
"value":"#f00"
}
}
}
}
]
}
代码还给出了两个错误:
未捕获的类型错误:无法读取行中未定义的属性"值".js 4965
未捕获的类型错误:无法读取行中未定义的属性"标记类型".js 9604
谁能帮我解决这个问题?
为词云编写的 javascript 函数。
var textData="";
function drawPersonWordCloud(cloudDiv ,Pname ,color){
getPersonDataCloud( cloudDiv,Pname ,color );
setInterval(function() {
// Do something every 5 minutes
getPersonDataCloud( cloudDiv,Pname ,color );
}, 300000);
}
function updatePersonText( newtext, cloudDiv,color){
var cloudDivID ="#"+cloudDiv;
var width = $(cloudDivID).width();
var height = $(cloudDivID).height();
console.log(JSON.stringify(newtext));
//var colorset =[color,"#6d4c41","#000000"];
//console.log(width);
var text={
"name": "wordcloud",
"width": width,
"height": height,
"padding": {"top":0, "bottom":0, "left":0, "right":0},
"data": [
{
"name": "table",
"values": newtext,
"transform": [
{
"type": "wordcloud",
"text": "data.text",
"font": "Helvetica Neue",
"fontSize": "data.value",
"rotate": {"random": [-90,-45,0,45,90]}
}
]
}
],
"marks": [
{
"type": "text",
"from": {"data": "table"},
"properties": {
"enter": {
"x": {"field": "x"},
"y": {"field": "y"},
"angle": {"field": "angle"},
"align": {"value": "center"},
"baseline": {"value": "alphabetic"},
"font": {"field": "font"},
"fontSize": {"field": "fontSize"},
"text": {"field": "data.text"}
},
"update": {
"fill": {"value": color}
},
"hover": {
"fill": {"value": "#f00"}
}
}
}
]
};
return text;
}
function getPersonDataCloud(cloudDiv, Pname,color){
var cloudDivID="#"+cloudDiv;
var newTestString=" ";
var JSONObj = new Object();
var Candidates = { Choose : Pname};
$.ajax({
url: "js/candidateCloud.jag",
dataType: "json",
contentType:'application/json',
data: JSON.stringify(Candidates),
type: "POST",
success: function (data) {
var TextData=JSON.stringify(data);
var res = TextData.split(";");
var jsonStr='{"values":[]}';
JSONObj=JSON.parse(jsonStr);
for(var i=1;i<res.length-1;i++){
var text=res[i];
var array= text.split(":");
var str= '{"text": "'+array[0]+'", "value":'+(array[1]*10)+'}';
JSONObj["values"].push(JSON.parse(str));
}
console.log(JSON.stringify(JSONObj["values"]));
var newcloud =updatePersonText(JSONObj["values"],cloudDiv,color);
console.log(JSON.stringify(newcloud));
var viewUpdateFunction = (function(chart) {
this.view = chart({el:cloudDivID}).update();
}).bind(this);
vg.parse.spec(newcloud, viewUpdateFunction);
}
});
}
Vega 文档指出"值"接受 JSON。在您的情况下,新文本在某种程度上是字符串。尝试将解析代码替换为以下示例。
...
$.ajax({
url: "js/candidateCloud.jag",
dataType: "json",
contentType:'application/json',
data: JSON.stringify(Candidates),
type: "POST",
success: function (data) {
var TextData=JSON.stringify(data);
var res = TextData.split(";");
//var jsonStr='{"values":[]}';
JSONObj=[];
for(var i=1;i<res.length-1;i++){
var text=res[i];
var array= text.split(":");
var str= '{"text": "'+array[0]+'", "value":'+(array[1]*10)+'}';
JSONObj.push(JSON.parse(str));
}
console.log(JSON.stringify(JSONObj["values"]));
var newcloud =updatePersonText(JSONObj,cloudDiv,color);
console.log(JSON.stringify(newcloud));
var viewUpdateFunction = (function(chart) {
this.view = chart({el:cloudDivID}).update();
}).bind(this);
vg.parse.spec(newcloud, viewUpdateFunction);
}
});
...
相关文章:
- 如何更改<svg>标记为<img>用js标记
- angular.js没有'无法在PhoneGap中处理视图标记
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 在标记mapbox.js上添加自定义图标
- jquery对标记类型和id进行过滤
- 使用 FileReader 加载 CSV 以创建地图标记的 JS 对象 (地图 API)
- php包含包含html标记和js脚本的文件
- 根据类型通过 HTML ID 标记进行 JS 搜索是否有限制
- 未捕获的类型错误:无法读取 vega.js 中未定义的属性“标记类型”
- 谷歌融合表 更改单个标记类型
- 谷歌地图v3从某些标记类型中删除信息窗口
- 如何使用jquery/javascript替换dom标记类型
- 创建一个字段以接受标记HTML/JS
- 语法错误,意外的标记='在js函数中
- 谷歌地图多标记与JS和JSTL
- 如何在加载了标记使用JS或Jquery
- 如何隐藏集群中的标记(Mapbox.js / leafmarkercluster)
- 如何根据标记类型设置不同颜色的标记,以及如何确定它们的坐标?我使用openlayers.org库
- 如何在经典ASP中分离活动环境和测试环境?(请在取消标记jQuery/JS之前阅读)
- 如何找到最近的标记leaflet.js