动态改变折线图中的标签

发布于 2024-11-04 06:07:47 字数 2094 浏览 0 评论 0原文

我从响应对象中的网络服务器获取以下 JSON 对象:

(JSON Array :[{"JUL":"5"},{"AUG":"7"},{"SEP":"9"},{"OCT":"11"},{"NOV":"13"},    {"DEC":"15"},{"JAN":"17"},{"FEB":"19"},{"MAR":"21"},{"APR":"23"},{"MAY":"25"},{"JUN":"27"}]) 

我正在数组中获取 JSON 对象的键

var al_key = [];

//For loop for x axis dispaly.
alert("al_key.length --------->"+al_key.length);

var jsonObj = []; //declare array

for (var i = 0; i < al_key.length; i++) {
    jsonObj.push({value: i, text: al_key[i].value});
}

// Add axes
chart.addAxis("x", {min:1, max: 12 ,labels: jsonObj , fixLower: "major", microTicks: true, majorTickStep: 1});  

chart.addAxis("y", { min: 0, max: 50, vertical: true, fixLower: "major", fixUpper: "major", includeZero: true });

但是,它没有按照我期望的方式工作。关于我哪里出错了有什么建议吗?或者在 x 轴上动态显示月份的其他方法?预先感谢您的任何帮助。


将响应粘贴为问题编辑:

实际上,我得到了 JSONArray 对象,其中包含单个 JSONObject,其中包含

//服务器端编码之类的值。

JSONObject object=new JSONObject(); 
object.put("JAN":"17"); 
object.put("FEB":"19"); 
object.put("MAR":"21"); 
object.put("APR":"23"); 
object.put("MAY":"24"); 
object.put("JUN":"27");

JSONArray arrayObj=new JSONArray(); 
arrayObj.add(object);

On System.out.println(arrayObj); // our json appears like {"JAN":"17"},{"FEB":"19"},{"MAR":"21"},{"APR":"23"}, //{"MAY":"24"},{"JUN":"27"}];

在 jsp 上:

//Call to the database to fetch the desired value 
dojo.xhrGet( { url : "/POC/Action.do", 
               handleAs : "json", 
               sync: true, 
               load : function(response, ioArgs) { 
  alert("retrived response ------"+response); 
  for(var i in response) 
    for(var x in response[i]) 
      output.push(response[i][x]); 
  alert("Get value to draw line chart---------------------->"+output); 
},
error: function(response, ioArgs){ 
  dojo.byId("grid").innerHTML = "An error occurred, with response: " + response; 
  return response; 
}, 
handleAs: "json" });

响应对象包含返回值(JSONArray 对象);

现在这个键需要显示在图表的x轴上。我是否需要再次获取密钥并准备以您上面所说的形式形成的 json 对象..

I am getting the following JSON object from a webserver in response object:

(JSON Array :[{"JUL":"5"},{"AUG":"7"},{"SEP":"9"},{"OCT":"11"},{"NOV":"13"},    {"DEC":"15"},{"JAN":"17"},{"FEB":"19"},{"MAR":"21"},{"APR":"23"},{"MAY":"25"},{"JUN":"27"}]) 

I am taking keys of the JSON object in an array

var al_key = [];

//For loop for x axis dispaly.
alert("al_key.length --------->"+al_key.length);

var jsonObj = []; //declare array

for (var i = 0; i < al_key.length; i++) {
    jsonObj.push({value: i, text: al_key[i].value});
}

// Add axes
chart.addAxis("x", {min:1, max: 12 ,labels: jsonObj , fixLower: "major", microTicks: true, majorTickStep: 1});  

chart.addAxis("y", { min: 0, max: 50, vertical: true, fixLower: "major", fixUpper: "major", includeZero: true });

However, it's not working the way I expect it to. Any suggestions as to where I am getting this wrong? Or alternative ways to display months on the x-axis dynamically? Thanks in advance for any help.


Pasted response as a question edit:

Actually i am getting JSONArray Object which contain single JSONObject which contains values like

//Server side coding..

JSONObject object=new JSONObject(); 
object.put("JAN":"17"); 
object.put("FEB":"19"); 
object.put("MAR":"21"); 
object.put("APR":"23"); 
object.put("MAY":"24"); 
object.put("JUN":"27");

JSONArray arrayObj=new JSONArray(); 
arrayObj.add(object);

On System.out.println(arrayObj); // our json appears like {"JAN":"17"},{"FEB":"19"},{"MAR":"21"},{"APR":"23"}, //{"MAY":"24"},{"JUN":"27"}];

On jsp:

//Call to the database to fetch the desired value 
dojo.xhrGet( { url : "/POC/Action.do", 
               handleAs : "json", 
               sync: true, 
               load : function(response, ioArgs) { 
  alert("retrived response ------"+response); 
  for(var i in response) 
    for(var x in response[i]) 
      output.push(response[i][x]); 
  alert("Get value to draw line chart---------------------->"+output); 
},
error: function(response, ioArgs){ 
  dojo.byId("grid").innerHTML = "An error occurred, with response: " + response; 
  return response; 
}, 
handleAs: "json" });

the response object contain the return value (JSONArray object);

now this key is need to be displayed in the x axis of the chart. Do i need to again get the key and prepare the json object which is formed in the form said above by you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2024-11-11 06:07:47

回复您更新的信息:

更改发送 JSON 的方式(根据可用的有限信息,我假设是使用 org.jon 包的 Java servlet)

JSONArray array = new JSONArray();
arrayObj.put (new JSONObject("{'month':'JAN', 'value':'17'}");
// and repeat for the rest of the data

然后您可以在您的 dojo 图表中使用它,如下所示 (jsonObj3)。


原创评论。

查看此处的示例,特别是以下部分:

labels: [{value: 0, text: "zero"},
         {value: 2, text: "two"},
         {value: 4, text: "four"} 

和添加两个系列

addSeries("Series A", [1, 2, 3, 4, 5], {stroke: {color: "red"}, fill: "lightpink"}).
addSeries("Series B", [5, 4, 3, 2, 1], {stroke: {color: "blue"}, fill: "lightblue"}).

对于该系列,JSON 可能是

var months = ["JUL","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR","APR","MAY","JUN"];
var jsonObj2 = [];

for (var i = 0; i < months.length ; i++) {
    jsonObj2.push({value: i, text: months[i]});
}

如果您想要一个 JSON 对象,请尝试按如下方式排列它:

var data = [{month: "JUL", value:"5"},
            {month: "AUG", value:"7"},
            {month: "SEP", value:"9"},
            {month: "OCT", value:"11"},
            {month: "NOV", value:"13"},    
            {month: "DEC", value:"15"},
            {month: "JAN", value:"17"},
            {month: "FEB", value:"19"},
            {month: "MAR", value:"21"},
            {month: "APR", value:"23"},
            {month: "MAY", value:"25"},
            {month: "JUN", value:"27"}];

var jsonObj3 = [];

for (var i = 0; i < data.length ; i++) {
    jsonObj3.push({value: i, text: data[i].month});
}

In reply to your updated information:

Change the way you send the JSON (based on the limited info available,I am assuming is a Java servlet using the org.jon package)

JSONArray array = new JSONArray();
arrayObj.put (new JSONObject("{'month':'JAN', 'value':'17'}");
// and repeat for the rest of the data

You can use it then in your dojo chart as I wrote below (jsonObj3).


Original comment.

Check at the examples here, specially the part:

labels: [{value: 0, text: "zero"},
         {value: 2, text: "two"},
         {value: 4, text: "four"} 

and add the two series

addSeries("Series A", [1, 2, 3, 4, 5], {stroke: {color: "red"}, fill: "lightpink"}).
addSeries("Series B", [5, 4, 3, 2, 1], {stroke: {color: "blue"}, fill: "lightblue"}).

For the series, the JSON might be

var months = ["JUL","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR","APR","MAY","JUN"];
var jsonObj2 = [];

for (var i = 0; i < months.length ; i++) {
    jsonObj2.push({value: i, text: months[i]});
}

If you want one single JSON object, try arranging it like this:

var data = [{month: "JUL", value:"5"},
            {month: "AUG", value:"7"},
            {month: "SEP", value:"9"},
            {month: "OCT", value:"11"},
            {month: "NOV", value:"13"},    
            {month: "DEC", value:"15"},
            {month: "JAN", value:"17"},
            {month: "FEB", value:"19"},
            {month: "MAR", value:"21"},
            {month: "APR", value:"23"},
            {month: "MAY", value:"25"},
            {month: "JUN", value:"27"}];

var jsonObj3 = [];

for (var i = 0; i < data.length ; i++) {
    jsonObj3.push({value: i, text: data[i].month});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文