TernaryDiagram.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. 
  2. using OTSCommon.Model;
  3. using OTSIncAReportApp.DataOperation.DataAccess;
  4. using OTSIncAReportApp.OTSSampleReportInfo;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using static OTSIncAReportApp.OTSReport_Export;
  14. namespace OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration
  15. {
  16. /// <summary>
  17. /// 三元图
  18. /// </summary>
  19. class TernaryDiagram
  20. {
  21. /// <summary>
  22. /// 三元图列表
  23. /// </summary>
  24. List<string> TemplateList;
  25. frmReportConditionChoose m_condition;
  26. public int condition = -1;
  27. //绘制图例
  28. public Bitmap DrawATernaryDiagramLegend(List<Color> Color_list, OTSReport_Export m_otsreport_export, c_TemplateClass m_mbszclass)
  29. {
  30. Bitmap map = new Bitmap(260, 115);
  31. Graphics g = Graphics.FromImage(map);
  32. SolidBrush sbrush_White = new SolidBrush(Color.White);
  33. SolidBrush sbrush = new SolidBrush(Color.Black);
  34. g.FillRectangle(sbrush_White, 0, 0, 260, 115);
  35. Font myFont = new Font("Arial", 10, FontStyle.Regular);
  36. Font myFont2 = new Font("Arial", 10, FontStyle.Regular);
  37. g.DrawString("(length,μm)", myFont, sbrush, 88, 3);
  38. //legend(length,microns)
  39. Pen mypen = new Pen(Color.Black, 1);
  40. g.DrawLine(mypen, 70, 19, 170, 19);
  41. //设置标签名称
  42. List<string> listName = new List<string>();
  43. //获取粒级表
  44. string pathe = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFileFolder + m_mbszclass.M_SYXT.str_cb_syxt_ljb.ToString();
  45. DataSet ds = XMLoperate.GetXml(pathe);
  46. string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
  47. for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
  48. {
  49. if (sizestr.Split(',')[i].Length > 0)
  50. {
  51. double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
  52. double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
  53. listName.Add(d1.ToString() + "~" + d2.ToString());
  54. }
  55. }
  56. double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
  57. listName.Add(d.ToString() + "~MAX");
  58. int PositionJeight = 27;
  59. //string str_unit = " μm";
  60. string str_unit = "";
  61. for (int i = 0; i < listName.Count; i = i + 3)
  62. {
  63. if (i < listName.Count)
  64. {
  65. SolidBrush sbrush_Color = new SolidBrush(Color_list[i]);
  66. g.DrawRectangle(mypen, 0, PositionJeight, 8, 8);
  67. g.FillRectangle(sbrush_Color, 1, PositionJeight + 1, 7, 7);
  68. g.DrawString(listName[i].ToString() + str_unit, myFont2, sbrush, 12, PositionJeight - 2);
  69. }
  70. if (i + 1 < listName.Count)
  71. {
  72. SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 1]);
  73. g.DrawRectangle(mypen, 94, PositionJeight, 8, 8);
  74. g.FillRectangle(sbrush_Color, 95, PositionJeight + 1, 7, 7);
  75. g.DrawString(listName[i + 1].ToString() + str_unit, myFont2, sbrush, 106, PositionJeight - 2);
  76. }
  77. if (i + 2 < listName.Count)
  78. {
  79. SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 2]);
  80. g.DrawRectangle(mypen, 188, PositionJeight, 8, 8);
  81. g.FillRectangle(sbrush_Color, 189, PositionJeight + 1, 7, 7);
  82. g.DrawString(listName[i + 2].ToString() + str_unit, myFont2, sbrush, 199, PositionJeight - 2);
  83. }
  84. PositionJeight = PositionJeight + 15;
  85. }
  86. return map;
  87. }
  88. public Bitmap DrawATernaryPicture(int selectindex, c_TemplateClass m_mbszclass, List<Color> Color_list,OTSReport_Export m_otsreport_export)
  89. {
  90. initialization(m_otsreport_export);
  91. ResultFile resultFile = m_otsreport_export.m_ReportApp.m_rstDataMgr.ResultFilesList[m_otsreport_export.m_ReportApp.m_rstDataMgr.getSelectedIndex()];
  92. string template = TemplateList[selectindex];
  93. if (template == "")
  94. { //三元相图模板
  95. template = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.TRIO_CHART_TYPE).ToString();
  96. }
  97. //获取粒级表
  98. string pathtpfs = Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
  99. List<string> nameList = new List<string>();
  100. DataSet ds2 = XMLoperate.GetXmlData(pathtpfs, "XMLData");
  101. DataTable dt = ds2.Tables["Member"];
  102. string TemplateName = "";
  103. //遍历第一层节点
  104. foreach (DataRow element in dt.Rows)
  105. {
  106. TemplateName = element["TemplateName"].ToString();
  107. if (TemplateName == template)
  108. {
  109. string Element = element["Element"].ToString();
  110. nameList.Add(Element.Split('.')[0]);
  111. nameList.Add(Element.Split('.')[1]);
  112. nameList.Add(Element.Split('.')[2]);
  113. break;
  114. }
  115. }
  116. DataTable dt_point_sort = new DataTable();
  117. dt_point_sort.Columns.Add("X", typeof(double));
  118. dt_point_sort.Columns.Add("Y", typeof(double));
  119. dt_point_sort.Columns.Add("Color_position");
  120. DataTable particles = GetParticles(resultFile.FilePath, nameList, 0,m_otsreport_export,m_mbszclass);
  121. foreach (DataRow item in particles.Rows)
  122. {
  123. if (item["particleLocation"].ToString() == "0,0,0")
  124. {
  125. continue;
  126. }
  127. double top = Convert.ToDouble(item["top"]);
  128. double left = Convert.ToDouble(item["left"]);
  129. double right = Convert.ToDouble(item["right"]);
  130. //x=right+top/2,y=(√3/2)*top
  131. double Y = 0.866 * top * 500;
  132. double X = (right + top / 2) * 500;
  133. double[] point_ = { Math.Round(X, 2), Math.Round(Y) };
  134. DataRow dr = dt_point_sort.NewRow();
  135. dr["X"] = Math.Round(X, 2);
  136. dr["Y"] = Math.Round(Y);
  137. dr["Color_position"] = item["Color_position"];
  138. dt_point_sort.Rows.Add(dr);
  139. }
  140. #region 图形
  141. int Line = 500;
  142. Bitmap map = new Bitmap(530, 530);
  143. Graphics g = Graphics.FromImage(map);
  144. SolidBrush sbrush_White = new SolidBrush(Color.White);
  145. g.FillRectangle(sbrush_White, 0, 0, 530, 530);
  146. Pen mypen = new Pen(Color.Black, 1);
  147. g.DrawImage(map, 0, 0, map.Width, map.Height);
  148. g.DrawLine(mypen, new Point(Line + 10, Line), new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))));//"\"
  149. g.DrawLine(mypen, new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))), new Point(0 + 10, Line));//"/"
  150. g.DrawLine(mypen, new Point(0 + 10, Line), new Point(Line + 10, Line));//"_"
  151. mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
  152. mypen.DashPattern = new float[] { 2, 2 };
  153. mypen = new Pen(Color.FromArgb(212, 212, 212), 1);
  154. int point = Line / 10;
  155. for (int i = 1; i < 10; i++)
  156. {
  157. //"\"
  158. int x1_a = point / 2 + ((point / 2) * (i - 1));
  159. int x2_a = Line - (43 * i);
  160. int y1_a = point * i;
  161. g.DrawLine(mypen, new Point(x1_a + 10, x2_a), new Point(y1_a + 10, Line));
  162. //"/"
  163. int x1_b = Line - (point / 2 + ((point / 2) * (i - 1)));
  164. int x2_b = Line - (43 * i);
  165. int y1_b = Line - (point * i);
  166. g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(y1_b + 10, Line));
  167. //"_"
  168. int x1_c = point / 2 + ((point / 2) * (i - 1));
  169. int x2_c = Line - (43 * i);
  170. int y1_c = Line - (point / 2 + ((point / 2) * (i - 1)));
  171. int y2_c = Line - (43 * i);
  172. g.DrawLine(mypen, new Point(x1_c + 10, x2_c), new Point(y1_c + 10, y2_c));
  173. g.DrawLine(mypen, new Point(y1_a + 10, Line + 10), new Point(y1_a + 10, Line));
  174. g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(x1_b + 10 + 10, x2_b));
  175. g.DrawLine(mypen, new Point(x1_c + 10 - 10, x2_c), new Point(y1_c + 10, y2_c));
  176. g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(x1_c - 20, x2_c - 5));
  177. g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(y1_b, Line + 8));
  178. g.DrawString(((10 - i) * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(x1_b + 20, x2_b - 5));
  179. }
  180. for (int i = 0; i < dt_point_sort.Rows.Count; i++)
  181. {
  182. mypen = new Pen(Color.Black, 1);
  183. SolidBrush mysbrush = new SolidBrush(Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])]);
  184. g.FillRectangle(mysbrush, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 8, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 4, 5);
  185. g.DrawRectangle(mypen, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 7, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 5, 5);
  186. }
  187. Font myFont = new Font("Arial Unicode MS", 13, FontStyle.Bold);
  188. Font Font_features = new Font("Arial Unicode MS", 10, FontStyle.Bold);
  189. SolidBrush sbrush = new SolidBrush(Color.Black);
  190. g.DrawString("features:" + dt_point_sort.Rows.Count.ToString(), Font_features, sbrush, 400, 200);
  191. g.DrawString(TemplateName, myFont, sbrush, 265 - (TemplateName.Length * 10 / 2), 10);
  192. g.DrawString(TemplateName.Split('.')[0].ToString(), myFont, sbrush, 265 - (TemplateName.Split('.')[0].ToString().Length * 10 / 2), 45);//顶点名字
  193. g.DrawString(TemplateName.Split('.')[1].ToString(), myFont, sbrush, 0, 510);//左侧名字
  194. g.DrawString(TemplateName.Split('.')[2].ToString(), myFont, sbrush, 500 - (TemplateName.Split('.')[0].ToString().Length * 10), 510);//左侧名字
  195. #endregion
  196. return map;
  197. }
  198. private void initialization(OTSReport_Export m_otsreport_export)
  199. {
  200. //加载三元相图各项
  201. string pathtpf = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TrigTemplateFileFolder + "\\" + m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TriTempFile;// Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
  202. TemplateList = new List<string>();
  203. DataSet ds = DataOperation.DataAccess.XMLoperate.GetXmlData(pathtpf, "XMLData");
  204. DataTable dt = ds.Tables["Member"];
  205. foreach (DataRow item in dt.Rows)
  206. {
  207. if (item["TemplateName"].ToString() != "")
  208. {
  209. TemplateList.Add(item["TemplateName"].ToString());
  210. }
  211. }
  212. m_condition = m_otsreport_export.m_ReportApp.m_conditionChoose;
  213. }
  214. private DataTable GetParticles(string filepath, List<string> nameList, int sel, OTSReport_Export m_otsreport_export, c_TemplateClass m_mbszclass)
  215. {
  216. ParticleData particledata = new ParticleData(filepath);
  217. //设置标签名称
  218. List<string> listName = new List<string>();
  219. //获取粒级表
  220. string pathe = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFileFolder + m_mbszclass.M_SYXT.str_cb_syxt_ljb.ToString();
  221. DataSet ds = XMLoperate.GetXml(pathe);
  222. string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
  223. for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
  224. {
  225. if (sizestr.Split(',')[i].Length > 0)
  226. {
  227. double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
  228. double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
  229. listName.Add(d1.ToString() + "~" + d2.ToString());
  230. }
  231. }
  232. double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
  233. listName.Add(d.ToString() + "~MAX");
  234. string filedAndParticl = "";
  235. string po = "";
  236. string con = "";
  237. int row = m_condition.m_conditionData.ConditionItemList.Count;
  238. if (condition != -1)
  239. {
  240. List<string> li = new List<string>() { "DMAX", "DMIN", "Area", "FERET" };
  241. con = li[condition];
  242. }
  243. else
  244. {
  245. if (row < 4)
  246. {
  247. con = "DMAX";
  248. }
  249. else
  250. {
  251. con = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.SIZE_CAL_METHOD_TYPE).ToString();
  252. }
  253. }
  254. switch (con)
  255. {
  256. case "DMAX":
  257. po = "DMAX";
  258. break;
  259. case "DMIN":
  260. po = "DMIN";
  261. break;
  262. case "ECD":
  263. po = "Area";
  264. break;
  265. case "FERET":
  266. po = "DFERET";
  267. break;
  268. }
  269. DataTable dtp = particledata.GetParticleAllHaveXray(filedAndParticl);
  270. for (int i = 0; i < dtp.Rows.Count; i++)
  271. {
  272. if (Convert.ToInt32(dtp.Rows[i]["TypeId"]) < 0 || dtp.Rows[i]["TypeName"].ToString() == "Invalid")
  273. {
  274. dtp.Rows[i].Delete();
  275. }
  276. }
  277. dtp.AcceptChanges();
  278. //创建一个临时表
  279. DataTable ret_dt = new DataTable();
  280. //然后额外存放三列,用于存放拆分后三个顶点的值
  281. ret_dt.Columns.Add("sizeLevel");
  282. ret_dt.Columns.Add("particleLocation");
  283. ret_dt.Columns.Add("top");
  284. ret_dt.Columns.Add("left");
  285. ret_dt.Columns.Add("right");
  286. ret_dt.Columns.Add("Color_position");
  287. for (int k = 0; k < listName.Count; k++)
  288. {
  289. string str = listName[k];
  290. string d1 = str.Split('~')[0];
  291. string d2 = str.Split('~')[1];
  292. if (d2 == "MAX")
  293. {
  294. d2 = "999";
  295. }
  296. DataRow[] datas = dtp.Select(getWhere(d2, d1, po));
  297. foreach (var item in datas)
  298. {
  299. DataRow dr = ret_dt.NewRow();
  300. dr["sizeLevel"] = str;
  301. string element = item["Element"].ToString();
  302. string strRet = getStrRet(nameList, element);
  303. dr["particleLocation"] = strRet;
  304. dr["top"] = strRet.Split(',')[0];
  305. dr["left"] = strRet.Split(',')[1];
  306. dr["right"] = strRet.Split(',')[2];
  307. dr["Color_position"] = k.ToString();
  308. ret_dt.Rows.Add(dr);
  309. }
  310. }
  311. return ret_dt;
  312. }
  313. private string getWhere(string max, string min, string col)
  314. {
  315. return col + ">=" + min + " and " + col + "<=" + max;
  316. }
  317. private string getStrRet(List<string> template, string element)
  318. {
  319. List<string> name = new List<string>()
  320. { "h","he",
  321. "li","be","b","c","n","o","f","ne",
  322. "na","mg","al","si","p","s","cl","ar",
  323. "k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr",
  324. "rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe",
  325. "cs","ba","la",
  326. "ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu",
  327. "hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn",
  328. "fr","ra","ac",
  329. "th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr"
  330. };
  331. List<double> value = new List<double>()
  332. { 1.008,4.003,
  333. 6.941,9.012,10.811,12.011,14.007,15.999,18.998,20.180,
  334. 22.990,24.305,26.982,28.086,30.974,32.066,35.453,39.948,
  335. 39.098,40.08,44.956,47.88,50.942,51.996,54.938,55.847,58.933,58.70,63.546,65.39,69.72,72.61,74.922,78.96,79.904,83.80,
  336. 85.468,87.62,88.906,91.22,92.906,95.94,98.00,101.07,102.906,106.42,107.868,112.41,114.82,118.71,121.76,127.60,126.905,131.29,
  337. 132.905,137.33,138.906,
  338. 140.12,140.908,144.24,145.0,150.36,151.97,157.25,158.925,162.50,64.93,167.26,168.934,173.04,174.967,
  339. 178.49,180.948,183.85,186.207,190.20,192.22,195.08,196.967,200.59,204.38,207.2,208.980,209.00,210.00,222.00,
  340. 223.00,226.025,227.028,
  341. 232.038,231.036,238.029,237.048,244.00,243.00,247.00,247.00,251.00,252.00,257.00,258.00,259.00,260.00
  342. };
  343. double d_ASum = 0;
  344. double d_BSum = 0;
  345. double d_CSum = 0;
  346. string aElements = template[0];
  347. string bElements = template[1];
  348. string cElements = template[2];
  349. foreach (var item in element.Split(';'))
  350. {
  351. if (item.Contains(aElements + "-"))
  352. {
  353. if (aElements != "")
  354. {
  355. d_ASum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(aElements.ToLower())];
  356. }
  357. }
  358. for (int i = 0; i < bElements.Split(',').Length; i++)
  359. {
  360. string e = bElements.Split(',')[i];
  361. if (item.Contains(e + "-"))
  362. {
  363. d_BSum = d_BSum + Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(e.ToLower())];
  364. }
  365. }
  366. if (item.Contains(cElements + "-"))
  367. {
  368. if (cElements != "")
  369. d_CSum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(cElements.ToLower())];
  370. }
  371. }
  372. double allNums = d_ASum + d_BSum + d_CSum;
  373. string strRet = "";
  374. if ((allNums) == 0)
  375. {
  376. strRet = "0,0,0";
  377. }
  378. else
  379. {
  380. double aPercent = 0, bPercent = 0, cPercent = 0;
  381. if (d_ASum != 0)
  382. {
  383. aPercent = d_ASum / allNums;
  384. }
  385. if (d_BSum != 0)
  386. {
  387. bPercent = d_BSum / allNums;
  388. }
  389. if (d_CSum != 0)
  390. {
  391. cPercent = d_CSum / allNums;
  392. }
  393. strRet = aPercent.ToString() + "," + bPercent.ToString() + "," + cPercent.ToString();
  394. }
  395. return strRet;
  396. }
  397. }
  398. }