2017-08-10 26 views
1

如何從JSON獲取輸入到繪製圖形

<html> 
 
<head> 
 
<title>JS Charts</title> 
 
</head> 
 
<script type="text/javascript"> 
 
    var Apple = []; 
 
    var Samsung = []; 
 
    var Nokia = []; 
 
    function loadJSON(callback) { 
 
    var xobj = new XMLHttpRequest(); 
 
    xobj.overrideMimeType("application/json"); 
 
    xobj.open('GET', 'https://api.myjson.com/bins/1igag', true); 
 
    xobj.onreadystatechange = function() { 
 
     if (xobj.readyState == 4 && xobj.status == "200") { 
 
      callback(xobj.responseText); 
 
     } 
 
    } 
 
    xobj.send(null); 
 
} 
 
loadJSON(function(response) { 
 
    var response; 
 
    var field=JSON.parse(response); 
 
    for (var i = 0; i < field.length; i++) { 
 
     Apple.push(field[i].xxx); 
 
     Samsung.push((field[i].xxx)+10); 
 
     Nokia.push((field[i].xxx)-30); 
 
     } 
 
    sections = 12; 
 
\t Val_max = 130; 
 
\t Val_min = -40; 
 
\t var stepSize = 10; 
 
\t var columnSize = 50; 
 
\t var rowSize = 50; 
 
\t var margin = 10; 
 
\t var xAxis = [" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
 
\t canvas = document.getElementById("canvas"); 
 
\t context = canvas.getContext("2d"); 
 
\t context.fillStyle = "#0099ff" 
 
\t context.font = "20 pt Verdana" 
 
\t yScale = (canvas.height - columnSize - margin)/(Val_max - Val_min); 
 
\t xScale = (canvas.width - rowSize)/sections; 
 
\t context.strokeStyle="#009933"; // color of grid lines 
 
\t context.beginPath(); 
 
    // print Parameters on X axis, and grid lines on the graph 
 
\t for (i=1;i<=sections;i++) { 
 
\t \t var x = i * xScale; 
 
\t \t context.fillText(xAxis[i], x,columnSize - margin); 
 
\t \t context.moveTo(x, columnSize); 
 
\t \t context.lineTo(x, canvas.height - margin); 
 
\t } 
 
    // print row header and draw horizontal grid lines 
 
\t var count = 0; 
 
\t for (scale=Val_max;scale>=Val_min;scale = scale - stepSize) { 
 
\t \t var y = columnSize + (yScale * count * stepSize); 
 
\t \t context.fillText(scale, margin,y + margin); 
 
\t \t context.moveTo(rowSize,y) 
 
\t \t context.lineTo(canvas.width,y) 
 
\t \t count++; 
 
\t } 
 
\t context.stroke(); 
 
\t context.translate(rowSize,canvas.height + Val_min * yScale); 
 
\t context.scale(1,-1 * yScale); 
 
\t \t // Color of each dataplot items 
 
\t context.strokeStyle="#FF0066"; 
 
\t plotData(Apple); 
 
\t context.strokeStyle="#9933FF"; 
 
\t plotData(Samsung); 
 
    context.strokeStyle="#000"; 
 
\t plotData(Nokia); 
 
function plotData(dataSet) { 
 
\t context.beginPath(); 
 
\t context.moveTo(0, dataSet[0]); 
 
\t for (i=1;i<sections;i++) { 
 
\t \t context.lineTo(i * xScale, dataSet[i]); 
 
\t } 
 
\t context.stroke(); 
 
} 
 
    }); 
 
</script> 
 
<body> 
 
<div align="center"> 
 
<h2>Monthly Profits of Companies(in million $)</h2> 
 
<canvas id="canvas" height="400" width="650"> 
 
</canvas> 
 
<br> 
 
\t <!--Legends for Dataplot --> 
 
<span style="color:#FF0066"> Apple </span> 
 
<span style="color:#9933FF"> Samsung</span> 
 
<span style="color:#000"> Nokia </span> 
 
</div> 
 
</body> 
 
</html>

嗨。我需要繪製一個線性圖。這裏我拿了樣品。現在我需要從json文件中獲取x和y的數據。我如何做到這一點。我不允許使用任何庫或API,只允許使用HTML,CSS和Javascript。任何人都可以告訴我。我的JSON數據看起來像

[{"aa":{ 
    "total_visits":"925", 
    "2017-07-29":{ 
     "visits":38, 
     "leads":0 
    }, 

    "total_leads":13 
}, 
"bb":{ 
    "total_visits":"144", 
    "2017-07-29":{ 
     "visits":1, 
     "leads":0 
    }, 
      "total_leads":1 
}, 
"cc":{ 
    "last_recorded":"2017-07-29", 
    "total_visits":"1386", 
    "2017-07-29":{ 
     "visits":41, 
     "leads":0 
    }, 

    "total_leads":12 
}, 
"dd":{ 
    "total_visits":"2364", 
    "2017-07-29":{ 
     "visits":55, 
     "leads":2.1 
    }, 

    "total_leads":59 
}, 
"ee":{ 

    "2017-07-29":{ 
     "visits":44, 
     "leads":0 
    }, 

    "total_leads":37 
}, 
"ff":{ 

    "total_leads":2 
}, 
"gg":{ 

    "total_leads":1 
}, 
"hh":{ 

    "total_visits":"115", 

    "2017-07-29":{ 

     "visits":2, 
     "leads":0 
    }, 
    "package_id":"2", 
    "total_leads":3 
}, 
"ii":{ 

    "total_visits":"2213", 

    "2017-07-29":{ 

     "visits":94, 
     "leads":0 
    }, 

    "total_leads":87 
} 
}] 

我需要採取total_visits(或)瀏覽次數(X軸)和total_leads(或)導線(Y軸)和繪製圖形。提前致謝。

+0

我認爲你可以使用http://underscorejs.org/庫來實現這一目標。 –

回答

1

試試這個代碼控制檯

編輯plotData功能繪製X,Y軸值

function plotData(xVisits,yLeads) { 
    context.beginPath(); 
    context.moveTo(xVisits, yLeads); 
    for (i=1;i<xVisits.length;i++) { 
     context.lineTo(xVisits[i], yLeads[i]); 
    } 
    context.stroke(); 
} 

<script type="text/javascript"> 
 
    var visits = []; 
 
    var leads = []; 
 
    function loadJSON(callback) { 
 
    var xobj = new XMLHttpRequest(); 
 
    xobj.overrideMimeType("application/json"); 
 
    xobj.open('GET', 'https://api.myjson.com/bins/17x8l1', true); 
 
    xobj.onreadystatechange = function() { 
 
     if (xobj.readyState == 4 && xobj.status == "200") { 
 
      callback(xobj.responseText); 
 
     } 
 
    } 
 
    xobj.send(null); 
 
} 
 
loadJSON(function(response) { 
 
    var response; 
 
    var field=JSON.parse(response); 
 
    for (var i = 0; i < field.length; i++) { 
 
    var $this=field[i]; 
 
     for (var key in $this) { 
 
      if ($this.hasOwnProperty(key)) { 
 
      var val = $this[key]; 
 
      visits.push(val.total_visits); 
 
      leads.push(val.total_leads); 
 
      } 
 
     } 
 
     } 
 
    sections = 12; 
 
\t Val_max = 130; 
 
\t Val_min = -40; 
 
\t var stepSize = 10; 
 
\t var columnSize = 50; 
 
\t var rowSize = 50; 
 
\t var margin = 10; 
 
\t var xAxis = [" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
 
\t canvas = document.getElementById("canvas"); 
 
\t context = canvas.getContext("2d"); 
 
\t context.fillStyle = "#0099ff" 
 
\t context.font = "20 pt Verdana" 
 
\t yScale = (canvas.height - columnSize - margin)/(Val_max - Val_min); 
 
\t xScale = (canvas.width - rowSize)/sections; 
 
\t context.strokeStyle="#009933"; // color of grid lines 
 
\t context.beginPath(); 
 
    // print Parameters on X axis, and grid lines on the graph 
 
\t for (i=1;i<=sections;i++) { 
 
\t \t var x = i * xScale; 
 
\t \t context.fillText(xAxis[i], x,columnSize - margin); 
 
\t \t context.moveTo(x, columnSize); 
 
\t \t context.lineTo(x, canvas.height - margin); 
 
\t } 
 
    // print row header and draw horizontal grid lines 
 
\t var count = 0; 
 
\t for (scale=Val_max;scale>=Val_min;scale = scale - stepSize) { 
 
\t \t var y = columnSize + (yScale * count * stepSize); 
 
\t \t context.fillText(scale, margin,y + margin); 
 
\t \t context.moveTo(rowSize,y) 
 
\t \t context.lineTo(canvas.width,y) 
 
\t \t count++; 
 
\t } 
 
\t context.stroke(); 
 
\t context.translate(rowSize,canvas.height + Val_min * yScale); 
 
\t context.scale(1,-1 * yScale); 
 
\t \t // Color of each dataplot items 
 
\t context.strokeStyle="#FF0066"; 
 
\t plotData(visits,leads); 
 
function plotData(xVisits,yLeads) { 
 
\t context.beginPath(); 
 
\t context.moveTo(xVisits, yLeads); 
 
\t for (i=1;i<xVisits.length;i++) { 
 
\t \t context.lineTo(xVisits[i], yLeads[i]); 
 
\t } 
 
\t context.stroke(); 
 
} 
 
    }); 
 
</script> 
 
<body> 
 
<div align="center"> 
 
<h2>Monthly Profits of Companies(in million $)</h2> 
 
<canvas id="canvas" height="400" width="650"> 
 
</canvas> 
 
<br> 
 
\t <!--Legends for Dataplot --> 
 
<span style="color:#FF0066"> Visits Vs Leads</span> 
 
</div> 
 
</body>

更新


,你應該修改json數據顯示線性圖表。

var X = []; 
 
var Y = []; 
 
var data = []; 
 
function loadJSON(callback) { 
 
    var xobj = new XMLHttpRequest(); 
 
    xobj.overrideMimeType("application/json"); 
 
    xobj.open('GET', 'https://api.myjson.com/bins/gzdjd', true); 
 
    xobj.onreadystatechange = function() { 
 
     if (xobj.readyState == 4 && xobj.status == "200") { 
 
      callback(xobj.responseText); 
 
     } 
 
    } 
 
    xobj.send(null); 
 
} 
 
loadJSON(function(response) { 
 
\t var response; 
 
\t var field=JSON.parse(response); 
 
\t var values=[]; 
 
\t for (var i = 0; i < field.length; i++) { 
 
\t \t var $this=field[i]; 
 
\t \t for (var key in $this) { 
 
\t \t if ($this.hasOwnProperty(key)) { 
 
\t \t \t var val = $this[key]; 
 
\t \t \t values.push({"X":val.total_visits,"Y":val.total_leads}); 
 
\t \t } 
 
\t \t } 
 
\t } 
 
    data=({"values":values}); 
 
    var graph; 
 
\t var xPadding = 30; 
 
\t var yPadding = 30; 
 
\t var sections = 12; 
 
\t var Val_max = 130; 
 
\t var Val_min = -40; 
 
\t var stepSize = 10; 
 
\t var columnSize = 50; 
 
\t var rowSize = 50; 
 
\t var margin = 10; 
 

 
\t function getMaxY() { 
 
\t \t var max = 0; 
 
\t \t for(var i = 0; i < data.values.length; i ++) { 
 
\t \t \t if(data.values[i].Y > max) { 
 
\t \t \t \t max = data.values[i].Y; 
 
\t \t \t } 
 
\t \t } 
 
\t \t max += 10 - max % 10; 
 
\t \t return max; 
 
\t } 
 

 
\t function getMaxX() { 
 
\t \t var max = 0; 
 
\t \t for(var i = 0; i < data.values.length; i ++) { 
 
\t \t \t if(data.values[i].X > max) { 
 
\t \t \t \t max = data.values[i].X; 
 
\t \t \t } 
 
\t \t } 
 
\t \t max += 10 - max % 10; 
 
\t \t return max; 
 
\t } 
 

 
\t function getXPixel(val) { 
 
\t \t return ((graph.width - xPadding)/getMaxX()) * val + (xPadding * 1.5); 
 
\t } 
 

 
\t function getYPixel(val) { 
 
\t \t return graph.height - (((graph.height - yPadding)/getMaxY()) * val) - yPadding; 
 
\t } 
 

 
\t graph = document.getElementById("canvas"); 
 
\t var c = graph.getContext('2d');    
 

 
\t c.lineWidth = 1; 
 
\t c.strokeStyle = '#333'; 
 
\t c.font = 'italic 8pt sans-serif'; 
 
\t c.textAlign = "center"; 
 

 
\t c.beginPath(); 
 
\t c.moveTo(xPadding, 0); 
 
\t c.lineTo(xPadding, graph.height - yPadding + 20); 
 
\t c.lineTo(graph.width, graph.height - yPadding + 20); 
 
\t c.stroke(); 
 

 
\t for(var i = 0; i < data.values.length; i ++) { 
 
\t \t c.fillText(data.values[i].X, getXPixel(data.values[i].X), graph.height - yPadding + 30); 
 
\t } 
 

 
\t c.textAlign = "right" 
 
\t c.textBaseline = "middle"; 
 

 
\t for(var i = 0; i < getMaxY(); i += 10) { 
 
\t \t c.fillText(i, xPadding - 10, getYPixel(i)); 
 
\t } 
 

 
\t c.strokeStyle = '#f00'; 
 

 
\t c.beginPath(); 
 
\t c.moveTo(getXPixel(data.values[0].X), getYPixel(data.values[0].Y)); 
 
\t for(var i = 1; i < data.values.length; i ++) { 
 
\t \t c.lineTo(getXPixel(data.values[i].X), getYPixel(data.values[i].Y)); 
 
\t } 
 
\t c.stroke(); 
 

 
\t c.fillStyle = '#333'; 
 

 
\t for(var i = 0; i < data.values.length; i ++) { 
 
\t \t c.beginPath(); 
 
\t \t c.arc(getXPixel(data.values[i].X), getYPixel(data.values[i].Y), 4, 0, Math.PI * 2, true); 
 
\t \t c.fill(); 
 
\t } 
 
\t var yScale = (graph.height - columnSize - margin)/(Val_max - Val_min); 
 
\t var xScale = (graph.width - rowSize)/sections; 
 
\t c.strokeStyle="#009933"; // color of grid lines 
 
\t c.beginPath(); 
 

 
\t for (i=1;i<=sections;i++) { 
 
\t \t var x = i * xScale; 
 
\t \t //c.fillText(xAxis[i], x,columnSize - margin); 
 
\t \t c.moveTo(x - 18, columnSize); 
 
\t \t c.lineTo(x - 18, graph.height - margin); 
 
\t } 
 

 
\t var count = 0; 
 
\t for (scale=Val_max;scale>=Val_min;scale = scale - stepSize) { 
 
\t \t var y = columnSize + (yScale * count * stepSize); 
 
\t \t //c.fillText(scale, margin,y + margin); 
 
\t \t c.moveTo(rowSize - 18,y) 
 
\t \t c.lineTo(graph.width,y) 
 
\t \t count++; 
 
\t } 
 
\t c.stroke(); 
 
});
<div align="center"> 
 
<h2>Monthly Profits of Companies(in million $)</h2> 
 
<canvas id="canvas" height="400" width="650"> 
 
</canvas> 
 
<br> 
 
\t <!--Legends for Dataplot --> 
 
<span style="color:#FF0066"> Visits Vs Leads</span> 
 
</div>

+0

爲什麼我們得到3行罪圖?是什麼原因? – Raghavi

+0

它從你生成'json' data.as你問我已經設置x和y軸的值graph.Did你需要兩個訪問和潛在客戶分開? – Amal

+0

在x軸上訪問,並在y軸上作爲單行導向。像線性圖。那個Jan..Feb..mar..are不需要。那可能嗎? – Raghavi

0
var data = {"arrkay":{"last_recorded":"2017-07-29","total_visits":"925","signup_date":"2017-01-23","signup_time":"03-23-50","2017-07-29":{"start_date":"2017-07-23","end_date":"2017-07-29","visits":38,"leads":0},"package_id":"2","total_leads":13}, "ascentevents":{"last_recorded":"2017-07-27","total_visits":"144","signup_date":"2016-11-14","signup_time":"03-18-47","2017-07-29":{"start_date":"2017-07-23","end_date":"2017-07-29","visits":1,"leads":0},"package_id":"2","total_leads":1}}; 

data = [data]; 

x = "arrkay"; 

alert("total_visits is: "+data[0][x].total_visits); 
alert("total_leads is: "+data[0][x].total_leads); 

看看這個樣本in fiddle

0

檢查,一旦你運行該code

 x = [{"arrkay": { 
     "last_recorded": "2017-07-29", 
     "total_visits": "925", 
     "signup_date": "2017-01-23", 
     "signup_time": "03-23-50", 
     "2017-07-29": { 
      "start_date": "2017-07-23", 
      "end_date": "2017-07-29", 
      "visits": 38, 
      "leads": 0 
     }, 
     "package_id": "2", 
     "total_leads": 13 
    }, 
     "ascentevents": { 
      "last_recorded": "2017-07-27", 
      "total_visits": "144", 
      "signup_date": "2016-11-14", 
      "signup_time": "03-18-47", 
      "2017-07-29": { 
       "start_date": "2017-07-23", 
       "end_date": "2017-07-29", 
       "visits": 1, 
       "leads": 0 
      }, 
      "package_id": "2", 
      "total_leads": 1 
     }, 
     "asterisk": { 
      "last_recorded": "2017-07-29", 
      "total_visits": "1386", 
      "signup_date": "2016-08-25", 
      "signup_time": "01-53-52", 
      "2017-07-29": { 
       "start_date": "2017-07-23", 
       "end_date": "2017-07-29", 
       "visits": 41, 
       "leads": 0 
      }, 
      "package_id": "2", 
      "total_leads": 12 
     }, 
     "atmabhan": { 
      "last_recorded": "2017-07-29", 
      "total_visits": "2364", 
      "signup_date": "2016-04-06", 
      "signup_time": "17-48-53", 
      "2017-07-29": { 
       "start_date": "2017-07-23", 
       "end_date": "2017-07-29", 
       "visits": 55, 
       "leads": 2.1 
      }, 
      "package_id": "2", 
      "total_leads": 59 
     }, 
     "atulsia": { 
      "last_recorded": "2017-07-29", 
      "total_visits": "2425", 
      "signup_date": "2015-12-01", 
      "signup_time": "11-30-01", 
      "2017-07-29": { 
       "start_date": "2017-07-23", 
       "end_date": "2017-07-29", 
       "visits": 44, 
       "leads": 0 
      }, 
      "package_id": "2", 
      "total_leads": 37 
     }, 
     "axxistech": { 
      "last_recorded": "2016-12-09", 
      "total_visits": "16", 
      "signup_date": "2016-10-27", 
      "signup_time": "16-56-00", 
      "package_id": "2", 
      "total_leads": 2 
     }, 
     "babasohna": { 
      "last_recorded": "2016-12-15", 
      "total_visits": "1", 
      "signup_date": "2016-12-15", 
      "signup_time": "12-39-30", 
      "package_id": "2", 
      "total_leads": 1 
     }, 
     "beastemergency": { 
      "last_recorded": "2017-07-28", 
      "total_visits": "115", 
      "signup_date": "2017-03-06", 
      "signup_time": "04-34-52", 
      "2017-07-29": { 
       "start_date": "2017-07-23", 
       "end_date": "2017-07-29", 
       "visits": 2, 
       "leads": 0 
      }, 
      "package_id": "2", 
      "total_leads": 3 
     }, 
     "bhardwaj": { 
      "last_recorded": "2017-07-29", 
      "total_visits": "2213", 
      "signup_date": "2016-09-28", 
      "signup_time": "09-55-24", 
      "2017-07-29": { 
       "start_date": "2017-07-23", 
       "end_date": "2017-07-29", 
       "visits": 94, 
       "leads": 0 
      }, 
      "package_id": "2", 
      "total_leads": 87 
     } 
    }] 

    for (i=0;i < x.length;i++){ 
    for (var _key in x[i]){ 
    console.log(x[i][_key]['total_visits'], x[i][_key]['total_leads']); 

    for (var p in x[i][_key]){ 
    if (Object.prototype.toString.call(x[i][_key][p]) == = '[object Object]') { 
    console.log(x[i][_key][p]['leads'], x[i][_key][p]['visits']); 
    } 

    }}} 
相關問題