简体   繁体   English

如何从2个数组设置饼图系列数据

[英]How to set pie chart series data from 2 arrays

I am using a epiechart js from Gentellela Alela, but I want to generate a dynamic chart with the data I got from mysql, I then made a query on php then i passed the data to javascript. 我正在使用来自Gentellela Alela的epiechart js,但我想生成一个动态图表,其中包含我从mysql获取的数据,然后我在php上查询然后我将数据传递给javascript。 I am so close but was tired searching after for hours. 我太近了,但是几个小时后我一直很累。 I want to insert values from my variable countData and names from genData. 我想从我的变量countData和genData中插入值。 This is the default series: 这是默认系列:

series: [{
      name: '访问来源',
      type: 'pie',
      radius: '55%',
      center: ['50%', '48%'],
      data: [{
        value: 335,
        name: 'Direct Access'
      }, {
        value: 310,
        name: 'E-mail Marketing'
      }]

I tried to set it like this, values are loaded properly but the names are not loaded: 我尝试像这样设置,值正确加载但名称未加载:

series: [{
      name: 'Genres in use',
      type: 'pie',
      radius: '55%',
      center: ['50%', '48%'],
      data: countData,genData
    }]

I've tried looping, changing how i set the data and a bunch of other stuff which didnt work but I am really unfamiliar to javascript syntax and functions. 我已经尝试了循环,改变了我设置数据的方式以及其他一些不起作用的东西,但我对javascript语法和函数并不熟悉。 I failed to figure out how to make this work so I'm asking for help 我没弄弄出如何做这项工作所以我正在寻求帮助

With the help of my close friend, I found out the solution was so simple. 在我的好朋友的帮助下,我发现解决方案非常简单。 :) Using a looped array push, I was able to make an array that is of the same format that the script was expecting :)使用循环数组推送,我能够创建一个与脚本期望的格式相同的数组

<script>

  var series = [];

  for(var i = 0; i < countData.length; i++)
  {
    var item = { value: countData[i], name: genData[i] };
    series.push(item);
  }

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM