Home  >  Article  >  Web Front-end  >  Use EVAL to process jqchart jquery line chart return data invalid solution_jquery

Use EVAL to process jqchart jquery line chart return data invalid solution_jquery

WBOY
WBOYOriginal
2016-05-16 15:29:401144browse

The eval function can parse some processed program code to reach a state that can be executed. I checked many posts. When the jqchart plug-in makes a line chart, all the returned data are eval when processing it, but I can't figure it out. Later I found out:

1. There is no need for eval processing at all, just intercept the string directly (the return value must be spliced);

2. When the processed string is put into the data of the series, [];

Here is the code:

Only one 5d951b1447c61ed1e8f4d70bad2848ba16b28748ea4df4d9c2150843fecfba68;

<script>里面
<script type="text/javascript" src="jquery.1.8.2.js"></script>
<script type="text/javascript" src="jquery-jqChart-min.js"></script>
<script type="text/javascript">
$(function () {
 $.get("tgajax.php",function(data){
  var dom = data.substring(0,data.length-1);//主要是对返回数据的处理,后面很明显多了一个","
  //var dom = eval('(' + dom + ')'); 
  //alert( dom);
 $('#jqChart').jqChart({
  title: { text: '线形图示例' },
  axes: [
   {
    location: 'left',
    minimum: 1,
    maximum: 10,
    interval: 1,
   }
  ],
   series: [
   {
    type: 'line',
    title:'上海',
    markers: null,//拐点不用圆点标示 
    strokeStyle: 'blue' , 
    data: [['json', 1], ['per', 9], ['perter', 3]]
   },
   {
    type: 'line',//line,Column
    title:'北京',
    strokeStyle: 'red' , 
    data:[dom]
   },
   ]
  }); 
});
});
</script>

I used simple php to create the background processing page, and I don’t know how to do anything else

<&#63;php
include ("configaz.php"); //数据库连接在另一个文件内,这里就不弄了
 $sql="select sid,sname,sprice,count(sprice) as pricenum from shangpin group by sname";
 $query=mysql_query($sql);
 $row=mysql_fetch_array($query);
 while($row=mysql_fetch_array($query)){
 $pricenum=$row['pricenum'];
 $sname=$row['sname'];
 } 
 echo $str .= "['".$sname."',".$pricenum."],";//拼接字符串,按照jqChart要求的字符串格式,当然用数组更好,可惜不怎么会

There must be a better way, but I just started learning and slowly exploring it

I believe there are more solutions than the above. There must be better solutions. We welcome everyone to learn and make progress together.

ps: ajax reads data and uses jqchart to display charts

In recent projects, chart effects need to be displayed, and the chart plug-ins collected have finally come into use.

But compared with jqchart, there are still many differences.

Realization effect:

I rewrote jqchart.

The first thing to solve is not displaying the x-axis and y-axis:

//各DIV作成 
     // 取消标题显示 
     /* 
     this.titleBox//Title 
      =this.mkBoxElement('T', 
       this.op.titleLeft,this.op.titleTop 
      ).appendTo(this.jQcanvasBox) 
      .css('width',this.op.width-this.op.titleLeft)//fix for safari3 2007.12.4 
      .get(0); 
     */ 
     // 取消y轴数字显示 
     /* 
     this.scaleYBox//Y軸スケール 
      =this.mkBoxElement('Y', 
       this.op.scaleYLeft,this.op.scaleYTop 
      ).appendTo(this.jQcanvasBox).get(0); 
     */ 
     // 取消x轴分类显示 
     /* 
     this.scaleXBox//X軸スケール 
      =this.mkBoxElement('X', 
       this.op.scaleXLeft,this.op.scaleXTop 
     ).appendTo(this.jQcanvasBox).get(0); 
     */

Secondly, for the text at the inflection point, the original display was the corresponding data value, but now the corresponding x-axis name needs to be displayed:

if( x <= op.width){ 
       var dx=x-op.paddingL,dy=y-op.paddingT; 
       var dxx = i<=0 &#63; (dx+op.labelDataOffsetX - 5 + 'px'):( dx+op.labelDataOffsetX - 20 + 'px'); //坐标点x轴偏移 
       var dyy = i%2 &#63; (dy+op.labelDataOffsetY - 25 + 'px'):(dy+op.labelDataOffsetY - 5 + 'px'); //坐标点y轴偏移 
       it.wrtText( 
        //dx+op.labelDataOffsetX - 20 + 'px', 
        dxx, 
        //dy+op.labelDataOffsetY - 10 + 'px', 
        dyy, 
        //op.rows[i],  // pre: 坐标点data值 
        op.txtpointers[i], // cychai:坐标点文字 
        op, 
        "#jQchart-data-D-"+op.id 
       ).css('color',(op.data.length==1)&#63;'#333':strokeStyle) 
       .css({"width":"100px","font-size":"12px"});  // cychai:样式控制 

The default data can be displayed. The next step is to collaborate with development.

I hope to use ajax to asynchronously obtain data and then display it in the foreground.

Here, I used a sample page chartdata.html, which is the required data page

[{labelX : ["Design","Portability","Easy to use","Battery standby","Camera function","Zoom"],data:[[5,7,2,3 ,9,4]]}]

In the foreground, I request the page through ajax, process the returned json data, and pass it to chartSetting:

$(function(){ 
 $.ajax({ 
  url: "chartdata.html", 
  type: "GET", 
  success: function(cdata){ 
   showDDChart(cdata); 
  } 
 }); 
 function showDDChart(cdata){ 
  var dd_chart = eval(cdata)[0]; 
  var chartSetting={ 
   config : {  
    title : "",  
    titleLeft: 70,  
    labelX :dd_chart.labelX,  
    //labelX :["外观设计","便携性","易用性","电池待机","摄像功能","变焦"], 
    scaleY : {min: 0,max:10,gap:2}, 
    width: 300+25,  
    height: 125+50,  
    paddingL : 10,  
    paddingT : 10  
   },  
   //data: [[5,3,1,8,4,9]] 
   data :dd_chart.data 
  };  
  $('#canvasMyID').jQchart(chartSetting); 
 } 
}); 

Full html page:

 
 
 
 
 
 
 
 
 

OK, you’re done! I will put the complete example in my resources.

I haven’t used jquery ajax for a long time, and I’m a bit unfamiliar with spelling out json data. I still like to develop this kind of logical work....

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn