社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

如何使用从jquery获得的数据,然后在for循环的var中使用它?

Attaroqqi • 6 年前 • 2056 次点击  

如何在变量中使用数据和for循环?我想使用变量内部的for循环将数据赋给变量,这样数据将赋给变量内部的多维数组

window.onload = function () {

var nape = ["per a", "per b"];
var dp = [{'x':3,'y':5}];
var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    title:{
        text: "Server Performance"
    },
    axisX: {
        title:"Server Load (in TPS)"
    },
    axisY:{
  labelFormatter: function(e){
    if(e.value==0){
          a="Provinsi"        
                return  a;
        }else if(e.value==1){
         a="Aceh"       
                return  a;
        }else if(e.value==2){
          a="Maluku"       
                return  a;
        }else if(e.value==3){
          a="NTT"       
                return  a;
        }else if(e.value==4){
          a="NTB"       
                return  a;
        }else if(e.value==5){
          a="Jakarta"       
                return  a;
        }else if(e.value==6){
          a="Sumatera"       
                return  a;
        }else if(e.value==7){
          a="Kalimantan"       
                return  a;
        }
  },      
        title: "Response Time (in ms)",
    interval: 1,    
    },
    data: [{
        "type": "scatter",
        toolTipContent: "<span>{name}</span><br/> Load: {x} TPS<br/> Response Time:</span> {y} ms",
        name: nape[0],
        showInLegend: true,
        dataPoints: [
            { x: 23, y: 1 },
            { x: 28, y: 2 },
            { x: 39, y: 3 },
            { x: 34, y: 4 },
            { x: 24, y: 5 },
            { x: 29, y: 6 },
            { x: 29, y: 1 },
            { x: 23, y: 2 },
            { x: 27, y: 3 },
            { x: 34, y: 4 },
            { x: 36, y: 5 },
            { x: 33, y: 6 },
            { x: 32, y: 1 },
            { x: 21, y: 2 }
        ]
    },
    {
        type: "scatter",
        name: nape[1],
        showInLegend: true, 
        toolTipContent: "<span style=\"color:#C0504E \"><b>{name}</b></span><br/><b> Load:</b> {x} TPS<br/><b> Response Time:</b></span> {y} ms",
        dataPoints: dp
    }]
});
chart.render();

}

所以我想做的是从jquery中获取数据,然后使用for循环将数据发布到var图表中的“data”

...
$.ajax({
url: "the_url",
method: "GET",
success: function(data) {
var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    title:{
        text: "Server Performance"
    },
    axisX: {
        title:"Server Load (in TPS)"
    },
    axisY:{
  labelFormatter: function(e){
    if(e.value==0){
          a="Provinsi"        
                return  a;
        }else if(e.value==1){
         a="Aceh"       
                return  a;
        }else if(e.value==2){
          a="Maluku"       
                return  a;
        }else if(e.value==3){
          a="NTT"       
                return  a;
        }else if(e.value==4){
          a="NTB"       
                return  a;
        }else if(e.value==5){
          a="Jakarta"       
                return  a;
        }else if(e.value==6){
          a="Sumatera"       
                return  a;
        }else if(e.value==7){
          a="Kalimantan"       
                return  a;
        }
  },      
        title: "Response Time (in ms)",
    interval: 1,    
    },
data: for(var i=0; i < data.length ; i++){
        "type": "scatter",
        toolTipContent: "<span>{name}</span><br/> Load: {x} TPS<br/> Response Time:</span> {y} ms",
        name: data[i].somedata,
        showInLegend: false,
        dataPoints: data[i].anothersomedata
    }
...

所以在“data:”中会有这样的返回值

...
[{
        type: "scatter",
        toolTipContent: "<span style=\"color:#4F81BC \"><b>{name}</b></span><br/><b> Load:</b> {x} TPS<br/><b> Response Time:</b></span> {y} ms",
        name: "Server Pluto",
        showInLegend: false,
        dataPoints: [
            { x: 23, y: 1 },
            { x: 28, y: 2 },
            { x: 39, y: 3 },
            { x: 34, y: 4 },
            { x: 24, y: 5 },
            { x: 29, y: 6 },
            { x: 29, y: 1 },
            { x: 23, y: 2 },
            { x: 27, y: 3 },
            { x: 34, y: 4 },
            { x: 36, y: 5 },
            { x: 33, y: 6 },
            { x: 32, y: 1 },
            { x: 21, y: 2 }
        ]
    },
    {
        type: "scatter",
        name: "Server Mars",
        showInLegend: false, 
        toolTipContent: "<span style=\"color:#C0504E \"><b>{name}</b></span><br/><b> Load:</b> {x} TPS<br/><b> Response Time:</b></span> {y} ms",
        dataPoints: [
            { x: 200, y: 3 },
            { x: 27, y: 4 },
            { x: 35, y: 5 },
            { x: 32, y: 6 },
            { x: 29, y: 1 },
            { x: 22, y: 2 },
            { x: 27, y: 3 },
            { x: 26, y: 4 },
            { x: 24, y: 5 },
            { x: 33, y: 6 },
            { x: 34, y: 1 },
            { x: 30, y: 2 },
            { x: 37, y: 3 },
            { x: 24, y: 4 }
        ]
    }]
...

我已经试了好几种方法,但似乎唉,有人能帮我吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53601
文章 [ 1 ]  |  最新文章 6 年前