fusioncharts 用法实例

支持xml格式和json格式的数据。

用法很简单。

1.需要引入FusionCharts.js.

2.html中定义个id="chart"的div

<div id="chart"></div>

3.js代码调用即可

(一)xml格式。

var dataXml = "<graph caption='每月销售额柱形图' xAxisName='月份' yAxisName='Units' showNames='1' decimalPrecision='0' formatNumberScale='0'><set name='一月' value='462' color='AFD8F8' /><set name='二月' value='857' color='F6BD0F' /><set name='三月' value='671' color='8BBA00' /><set name='四月' value='494' color='FF8E46' /><set name='五月' value='761' color='008E8E' /><set name='六月' value='960' color='D64646' /><set name='七月' value='629' color='8E468E' /><set name='八月' value='622' color='588526' /><set name='九月' value='376' color='B3AA00' /><set name='十月' value='494' color='008ED6' /><set name='十一月' value='761' color='9D080D' /><set name='十二月' value='960' color='A186BE' /></graph>";

var myChart = new FusionCharts("${base}/thirdparty/FusionCharts/ChartsV3/Charts/Pie3D.swf", "myChartId_00", "500", "400");//参数分别为【需要使用的swf样式】,【chart的id(同一个页面有多个chart避免重复)】,【width】,【height】
//myChart.setJSONData(${jsondata!});
myChart.setDataURL(dataXml);
myChart.render("chart");

(二)json

  1.  //使用json数据   
  2.         var  MSLine = new FusionCharts("flex/fusioncharts/MSLine.swf", "MSLineID", "460", "350", "0", "1");   MSLine.setJSONData({      
  3.              "chart":{      
  4.                     "caption":"Business Results 2005 v 2006",     
  5.                     "xaxisname":"Month",     
  6.                     "yaxisname":"Revenue",    
  7.                     "showvalues":"0",      
  8.                     "numberprefix":"$"  },   
  9.              "categories":[{       
  10.                  "category":[  
  11.                                  {          "label":"Jan"        },      
  12.                                  {          "label":"Feb"        },      
  13.                                  {          "label":"Mar"        },        
  14.                                  {          "label":"Apr"        },      
  15.                                  {          "label":"May"        },      
  16.                                  {          "label":"Jun"        },      
  17.                                  {          "label":"Jul"        },      
  18.                                  {          "label":"Aug"        },      
  19.                                  {          "label":"Sep"        },      
  20.                                  {          "label":"Oct"        },      
  21.                                  {          "label":"Nov"        },       
  22.                                  {          "label":"Dec"        }]      
  23.                }  
  24.                ],   
  25.              "dataset":[        
  26.                           {     
  27.                               "seriesname":"2006",      
  28.                               "data":[  
  29.                                      {          "value":"27400"        },       
  30.                                      {          "value":"29800"        },   
  31.                                      {          "value":"25800"        },          
  32.                                      {          "value":"26800"        },          
  33.                                      {          "value":"29600"        },          
  34.                                      {          "value":"32600"        },          
  35.                                      {          "value":"31800"        },          
  36.                                      {          "value":"36700"        },          
  37.                                      {          "value":"29700"        },          
  38.                                      {          "value":"31900"        },          
  39.                                      {          "value":"34800"        },          
  40.                                      {          "value":"24800"        }      ]    },    
  41.                            {   "seriesname":"2007",      
  42.                                "data":[  
  43.                                      {          "value":"27900"        },       
  44.                                      {          "value":"29800"        },   
  45.                                      {          "value":"15800"        },          
  46.                                      {          "value":"24800"        },          
  47.                                      {          "value":"19600"        },          
  48.                                      {          "value":"32600"        },          
  49.                                      {          "value":"35800"        },          
  50.                                      {          "value":"31700"        },          
  51.                                      {          "value":"39700"        },          
  52.                                      {          "value":"51900"        },          
  53.                                      {          "value":"14800"        },          
  54.                                      {          "value":"20800"        }      ]    },    
  55.                            {   "seriesname":"2005",       
  56.                                "data":[{        "value":"10000"        },         
  57.                                      {          "value":"11500"        },          
  58.                                      {          "value":"12500"        },          
  59.                                      {          "value":"15000"        },          
  60.                                      {          "value":"11000"        },          
  61.                                      {          "value":"9800"         },          
  62.                                      {          "value":"11800"        },          
  63.                                      {          "value":"19700"        },          
  64.                                      {          "value":"21700"        },          
  65.                                      {          "value":"21900"        },          
  66.                                      {          "value":"22900"        },          
  67.                                      {          "value":"20800"        }      ]    }  ],   
  68.                     "trendlines":{      
  69.                        "line":[{        "startvalue":"26000",         
  70.                                         "color":"91C728",         
  71.                                         "displayvalue":"Target",         
  72.                                         "showontop":"1"        
  73.                        }]  
  74.                      },    
  75.                     "styles":[{        
  76.                        "definition":[{            
  77.                                "style":[{          "name":"CanvasAnim",  
  78.                                                    "type":"animation",                
  79.                                                    "param":"_xScale",                
  80.                                                    "start":"0",                
  81.                                                    "duration":"1"              
  82.                                         }]  
  83.                        }],        
  84.                     "application":[{              
  85.                                "apply":[{         "toobject":"Canvas",                
  86.                                                   "styles":"CanvasAnim"              
  87.                                          }]          
  88.                        }]  
  89.                      }]  
  90.             });  

//如果java代码生成,可以理解为整个是map,chart是纯map格式的,data是map中包含了list<Map>。

Map<String,Object> chart = new HashMap<String, Object>();//图表属性
chart.put("caption", "图表标题");//图表标题
chart.put("bgcolor", "F2F8EF");//bgcolor
chart.put("showborder", "0");//border
chart.put("exportenabled", "1");//export
chart.put("exportshowmenuitem", "1");//export
chart.put("logoalpha", "30");//alpha
chart.put("useroundedges", "1");
chart.put("yaxisname", "Sales Figure");
chart.put("logoposition", "CC");

List<Map<String,Object>> datalist = new ArrayList<Map<String ,Object>>();//组织数据List<Map>

for(Map<String,Object> m :queryListC){
Map<String,Object> map = new HashMap<String, Object>();
map.put("label", "xxx");
map.put("value", value);
datalist.add(map);
}

Map<String ,Object> jsondataMap = new HashMap<String, Object>();//最终json仍然是map,需要把前面的数据放入

jsondataMap.put("data", datalist);
jsondataMap.put("chart", chart);

最终将数据返回js

model.put("jsondata", net.sf.json.JSONObject.fromObject(jsondataMap).toString().replace("\"", "'"));

 

以上是fusionCharts的基本用法。fusionCharts功能很强大,能实现精美的图标功能,可以上官网查看demo。

posted @ 2015-06-29 12:43  江南的夏天  阅读(630)  评论(0编辑  收藏  举报