结合Bindows生成各种类型图表[代码]


参照Bindows Samples的ChartTest2.xml





代码:

  1        /// <summary>
  2        /// 生成随机颜色数组
  3        /// </summary>
  4        /// <param name="ColorCount">随机颜色的个数</param>
  5        /// <returns>随机颜色数组</returns>

  6        private System.Collections.ArrayList RndColor(int ColorCount)
  7        {
  8
  9            System.Collections.ArrayList al = new System.Collections.ArrayList();
 10
 11            System.Collections.ArrayList alColor = new System.Collections.ArrayList();
 12
 13            al.Add(System.Drawing.Color.Crimson);
 14            al.Add(System.Drawing.Color.DeepPink);
 15            al.Add(System.Drawing.Color.DarkMagenta);
 16            al.Add(System.Drawing.Color.DarkViolet);
 17            al.Add(System.Drawing.Color.Indigo);
 18            al.Add(System.Drawing.Color.DarkSlateBlue);
 19            al.Add(System.Drawing.Color.Blue);
 20            al.Add(System.Drawing.Color.MidnightBlue);
 21            al.Add(System.Drawing.Color.RoyalBlue);
 22            al.Add(System.Drawing.Color.DodgerBlue);
 23            al.Add(System.Drawing.Color.SteelBlue);
 24            al.Add(System.Drawing.Color.CadetBlue);
 25            al.Add(System.Drawing.Color.DarkSlateGray);
 26            al.Add(System.Drawing.Color.Teal);
 27            al.Add(System.Drawing.Color.SeaGreen);
 28            al.Add(System.Drawing.Color.Green);
 29            al.Add(System.Drawing.Color.DarkGreen);
 30            al.Add(System.Drawing.Color.OliveDrab);
 31            al.Add(System.Drawing.Color.Olive);
 32            al.Add(System.Drawing.Color.Gold);
 33            al.Add(System.Drawing.Color.Goldenrod);
 34            al.Add(System.Drawing.Color.Orange);
 35            al.Add(System.Drawing.Color.DarkOrange);
 36            al.Add(System.Drawing.Color.Chocolate);
 37            al.Add(System.Drawing.Color.Sienna);
 38            al.Add(System.Drawing.Color.OrangeRed);
 39            al.Add(System.Drawing.Color.Tomato);
 40            al.Add(System.Drawing.Color.Red);
 41            al.Add(System.Drawing.Color.Brown);
 42            al.Add(System.Drawing.Color.DimGray);
 43
 44            int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数
 45
 46            //采用一个简单的算法以保证生成随机颜色的不同
 47            Random rand =new Random();
 48            for ( int i = 1 ; i < ColorCount + 1 ; i++ ) 
 49            {    
 50                if ( temp != -1
 51                {
 52                    rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
 53                }
    
 54                int t = rand.Next(30);
 55                if (temp != -1 && temp == t) 
 56                {
 57                    return RndColor(ColorCount);
 58                }

 59
 60                temp = t  ;
 61
 62                alColor.Add(al[t]);
 63                
 64            }

 65        
 66            return alColor;
 67        
 68        }

 69
 70
 71
 72
 73
 74
 75
 76        /// <summary>
 77        /// 多种类型图表的调用文件
 78        /// </summary>
 79        /// <param name="标题">Bindows窗体的标题</param>
 80        /// <param name="宽度">Bindows窗体的宽度</param>
 81        /// <param name="高度">Bindows窗体的高度</param>
 82        /// <param name="附属文件名">此主文件调用的数据文件的文件名的前半部分</param>
 83        /// <returns>生成主文件时的TimeStamp,供生成数据文件的文件名使用</returns>

 84        private string AllChartMain(string 标题, int 宽度, int 高度, string 附属文件名)
 85        {
 86            System.Text.StringBuilder sbContent = new System.Text.StringBuilder(100);
 87
 88            sbContent.Append("<?xml version=\"1.0\" ?>\n");
 89
 90            sbContent.Append("<application>\n");
 91
 92            sbContent.Append("<window caption=\"" + 标题 + "\" width=\"" + 宽度.ToString() + "\" height=\"" + 高度.ToString() + "\" centered=\"true\" />\n");
 93
 94            sbContent.Append("<resources>\n");
 95
 96            sbContent.Append("<script>\n");
 97
 98            sbContent.Append("<![CDATA[ \n");
 99
100            string sbCN = this.CurTimeStamp();
101
102            sbContent.Append("function " + 附属文件名 + "Main" + sbCN + "() {\n");
103
104            sbContent.Append("var win = application.getWindow();\n");
105
106            sbContent.Append("var cb = new BiComboBox([\"line\", \"column\", \"bar\", \"pie\", \"grid\"]);\n");
107
108            sbContent.Append("win.add(cb);\n");
109
110            sbContent.Append("cb.setLocation(10, 10);\n");
111
112            sbContent.Append("cb.setWidth(150);\n");
113
114            sbContent.Append("var oDoc = BiXmlLoader.load( \"" + 附属文件名 + sbCN + ".xml\" );\n");
115
116            sbContent.Append("if (oDoc.parseError.errorCode != 0) {\n");
117
118            sbContent.Append("alert(\"无法分析查询到的数据!\");\n");
119
120            sbContent.Append("return;\n");
121
122            sbContent.Append("}\n");
123
124            sbContent.Append("var graph = BiGraph.fromXmlDocument(oDoc);\n");
125
126            sbContent.Append("win.add(graph);\n");
127
128            sbContent.Append("graph.update();\n");
129
130            sbContent.Append("graph.setLocation(10, cb.getHeight() + 20);\n");
131
132            sbContent.Append("graph.setRight(10);\n");
133
134            sbContent.Append("graph.setBottom(10);\n");
135
136            sbContent.Append("graph.setBorder( new BiBorder(1, \"solid\", \"black\") );\n");
137
138            sbContent.Append("graph.setBackColor(\"#cfe0f2\");\n");
139
140            sbContent.Append("cb.findString(graph.getChartType()).setSelected(true);\n");
141
142            sbContent.Append("cb.addEventListener(\"change\", function (e) {\n");
143
144            sbContent.Append("graph.setChartType(cb.getSelectedItem().getText());\n");
145
146            sbContent.Append("graph.update();\n");
147
148            sbContent.Append("}, this);\n");
149
150            sbContent.Append("oDoc = null;\n");
151
152            sbContent.Append("}\n");
153
154            sbContent.Append("" + 附属文件名 + "Main" + sbCN + ".main = function () { new " + 附属文件名 + "Main" + sbCN + "; };\n");
155
156            sbContent.Append("]]>\n");
157
158            sbContent.Append("</script>\n");
159
160            sbContent.Append("</resources>\n");
161
162            sbContent.Append("</application>\n");
163
164            string strRet = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"html\html\bindows\launcher\" + 附属文件名 + "Main" + sbCN + ".xml");
165
166            System.IO.StreamWriter swResult = new System.IO.StreamWriter(strRet, false, System.Text.Encoding.UTF8);
167
168            swResult.Write(sbContent.ToString());
169
170            swResult.Close();
171
172            return sbCN;
173
174        }

175
176
177
178
179
180
181
182
183
184
185
186
187
188        /// <summary>
189        ///    多种类型图表的数据文件        
190        /// </summary>
191        /// <param name="视图">使用的视图</param>
192        /// <param name="中间字段">构造的视图可能要包含多余的字段,设定此值介定</param>
193        /// <param name="查询条件">视图的筛选条件</param>
194        /// <param name="图表类型">图表类型,可用Bindows提供的各种图表类型</param>

195        private void AllChart(string 标题, string 文件名, int 宽度, int 高度, string 视图, int 中间字段, string 查询条件, string 图表类型)
196        {
197            this.win = this.doc.parentWindow;
198
199            System.Text.StringBuilder sbContent = new System.Text.StringBuilder(100);
200
201            sbContent.Append("<?xml version=\"1.0\" ?>\n");
202
203            sbContent.Append("<Graph>\n");
204
205            sbContent.Append("<Data>\n");
206
207            sbContent.Append("<Categories>\n");
208
209            string strSQL = "SELECT * FROM " + 视图 + " WHERE " + 查询条件;
210
211            System.Data.DataSet dsResult = HEWin.Sys.sysDb.GetDataSetBySql(strSQL, "Result");
212
213            if (dsResult.Tables["Result"].Rows.Count == 0)
214            {
215                this.win.execScript("showMD(\"环境检测管理系统\", \"../../errnorecordset.htm\", true, false, false)""javascript");
216                dsResult.Dispose();
217                return;
218            }

219
220            strSQL = "SELECT syscolumns.name, syscolumns.colid FROM syscolumns INNER JOIN sysobjects ON syscolumns.id = sysobjects.id WHERE (sysobjects.name = N'" + 视图 + "') ORDER BY syscolumns.colid";
221
222            System.Data.DataSet dsTable = HEWin.Sys.sysDb.GetDataSetBySql(strSQL, "Table");
223
224            if (dsTable.Tables["Table"].Rows.Count < 中间字段 + 1)
225            {
226                this.win.execScript("showMD(\"环境检测管理系统\", \"../../errquery.htm\", true, false, false)""javascript");
227                dsTable.Dispose();
228                return;            
229            }

230
231            int intStart = 0;
232
233            System.Collections.ArrayList alColor = this.RndColor(10);
234
235            string ChartColor = "";
236
237            for (int i = 0; i < dsResult.Tables["Result"].Rows.Count; i ++)
238            {
239                sbContent.Append("<Category Id=\"c" + i.ToString() + "\">\n");
240                sbContent.Append("<Title>" + dsResult.Tables["Result"].Rows[i][0].ToString() + "</Title>\n");
241                sbContent.Append("</Category>\n");
242            }

243
244            sbContent.Append("</Categories>\n");
245
246            sbContent.Append("<SeriesGroup>\n");
247
248            intStart = 0;
249
250            for(int i = 中间字段; i < dsTable.Tables["Table"].Rows.Count; i ++)
251            {
252                sbContent.Append("<Series Id=\"s" + intStart.ToString() + "\">\n");
253
254                sbContent.Append("<Title>" + dsTable.Tables["Table"].Rows[i]["name"].ToString() + "</Title>\n");
255
256                sbContent.Append("<Values>\n");
257
258                ChartColor += "<Chart Series=\"s" + i.ToString() + "\">\n";
259                ChartColor += "<Fill Color=\"#" + ((System.Drawing.Color)alColor[intStart]).R.ToString("X") + ((System.Drawing.Color)alColor[i]).G.ToString("X") + ((System.Drawing.Color)alColor[i]).B.ToString("X") + "\" />\n";
260                ChartColor += "</Chart>\n";
261
262                for (int j = 0; j < dsResult.Tables["Result"].Rows.Count; j ++)
263                {
264                    sbContent.Append("<Value Category=\"c" + j.ToString() + "\">" + dsResult.Tables["Result"].Rows[j][i].ToString() + "</Value>");
265                }

266
267                sbContent.Append("</Values>\n");
268
269                sbContent.Append("</Series>\n");
270
271                intStart ++;
272
273            }

274
275            sbContent.Append("</SeriesGroup>\n");
276
277            sbContent.Append("</Data>\n");
278
279            sbContent.Append("<Presentation Type=\"" + 图表类型 + "\">\n");
280
281            sbContent.Append("<Legend Visible=\"true\" />\n");
282
283            sbContent.Append("<Axes>\n");
284
285            sbContent.Append("<ValueAxis Visible=\"true\" ShowMajorTicks=\"true\" ShowMinorTicks=\"false\">\n");
286
287            sbContent.Append("<Title>Value Axis Title</Title>\n");
288
289            sbContent.Append("</ValueAxis>\n");
290
291            sbContent.Append("<CategoryAxis Visible=\"true\" ShowMajorTicks=\"true\" ShowMinorTicks=\"false\">\n");
292
293            sbContent.Append("<Title>Category Axis Title</Title>");
294
295            sbContent.Append("</CategoryAxis>\n");
296
297            sbContent.Append("</Axes>\n");
298
299            sbContent.Append("<GridLines>\n");
300
301            sbContent.Append("<MajorValue Visible=\"true\"><Stroke Color=\"#cfe0f2\" />\n");
302
303            sbContent.Append("</MajorValue>\n");
304
305            sbContent.Append("<MinorValue Visible=\"false\" />\n");
306
307            sbContent.Append("<MajorCategory Visible=\"false\" />\n");
308
309            sbContent.Append("<MinorCategory Visible=\"false\" />\n");
310
311            sbContent.Append("</GridLines>\n");
312
313            sbContent.Append("<ChartArea>\n");
314
315            sbContent.Append("<Stroke Color=\"transparent\" />\n");
316
317            sbContent.Append("<Fill Color=\"#e3edf9\" Color2=\"white\" Type=\"Gradient\" />\n");
318
319            sbContent.Append("</ChartArea>\n");
320
321            sbContent.Append("<Points />\n");
322
323            sbContent.Append("<Charts>\n");
324
325            sbContent.Append(ChartColor);
326
327            sbContent.Append("</Charts>\n");
328
329            sbContent.Append("</Presentation>\n");
330
331            sbContent.Append("</Graph>\n");
332
333            string sbCN = this.AllChartMain(标题, 宽度, 高度, 文件名);
334
335            string strFileName = 文件名 + "Main" + sbCN + ".xml";
336            
337            string strRet = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"html\html\bindows\launcher\" + 文件名 + sbCN + ".xml");
338
339            System.IO.StreamWriter swResult = new System.IO.StreamWriter(strRet, false, System.Text.Encoding.UTF8);
340
341            swResult.Write(sbContent.ToString());
342
343            swResult.Close();
344
345            win.execScript("biExec('../html', '" + strFileName + "', false, 0, 'one', true);""JavaScript");
346
347            win = null;
348
349            dsTable.Dispose();
350
351            dsResult.Dispose();
352
353        }


Bindows中是有的颜色名如"DarkViolet"是不能用的,要转换为十六进制的颜色。
posted @ 2005-04-27 18:06  蜡人张  阅读(2241)  评论(3编辑  收藏  举报