root/as3/ChartData/src/org/libspark/snippets/controls/ChartData/FusionCharts/SingleChartData.as

リビジョン 268, 9.6 kB (コミッタ: daoki2, コミット時期: 4 年 前)

First Release

  • svn:executable 属性の設定値: *
Line 
1 /**
2  * SingleChartData - The HTML & XML Build class for FusionCharts
3  *
4  * @author      Copyright (c) 2008 daoki2
5  * @version     0.5.0
6  * @link        http://homepage.mac.com/daoki2/
7  *
8  * Copyright (c) 2008 daoki2
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28
29 package org.libspark.snippets.controls.ChartData.FusionCharts {
30     import flash.utils.ByteArray;
31     import flash.filesystem.*;
32
33     public class SingleChartData {
34         public var graphAttribute:Object = {
35             bgColor: "",
36             bgAlpha: "",
37             bgSWF: "",
38             canvasBgColor: "",
39             canvasBgAlpha: "",
40             canvasBorderColor: "",
41             canvasBorderThickness: "",
42             caption: "",
43             subCaption: "",
44             xAxisName: "",
45             yAxisName: "",
46             yAxisMinValue: "",
47             yAxisMaxValue: "",
48             showNames: "",
49             showValues: "",
50             showLimits: "",
51             rotateNames: "",
52             animation: "",
53             showColumnShadow: "",
54             baseFont: "",
55             baseFontSize: "",
56             baseFontColor: "",
57             outCnvBaseFont: "",
58             outCnvBaseFontSize: "",
59             outCnvBaseFontColor: "",
60             numberPrefix: "",
61             numberSuffix: "",
62             formatNumber: "",
63             formatNumberScale: "",
64             decimalSeparator: "",
65             thousandSeparator: "",
66             decimalPrecision: "",
67             divLineDecimalPrecision: "",
68             limitsDecimalPrecision: "",
69             zeroPlaneThickness: "",
70             zeroPlaneColor: "",
71             zeroPlaneAlpha: "",
72             numdivlines: "",
73             divlinecolor: "",
74             divLineThickness: "",
75             divLineAlpha: "",
76             showDivLineValue: "",
77             showAlternateHGridColor: "",
78             alternateHGridColor: "",
79             alternateHGridAlpha: "",
80             numVDivLines: "",
81             VDivlinecolor: "",
82             VDivLineThickness: "",
83             VDivLineAlpha: "",
84             alternateVGridColor: "",
85             alternateVGridAlpha: "",
86             showhovercap: "",
87             hoverCapBgColor: "",
88             hoverCapBorderColor: "",
89             hoverCapSepChar: "",
90             charLeftMargin: "",
91             charRightMargin: "",
92             charTopMargin: "",
93             charBottomMargin: ""
94         };
95
96         private var setElements:Array = new Array();
97         private var lineElements:Array = new Array();
98         private var defaultColor:Array = [
99             "AFD8F8", "F6BD0F", "8BBA00", "FF8E46", "008E8E", "D64646", "8E468E", "588526", "B3AA00", "008ED6", "9D080D", "A186BE"
100         ];
101
102         /**
103          * Constructor
104          */
105         public function SingleChartData():void {
106         }
107
108         /**
109          * Create HTML File
110          * @param  file         HTML file to create
111          * @param  chartType    Chart type (Column2D, Column3D, Pie2D, Pie3D, Line, Bar2D, Area2D, Doughnut2D)
112          * @param  width        Chart width
113          * @param  height       Chart height
114          * @param  dataFilename Data file name string
115          * @return object       If the operation succeed. Contains the following property.
116          *                      - status : true/false
117          *                      - message: message text
118          */
119         public function createHtmlFile(file:File, chartType:String, width:uint, height:uint, dataFilename:String):Object {
120             var appDir:String = File.applicationDirectory.nativePath.split("\\").join("/");
121             var html:String = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
122             html += "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
123             html += "  <head>\n";
124             html += "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
125             html += "    <script language=\"javascript\" src=\"file://" + appDir + "/JSClass/FusionCharts.js\"></script>\n";
126             html += "  </head>\n";
127             html += "  <body>\n";
128             html += "    <div id=\"FusionCharts\"/>\n";
129             html += "    <script language=\"javascript\" type=\"text/javascript\">\n";
130             html += "      var chart = new FusionCharts(\"file://" + appDir + "/Charts/FCF_" + chartType + ".swf\", \"ChartId\", \"" + width + "\", \"" + height + "\");\n";
131             html += "      chart.setDataURL(\"file://" + dataFilename.split("\\").join("/") + "\");\n";
132             html += "      chart.render(\"FusionCharts\");\n";
133             html += "    </script>\n";
134             html += "  </body>\n";
135             html += "</html>";
136             try {
137                 var fs:FileStream = new FileStream();
138                 fs.open(file, FileMode.WRITE);
139                 fs.writeUTFBytes(html);
140                 fs.close();
141             } catch (err:Error) {
142                 return {status: false, message: err.message};
143             }
144             return {status: true, message: ""};
145         }
146
147         /**
148          * Create Data File
149          * @param  file         Data file to create
150          * @return object       If the operation succeed. Contains the following property.
151          *                      - status : true/false
152          *                      - message: message text
153          */
154         public function createDataFile(file:File):Object {
155             var BOM:ByteArray = new ByteArray(); // Byte Order Mark of UTF-8
156             BOM.writeByte(0xEF);
157             BOM.writeByte(0xBB);
158             BOM.writeByte(0xBF);
159             try {
160                 var fs:FileStream = new FileStream();
161                 fs.open(file, FileMode.WRITE);
162                 fs.writeBytes(BOM);
163                 fs.writeUTFBytes(toXML());
164                 fs.close();
165             } catch (err:Error) {
166                 return {status: false, message: err.message};
167             }
168             return {status: true, message: ""};
169         }
170
171         private function toXML():String {
172             var xml:String = "<graph";
173             for (var val:* in this.graphAttribute) {
174                 if (this.graphAttribute[val] != "")
175                     xml += " " + val + "=\"" + this.graphAttribute[val] + "\"";
176             }
177             xml += ">\n";
178             xml += toSetElement();
179             xml += toTrendLinesElement();
180             xml += "</graph>";
181             return xml;
182         }
183
184         private function toSetElement():String {
185             var result:String = "";
186             var index:uint = 0;
187             if (this.setElements.length > 0) {
188                 for each (var val:* in this.setElements) {
189                     result += "<set";
190                     for (var attr:* in val) {
191                         if (val[attr] != "") {
192                             result += " " + attr + "=\"" + val[attr] + "\"";
193                         } else {
194                             if (attr == "color") {
195                                 result += " " + attr + "=\"" + defaultColor[index++] + "\"";
196                                 if (index >= defaultColor.length)
197                                     index = 0;
198                             }
199                         }
200                     }
201                     result += "/>\n";
202                 }
203             }
204             return result;
205         }
206
207         private function toTrendLinesElement():String {
208             var result:String = "";
209             if (this.lineElements.length > 0) {
210                 result += "<trendLines>\n";
211                 for each (var val:* in this.lineElements) {
212                     result += "<line";
213                     for (var attr:* in val) {
214                         if (val[attr] != "")
215                             result += " " + attr + "=\"" + val[attr] + "\"";
216                     }
217                     result += ">\n";
218                 }
219                 result += "</trendLines>\n";
220             }
221             return result;
222         }
223
224         public function addSetElement(obj:Object):void {
225             this.setElements.push(obj);
226         }
227
228         public function addLineElement(obj:Object):void {
229             this.lineElements.push(obj);
230         }
231
232         public function getSetElement():Object {
233             var result:Object = new Object();
234             result = {name: "", value: "", color: "", hoverText: "", link: "", alpha: "", showName: ""};
235             return result;
236         }
237
238         public function getLineElement():Object {
239             var result:Object = new Object;
240             result = {startValue: "", endValue: "", color: "", displayValue: "", thickness: "", isTrendZone: "", showOnTop: "", alpha: ""};
241             return result;
242         }
243
244         public function clear():void {
245             for (var val:* in this.graphAttribute)
246                 this.graphAttribute[val] = "";
247                 this.setElements = new Array();
248                 this.lineElements = new Array();
249         }
250     }
251 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。