要在flash动画中加载两个或多个 FusionCharts 图表文件,只需为每个图表类创建一个实例,提供相应的XML数据然后将它呈现出来就可以了,非常简单。
在本例中,我们在FlashExamples文件夹下创建MultipleCharts.fla,然后再在这个新创建的文件夹下创建一层 "Graphic Elements",添加两个圆角矩形的背景框,用来加载两个图表,如图所示:
示例代码:
//You first need to include the following two files in your movie. //These two files contain pre-loading functions and application //messages for the chart. //Note: If you're loading multiple charts in your Flash movie, you //do NOT need to include these files for each chart. You can put these //lines in the main timeline, so that it gets loaded only once. #include "com/fusioncharts/includes/LoadingFunctions.as" #include "com/fusioncharts/includes/AppMessages.as" //To create the chart, you now need to import the Class of the //chart which you want to create. All charts are present in the package //com.fusioncharts.core.charts (Download Package > SourceCode folder) //If you're using multiple charts, you can import all the requisite //chart classes in the main timeline of your movie. That way, you //will not have to import the chart classes everytime you wish to use. import com.fusioncharts.core.charts.Column2DChart; import com.fusioncharts.core.charts.Line2DChart; // ------------- XML Data for the charts -------------- // //Data for chart 1 var strXML1:String = "<chart showBorder='0' bgAlpha='0,0' palette='1' caption='Hourly Working Rate' numberPrefix='$'>"; //Add simple data for demo. strXML1 = strXML1+"<set label='John' value='32' />"; strXML1 = strXML1+"<set label='Mary' value='65' />"; strXML1 = strXML1+"<set label='Michelle' value='29' />"; strXML1 = strXML1+"<set label='Cary' value='43' />"; strXML1 = strXML1+"</chart>"; var xmlData1:XML = new XML(strXML1); // Data for Chart 2 var strXML2:String = "<chart showBorder='0' bgAlpha='0,0' palette='1' caption='Hours Worked Last week' canvasPadding='20'>"; //Add simple data for demo. strXML2 = strXML2+"<set label='John' value='49' />"; strXML2 = strXML2+"<set label='Mary' value='34' />"; strXML2 = strXML2+"<set label='Michelle' value='61' />"; strXML2 = strXML2+"<set label='Cary' value='40' />"; strXML2 = strXML2+"</chart>"; var xmlData2:XML = new XML(strXML2); // --------------------------------------------------- // // -------------- Actual Code to create the chart ------------// //Create movie clips required for both the charts var chartContainer1MC:MovieClip = this.createEmptyMovieClip("ChartHolder1", 1); var chartContainer2MC:MovieClip = this.createEmptyMovieClip("ChartHolder2", 2); //Now, instantiate the charts using Constructor function of the chart. var chart1:Column2DChart = new Column2DChart(chartContainer1MC, 1, 380, 325, 20, 15, false, "EN", "noScale"); var chart2:Line2DChart = new Line2DChart(chartContainer2MC, 1, 380, 325, 440, 15, false, "EN", "noScale"); //Convey the XML data to chart. chart1.setXMLData(xmlData1); chart2.setXMLData(xmlData2); //Draw the charts chart1.render(); chart2.render(); //Stop stop();
解释一下上面的代码:
1、首先加进了com/fusioncharts/includes/LoadingFunctions.as & com/fusioncharts/includes/AppMessages.as ,预加载图表所需的文件;
2、然后,我们要绘制两个图表,所以导入Column2DChart 类和Line2DChart 类;
3、为两个图表创建XML数据(在此例中,是硬编码的XML数据),你也可以根据数据源动态创建XML数据;
4、将XML数据转换为XML对象;
5、然后为两个图表分别创建两个空的动画片段;
6、为两个图表创建实例以及所需的参数。设置x和y轴平移线以便在柱状图右边绘制;
7、用setXMLData 传递XML数据;
8、最后,向每个图表的实例对象调用 render() 方法,绘制两个图表。
查看应用程序,输出效果如下图:
在本例中,我们只加载了2D柱状图和线形图,你也可以加载FusionCharts的其他图表类型。另外,你还可以加载任意数量的图表到你的应用程序中。