AngularJS 调用 xml 文件并处理数据

AngularJS calling xml file and processing data

我是一般 javascript 编码的新手,我在使用自定义 xml 文件创建 angular 图表时遇到了一些问题。我正在使用 x2js 将我的 xml 文件转换为 json 以便在图表数据中使用。在我的 plnkr 中,我创建了一个 angular 服务,合并了 x2js 来转换我的 xml 文件。

app.js文件中,我回调了服务中的函数,将数据解析成图表数据需要的片段。我不确定我这样做是否正确,但 value 元素中的 parseInt() 函数可能存在一个问题。我包含这个解析函数是因为我注意到我的数据值周围可能有引号,这会干扰图表库所需的数据格式。 $scope.dataOne$scope.dataTwo 是我要用来创建两个不同图形的两组数据。

注意:如果需要,我使用了 plnkr, and expanded on it to include the things I need in my code. I also used this documentation 中的 krispo 模板。

我的代码中某处似乎有错误。我没有解析所有数据,只够创建两对数据来测试图表。什么都没有出现。

任何帮助将不胜感激,因为我是编程新手,之前只使用过 python。

编辑:它还活着(在 plnkr 中)! xml 必须已正确转换,因为现在图表上方的选择菜单与来自 xml 数据的系列一起出现。但是图表没有出现。我遇到了障碍,因为当我尝试在浏览器中打开文件以查找我的错误所在时,它给了我以下错误:

XMLHttpRequest cannot load file:///C:/Users/Nicholas/Desktop/logixdata/new%20website/newchart/data.xml.
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

还有这个:

Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:...the path...'.
    at Error (native) 

我越来越接近解决方案,但我已将其缩小到 app.js 文件中 DataSource.get().success(function(data){ 代码段中的某处。

我找到了解决方案。我创建的所有 $scope 数据元素都必须在 DataSource 函数内,该函数正在回调 xml 转换服务函数。我在那里解析了所有数据。我也错过了我的符号规范 - 例如,我有 data.Header.data[0].y,其中 y 键保存图表的数据值。我忘记了 y 的值是一个数组,所以我必须为每个赋值指定我想要的值(例如 data.Header.data[0].x[1])。