dongwei2610 2016-04-28 05:23
浏览 93
已采纳

使用mysql数据创建条形图

I want create bar using mysql data which is returned in JSON Format.

[
  {
    "Score": "84",
    "Trainer": "Jamshaid",
    "Subject Name": "Java"
  },
  {
    "Score": "50",
    "Trainer": "Zaid",
    "Subject Name": "Android"
  },
  {
    "Score": "40",
    "Trainer": "Sajjad",
    "Subject Name": "iOS"
  },
  {
    "Score": "50",
    "Trainer": "Jamshaid",
    "Subject Name": "iOS"
  }
]

I want to create like this chart

But problem is that this gets formatted from Table. But I have data in JSON as shown above.

Here is the link I am following. Can we convert the JSON Data in a Format so that it can populate chart based on the Data as shown

https://blueflame-software.com/bar-chart-with-data-from-mysql-using-highcharts/

  • 写回答

1条回答 默认 最新

  • dtmm0148603 2016-04-28 10:35
    关注

    You can process the JSON in the Javascript to build the data required for the chart as shown below:

    var jsonFromServer = [
      {
        "Score": "84",
        "Trainer": "Jamshaid",
        "Subject Name": "Java"
      },
      {
        "Score": "50",
        "Trainer": "Zaid",
        "Subject Name": "Android"
      },
      {
        "Score": "40",
        "Trainer": "Sajjad",
        "Subject Name": "iOS"
      },
      {
        "Score": "50",
        "Trainer": "Jamshaid",
        "Subject Name": "iOS"
      }
    ];
    

    The above is data from server captured in a variable

    The below code helps you to extract the Y axis Values (i.e the unique subject names) from the JSON response:

    function getSubjects(){
        var subjectsMap = {};
      $.each(jsonFromServer, function(index, value){
        if(!subjectsMap[value['Subject Name']]){
            subjectsMap[value['Subject Name']] = 1;
        }
      });
      return $.map(subjectsMap, function(index, val){ return val; });
    }
    

    Then we need to extract the Scores for each trainer in different subject which should be of form: [{name: "Trainer", data: []}] where data is an array of scores in subject whose order should be same as the order of subjects appearing in the Y axis data. Which can be achieved using the below code:

    function getTrainers(){
        var trainersMap = {};
      $.each(jsonFromServer, function(index, value){
        if(!trainersMap[value['Trainer']]){
            trainersMap[value['Trainer']] = 1;
        }
      });
      return $.map(trainersMap, function(index, val){ return val; });
    }
    function getMarks(){
      var subjects = getSubjects();
      var trainers= getTrainers();
      var marks = {};
      //initialize the trainers scores in all subjects to 0
       $.each(trainers, function(index, trainer){
        if ( !marks[trainer]){
            marks[trainer] = {};
        }
        $.each(subjects, function(idx2, sub){
            marks[trainer][sub] = 0;
        });
      });
    
      //now populate with the actual values obtained from server
      $.each(subjects, function(idx2, sub){
        $.each(jsonFromServer, function(index, value){
            var trainer = value['Trainer'];
            var subName = value['Subject Name'];
          var score = value['Score'];
          if ( sub == subName){
                marks[trainer][sub] = score;  
          }
        });
    
      });
    
      //now format the marks object into the format required for highcharts
      //i.e an array of object with properties [{name: "", data:[]}]
      return $.map(marks, function(val, index){ 
        var scoreArray = [];
        $.each(val, function(index, score){
            scoreArray.push(parseInt(score));
        });
        return {"name": index, "data": scoreArray}; 
      });
    
    }
    

    The working code can be found in the fiddle here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器