DEVChart_Trianglediagram.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. using NPOI.SS.UserModel;
  2. using NPOI.XSSF.UserModel;
  3. using OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration;
  4. using OTSIncAReportApp.DataOperation.DataAccess;
  5. using OTSIncAReportApp.OTSRstMgrFunction;
  6. using OTSIncAReportApp.OTSSampleReportInfo;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Windows.Forms;
  13. namespace OTSIncAReportApp.Control_ECharts
  14. {
  15. /// <summary>
  16. /// 三元相图EChart版
  17. /// </summary>
  18. public partial class DEVChart_Trianglediagram : UserControl
  19. {
  20. #region 变量定义
  21. public List<string> m_list_string_sizelevel; //图例中:颗粒粒级字符列表
  22. public List<string> m_old_list_string_sizelevel;//图例中,颗粒粒级字符列表,未处理格式的,从底层拿出来的原始格式
  23. public List<string> m_list_string_samplename;//图例中:样品名字符列表
  24. private List<DataTable> TernaryData = new List<DataTable>();//当前三元图的数据
  25. private List<string> TernaryName = new List<string>();//三元图名称
  26. public string Template { get; set; }
  27. public int condition = -1;
  28. frmReportApp m_ReportApp;
  29. frmReportConditionChoose m_condition;
  30. ResultDataMgr m_DataMgr;
  31. private bool ReportExporting = false;
  32. public bool Report { set; get; }
  33. /// <summary>
  34. /// 判断颗粒范围(true全部颗粒,false选择颗粒)
  35. /// </summary>
  36. public bool ParticleRange { set; get; }
  37. #endregion
  38. #region 构造函数及窗体加载
  39. /// <summary>
  40. /// 三角图,构造函数
  41. /// </summary>
  42. /// <param name="in_sec">传入,委托类对象</param>
  43. /// <param name="in_Cotsreportprojfilemgrclr">传入,项目管理类对象</param>
  44. public DEVChart_Trianglediagram(frmReportApp ReportApp)
  45. {
  46. m_list_string_sizelevel = new List<string>();
  47. m_list_string_samplename = new List<string>();
  48. m_old_list_string_sizelevel = new List<string>();
  49. TernaryData = new List<DataTable>();
  50. m_ReportApp = ReportApp;
  51. m_condition = ReportApp.m_conditionChoose;
  52. m_DataMgr = ReportApp.m_rstDataMgr;
  53. InitializeComponent();
  54. }
  55. private void EChart_Trianglediagram_Load(object sender, EventArgs e)
  56. {
  57. List<DataTable> listDraw = new List<DataTable>();
  58. string template = Template;
  59. TernaryName.Clear();
  60. if (template == "")
  61. { //三元相图模板
  62. template = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.TRIO_CHART_TYPE).ToString();
  63. }
  64. string pathtpf = Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
  65. List<string> nameList = new List<string>();
  66. DataSet ds = XMLoperate.GetXmlData(pathtpf, "XMLData");
  67. DataTable dt = ds.Tables["Member"];
  68. string TemplateName ="";
  69. //设置标签名称
  70. List<string> listName = new List<string>();
  71. //获取粒级表
  72. string sizestr = "";
  73. if (m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFile == "AUTO.psf")
  74. {
  75. double dn = 99999;
  76. string sou1 = m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemDisplayVal.ToString();
  77. if (sou1.Contains("+"))
  78. {
  79. for (int i = 0; i < sou1.Split('+').Length; i++)
  80. {
  81. ResultFile resultFile = m_ReportApp.m_rstDataMgr.ResultFilesList.Find(s => s.anotherFileName == sou1.Split('+')[i]);
  82. if (resultFile != null)
  83. {
  84. if (dn > resultFile.GetParticleMINECD())
  85. {
  86. dn = resultFile.GetParticleMINECD();
  87. }
  88. }
  89. }
  90. }
  91. else
  92. {
  93. ResultFile resultFile = m_ReportApp.m_rstDataMgr.CurResultFile;
  94. dn = resultFile.GetParticleMINECD();
  95. }
  96. sizestr = dn.ToString() + ",";
  97. for (double p = dn; p < 50; p = p * Math.Sqrt(2))
  98. {
  99. if (p > dn && p <= 50.5)
  100. {
  101. double dd = Math.Round(p);
  102. sizestr += dd.ToString() + ",";
  103. }
  104. }
  105. sizestr = sizestr.Remove(sizestr.Length - 1, 1);
  106. }
  107. else
  108. {
  109. string pathe = m_DataMgr.m_RptConfigFile.PartSizeFileFolder + m_DataMgr.m_RptConfigFile.PartSizeFile;
  110. DataSet ds2 = XMLoperate.GetXml(pathe);
  111. sizestr = ds2.Tables[0].Rows[0]["Sizes"].ToString();
  112. }
  113. for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
  114. {
  115. if (sizestr.Split(',')[i].Length > 0)
  116. {
  117. double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
  118. double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
  119. listName.Add(d1.ToString() + "~" + d2.ToString());
  120. }
  121. }
  122. double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
  123. listName.Add(d.ToString() + "~MAX");
  124. Bitmap map = new Bitmap(950, 115);
  125. Graphics g = Graphics.FromImage(map);
  126. SolidBrush sbrush_White = new SolidBrush(Color.White);
  127. SolidBrush sbrush = new SolidBrush(Color.Black);
  128. g.FillRectangle(sbrush_White, 0, 0, 950, 115);
  129. Font myFont = new Font("Arial", 10, FontStyle.Regular);
  130. Font myFont2 = new Font("Arial", 10, FontStyle.Regular);
  131. Pen mypen = new Pen(Color.Black, 1);
  132. List<Color> Color_list = SetColor_list();
  133. string str_unit = "";
  134. int PositionJeight = 27;
  135. TernaryDiagram ternaryDiagram = new TernaryDiagram();
  136. for (int i = 0; i < listName.Count; i = i + 8)
  137. {
  138. if (i < listName.Count)
  139. {
  140. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i]);
  141. ternaryDiagram.ShapeList(0, new PointF(10, PositionJeight), 10, Color_list[i], g);
  142. //g.DrawRectangle(mypen, 0, PositionJeight, 8, 8);
  143. //g.FillRectangle(sbrush_Color, 1, PositionJeight + 1, 7, 7);
  144. g.DrawString(listName[i].ToString() + str_unit, myFont2, sbrush, 22, PositionJeight - 2);
  145. }
  146. if (i + 1 < listName.Count)
  147. {
  148. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 1]);
  149. ternaryDiagram.ShapeList(1, new PointF(104, PositionJeight), 10, Color_list[i+1], g);
  150. //g.DrawRectangle(mypen, 94, PositionJeight, 8, 8);
  151. //g.FillRectangle(sbrush_Color, 95, PositionJeight + 1, 7, 7);
  152. g.DrawString(listName[i + 1].ToString() + str_unit, myFont2, sbrush, 116, PositionJeight - 2);
  153. }
  154. if (i + 2 < listName.Count)
  155. {
  156. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 2]);
  157. ternaryDiagram.ShapeList(2, new PointF(194, PositionJeight), 10, Color_list[i + 2], g);
  158. //g.DrawRectangle(mypen, 188, PositionJeight, 8, 8);
  159. //g.FillRectangle(sbrush_Color, 189, PositionJeight + 1, 7, 7);
  160. g.DrawString(listName[i + 2].ToString() + str_unit, myFont2, sbrush, 206, PositionJeight - 2);
  161. }
  162. if (i + 3 < listName.Count)
  163. {
  164. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 3]);
  165. ternaryDiagram.ShapeList(3, new PointF(292, PositionJeight), 10, Color_list[i + 3], g);
  166. //g.DrawRectangle(mypen, 282, PositionJeight, 8, 8);
  167. //g.FillRectangle(sbrush_Color, 283, PositionJeight + 1, 7, 7);
  168. g.DrawString(listName[i + 3].ToString() + str_unit, myFont2, sbrush, 304, PositionJeight - 2);
  169. }
  170. if (i + 4 < listName.Count)
  171. {
  172. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 4]);
  173. ternaryDiagram.ShapeList(0, new PointF(386, PositionJeight), 10, Color_list[i + 4], g);
  174. //g.DrawRectangle(mypen, 376, PositionJeight, 8, 8);
  175. //g.FillRectangle(sbrush_Color, 377, PositionJeight + 1, 7, 7);
  176. g.DrawString(listName[i + 4].ToString() + str_unit, myFont2, sbrush, 398, PositionJeight - 2);
  177. }
  178. if (i + 5 < listName.Count)
  179. {
  180. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 5]);
  181. ternaryDiagram.ShapeList(1, new PointF(480, PositionJeight), 10, Color_list[i + 5], g);
  182. //g.DrawRectangle(mypen, 470, PositionJeight, 8, 8);
  183. //g.FillRectangle(sbrush_Color, 471, PositionJeight + 1, 7, 7);
  184. g.DrawString(listName[i + 5].ToString() + str_unit, myFont2, sbrush, 492, PositionJeight - 2);
  185. }
  186. if (i + 6 < listName.Count)
  187. {
  188. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 6]);
  189. ternaryDiagram.ShapeList(2, new PointF(574, PositionJeight), 10, Color_list[i + 6], g);
  190. //g.DrawRectangle(mypen, 564, PositionJeight, 8, 8);
  191. //g.FillRectangle(sbrush_Color, 565, PositionJeight + 1, 7, 7);
  192. g.DrawString(listName[i + 6].ToString() + str_unit, myFont2, sbrush, 586, PositionJeight - 2);
  193. }
  194. if (i + 7 < listName.Count)
  195. {
  196. //SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 7]);
  197. ternaryDiagram.ShapeList(3, new PointF(668, PositionJeight), 10, Color_list[i + 7], g);
  198. //g.DrawRectangle(mypen, 658, PositionJeight, 8, 8);
  199. //g.FillRectangle(sbrush_Color, 659, PositionJeight + 1, 7, 7);
  200. g.DrawString(listName[i + 7].ToString() + str_unit, myFont2, sbrush, 680, PositionJeight - 2);
  201. }
  202. //if (i + 8 < listName.Count)
  203. //{
  204. // SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 5]);
  205. // g.DrawRectangle(mypen, 752, PositionJeight, 8, 8);
  206. // g.FillRectangle(sbrush_Color, 753, PositionJeight + 1, 7, 7);
  207. // g.DrawString(listName[i + 8].ToString() + str_unit, myFont2, sbrush, 762, PositionJeight - 2);
  208. //}
  209. //if (i + 9 < listName.Count)
  210. //{
  211. // SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 5]);
  212. // g.DrawRectangle(mypen, 846, PositionJeight, 8, 8);
  213. // g.FillRectangle(sbrush_Color, 847, PositionJeight + 1, 7, 7);
  214. // g.DrawString(listName[i + 9].ToString() + str_unit, myFont2, sbrush, 856, PositionJeight - 2);
  215. //}
  216. PositionJeight = PositionJeight + 15;
  217. }
  218. PictureBox pictureBox = new PictureBox();
  219. pictureBox.Size = new Size(950, 115);
  220. pictureBox.Location = new Point(40, 10);
  221. pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
  222. pictureBox.Image = map;
  223. this.panel1.Controls.Add(pictureBox);
  224. //遍历第一层节点
  225. foreach (DataRow element in dt.Rows)
  226. {
  227. string Name = element["TemplateName"].ToString();
  228. if (Name == template)
  229. {
  230. string Element = element["Element"].ToString();
  231. nameList.Add(Element.Split('.')[0]);
  232. nameList.Add(Element.Split('.')[1]);
  233. nameList.Add(Element.Split('.')[2]);
  234. TernaryName.Add(template.Split('.')[0]);
  235. TernaryName.Add(template.Split('.')[1]);
  236. TernaryName.Add(template.Split('.')[2]);
  237. TemplateName=Name;
  238. break;
  239. }
  240. }
  241. //将所有的测量结果名添加到list中
  242. m_list_string_samplename.Clear();
  243. TernaryData.Clear();
  244. string sou = "";
  245. int sel = 0;
  246. if (Report)
  247. {
  248. sou = m_DataMgr.ResultFilesList[m_DataMgr.GetWorkingResultId()].anotherFileName.ToString();
  249. if(ParticleRange)
  250. {
  251. sel = 0;
  252. }
  253. else
  254. {
  255. sel = 1;
  256. }
  257. }
  258. else
  259. {
  260. sou = m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemDisplayVal.ToString();
  261. //判断颗粒范围(全部颗粒或者选择颗粒)
  262. sel = m_ReportApp.m_conditionChoose.m_conditionData.GetComboDownListIndexByItemName(OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
  263. }
  264. Report = false;
  265. if (sou.Contains("+"))
  266. {
  267. if (m_ReportApp.more)
  268. {
  269. m_ReportApp.trans = false;
  270. }
  271. else
  272. {
  273. m_ReportApp.trans = true;
  274. }
  275. for (int i = 0; i < sou.Split('+').Length; i++)
  276. {
  277. ResultFile resultFile = m_DataMgr.ResultFilesList.Find(s => s.anotherFileName == sou.Split('+')[i]);
  278. if (resultFile == null)
  279. {
  280. continue;
  281. }
  282. DataTable cl = GettriangleJsonClass(resultFile.FilePath, resultFile.anotherFileName, nameList,sel);
  283. listDraw.Add(cl);
  284. }
  285. }
  286. else
  287. {
  288. if (!m_ReportApp.more)
  289. {
  290. m_ReportApp.trans = false;
  291. }
  292. else
  293. {
  294. m_ReportApp.trans = true;
  295. }
  296. m_ReportApp.more = false;
  297. ResultFile resultFile;
  298. int index = (int)m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemVal;
  299. if (ReportExporting)
  300. {
  301. resultFile = m_DataMgr.ResultFilesList[m_DataMgr.GetWorkingResultId()];
  302. }
  303. else
  304. {
  305. resultFile = m_DataMgr.ResultFilesList[index];
  306. }
  307. ReportExporting = false;
  308. DataTable cl = GettriangleJsonClass(resultFile.FilePath, resultFile.anotherFileName, nameList, sel);
  309. listDraw.Add(cl);
  310. }
  311. int point = 40;
  312. for (int i=0;i<listDraw.Count;i++)
  313. {
  314. Bitmap maps = Plot(listDraw[i], TemplateName, Color_list);
  315. PictureBox picture = new PictureBox();
  316. picture.Size = new Size(530, 530);
  317. picture.Location = new Point(point, 125);
  318. picture.SizeMode = PictureBoxSizeMode.StretchImage;
  319. picture.Image = maps;
  320. this.panel1.Controls.Add(picture);
  321. point = point + 570;
  322. }
  323. }
  324. /// <summary>
  325. /// 颜色列表
  326. /// </summary>
  327. /// <returns></returns>
  328. private List<Color> SetColor_list()
  329. {
  330. List<Color> Color_list = new List<Color>();
  331. Color_list.Add(FromHex("#0f4471"));
  332. Color_list.Add(FromHex("#FFD700"));
  333. Color_list.Add(FromHex("#ff5722"));
  334. Color_list.Add(FromHex("#5628b4"));
  335. Color_list.Add(FromHex("#20BF55"));
  336. Color_list.Add(FromHex("#f23557"));
  337. Color_list.Add(FromHex("#118df0"));
  338. Color_list.Add(FromHex("#FF00FF"));
  339. Color_list.Add(FromHex("#d3327b"));
  340. Color_list.Add(FromHex("#ae318a"));
  341. Color_list.Add(FromHex("#993090"));
  342. Color_list.Add(FromHex("#6f3071"));
  343. Color_list.Add(FromHex("#7711AF"));
  344. Color_list.Add(FromHex("#CF77FF"));
  345. Color_list.Add(FromHex("#AE004F"));
  346. Color_list.Add(FromHex("#F35872"));
  347. Color_list.Add(FromHex("#FA7729"));
  348. Color_list.Add(FromHex("#FFC526"));
  349. Color_list.Add(FromHex("#F8E71C"));
  350. Color_list.Add(FromHex("#34ADAE"));
  351. Color_list.Add(FromHex("#3DDFD2"));
  352. Color_list.Add(FromHex("#A0FFFF"));
  353. return Color_list;
  354. }
  355. /// <summary>
  356. /// 绘制图形
  357. /// </summary>
  358. /// <param name="dt_point_sort">数据</param>
  359. /// <param name="TemplateName">表名称</param>
  360. /// <param name="Color_list">颜色列</param>
  361. /// <returns></returns>
  362. private Bitmap Plot(DataTable dt_point_sort, string TemplateName, List<Color> Color_list)
  363. {
  364. int Line = 500;
  365. Bitmap map = new Bitmap(530, 530);
  366. Graphics g = Graphics.FromImage(map);
  367. SolidBrush sbrush_White = new SolidBrush(Color.White);
  368. g.FillRectangle(sbrush_White, 0, 0, 530, 530);
  369. Pen mypen = new Pen(Color.Black, 1);
  370. g.DrawImage(map, 0, 0, map.Width, map.Height);
  371. g.DrawLine(mypen, new Point(Line + 10, Line), new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))));//"\"
  372. g.DrawLine(mypen, new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))), new Point(0 + 10, Line));//"/"
  373. g.DrawLine(mypen, new Point(0 + 10, Line), new Point(Line + 10, Line));//"_"
  374. mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
  375. mypen.DashPattern = new float[] { 2, 2 };
  376. mypen = new Pen(Color.FromArgb(212, 212, 212), 1);
  377. int point = Line / 10;
  378. for (int i = 1; i < 10; i++)
  379. {
  380. //"\"
  381. int x1_a = point / 2 + ((point / 2) * (i - 1));
  382. int x2_a = Line - (43 * i);
  383. int y1_a = point * i;
  384. g.DrawLine(mypen, new Point(x1_a + 10, x2_a), new Point(y1_a + 10, Line));
  385. //"/"
  386. int x1_b = Line - (point / 2 + ((point / 2) * (i - 1)));
  387. int x2_b = Line - (43 * i);
  388. int y1_b = Line - (point * i);
  389. g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(y1_b + 10, Line));
  390. //"_"
  391. int x1_c = point / 2 + ((point / 2) * (i - 1));
  392. int x2_c = Line - (43 * i);
  393. int y1_c = Line - (point / 2 + ((point / 2) * (i - 1)));
  394. int y2_c = Line - (43 * i);
  395. g.DrawLine(mypen, new Point(x1_c + 10, x2_c), new Point(y1_c + 10, y2_c));
  396. g.DrawLine(mypen, new Point(y1_a + 10, Line + 10), new Point(y1_a + 10, Line));
  397. g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(x1_b + 10 + 10, x2_b));
  398. g.DrawLine(mypen, new Point(x1_c + 10 - 10, x2_c), new Point(y1_c + 10, y2_c));
  399. 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));
  400. g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(y1_b, Line + 8));
  401. 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));
  402. }
  403. mypen = new Pen(Color.Black, 1);
  404. TernaryDiagram ternaryDiagram = new TernaryDiagram();
  405. int order = 1;
  406. HashSet<int> hashSet = new HashSet<int>();
  407. for (int i = 0; i < dt_point_sort.Rows.Count; i++)
  408. {
  409. int pos = Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"]);
  410. if (pos > 3)
  411. {
  412. order = pos % 4;
  413. }
  414. else
  415. {
  416. order = pos;
  417. }
  418. PointF pointF = new PointF(Convert.ToInt32(dt_point_sort.Rows[i]["X"])+10, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]));
  419. ternaryDiagram.ShapeList(order, pointF, 10, Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])], g);
  420. //SolidBrush mysbrush = new SolidBrush(Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])]);
  421. //g.FillRectangle(mysbrush, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 8, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 4, 5);
  422. //g.DrawRectangle(mypen, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 7, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 5, 5);
  423. }
  424. Font myFont = new Font("Arial Unicode MS", 13, FontStyle.Bold);
  425. Font Font_features = new Font("Arial Unicode MS", 10, FontStyle.Bold);
  426. SolidBrush sbrush = new SolidBrush(Color.Black);
  427. g.DrawString("features:" + dt_point_sort.Rows.Count.ToString(), Font_features, sbrush, 400, 200);
  428. g.DrawString(TemplateName, myFont, sbrush, 265 - (TemplateName.Length * 10 / 2), 10);
  429. g.DrawString(TemplateName.Split('.')[0].ToString(), myFont, sbrush, 265 - (TemplateName.Split('.')[0].ToString().Length * 10 / 2), 45);//顶点名字
  430. g.DrawString(TemplateName.Split('.')[1].ToString(), myFont, sbrush, 0, 510);//左侧名字
  431. g.DrawString(TemplateName.Split('.')[2].ToString(), myFont, sbrush, 500 - (TemplateName.Split('.')[0].ToString().Length * 10), 510);//左侧名字
  432. #endregion
  433. return map;
  434. }
  435. public static Color FromHex(string hex)
  436. {
  437. hex = hex.Replace("#", ""); // 移除 # 前缀
  438. byte a = 255; // 默认Alpha值为255(不透明)
  439. byte r = Convert.ToByte(hex.Substring(0, 2), 16);
  440. byte g = Convert.ToByte(hex.Substring(2, 2), 16);
  441. byte b = Convert.ToByte(hex.Substring(4, 2), 16);
  442. return Color.FromArgb(a, r, g, b);
  443. }
  444. private DataTable GettriangleJsonClass(string path, string name, List<string> nameList, int sel)
  445. {
  446. DataTable data = new DataTable();
  447. data.Columns.Add("A");
  448. data.Columns.Add("B");
  449. data.Columns.Add("C");
  450. data.Columns.Add("DMAX");
  451. data.Columns.Add("DMIN");
  452. DataTable runData = new DataTable();
  453. runData.Columns.Add("X");
  454. runData.Columns.Add("Y");
  455. runData.Columns.Add("C");
  456. runData.Columns.Add("Color_position");
  457. List<string> list_string_sort = new List<string>();
  458. List<double[]> list_point_sort = new List<double[]>();
  459. DataTable particles = GetParticles(path, nameList, sel);
  460. foreach (DataRow item in particles.Rows)
  461. {
  462. if (item["particleLocation"].ToString() == "0,0,0")
  463. {
  464. continue;
  465. }
  466. list_string_sort.Add(item["sizeLevel"].ToString());
  467. double top = Convert.ToDouble(item["top"]);
  468. double left = Convert.ToDouble(item["left"]);
  469. double right = Convert.ToDouble(item["right"]);
  470. //x=right+top/2,y=(√3/2)*top
  471. double Y = 0.866 * top * 500;
  472. double X = (right + top / 2) * 500;
  473. double[] point = { Math.Round(X, 2), Math.Round(Y) };
  474. list_point_sort.Add(point);
  475. DataRow dr = data.NewRow();
  476. dr["A"] = top.ToString();
  477. dr["B"] = left.ToString();
  478. dr["C"] = right.ToString();
  479. dr["DMAX"] = item["DMAX"].ToString();
  480. dr["DMIN"] = item["DMIN"].ToString();
  481. data.Rows.Add(dr);
  482. DataRow runDR = runData.NewRow();
  483. runDR["X"] = Math.Round(X);
  484. runDR["Y"] = Math.Round(Y);
  485. runDR["Color_position"] = item["Color_position"].ToString();
  486. runData.Rows.Add(runDR);
  487. }
  488. TernaryData.Add(data);
  489. return runData;
  490. }
  491. /// <summary>
  492. /// 导出Excel数据
  493. /// </summary>
  494. private void ExportExcelDocument()
  495. {
  496. // 创建一个SaveFileDialog实例
  497. SaveFileDialog saveFileDialog = new SaveFileDialog();
  498. // 设置对话框的标题
  499. saveFileDialog.Title = "选择Excel文件保存位置";
  500. // 设置默认的文件扩展名为.xlsx
  501. saveFileDialog.DefaultExt = "xlsx";
  502. // 设置过滤器,限制用户可以选择的文件类型为Excel文件
  503. saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx";
  504. // 显示对话框并检查用户是否点击了“保存”按钮
  505. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  506. {
  507. // 获取用户选择的文件路径
  508. string filePath = saveFileDialog.FileName;
  509. // 调用导出方法
  510. ExportDataTableToExcel(TernaryData, filePath);
  511. }
  512. }
  513. //获取颗粒信息
  514. private DataTable GetParticles(string filepath, List<string> nameList,int sel)
  515. {
  516. ParticleData particledata = new ParticleData(filepath);
  517. //设置标签名称
  518. List<string> listName = new List<string>();
  519. //获取粒级表
  520. string sizestr = "";
  521. if (m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFile == "AUTO.psf")
  522. {
  523. double dn = 99999;
  524. string sou1 = m_condition.m_CurrentConditions[OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE].itemDisplayVal.ToString();
  525. if (sou1.Contains("+"))
  526. {
  527. for (int i = 0; i < sou1.Split('+').Length; i++)
  528. {
  529. ResultFile resultFile = m_ReportApp.m_rstDataMgr.ResultFilesList.Find(s => s.anotherFileName == sou1.Split('+')[i]);
  530. if (resultFile != null)
  531. {
  532. if (dn > resultFile.GetParticleMINECD())
  533. {
  534. dn = resultFile.GetParticleMINECD();
  535. }
  536. }
  537. }
  538. }
  539. else
  540. {
  541. ResultFile resultFile = m_ReportApp.m_rstDataMgr.CurResultFile;
  542. dn = resultFile.GetParticleMINECD();
  543. }
  544. sizestr = dn.ToString() + ",";
  545. for (double p = dn; p < 50; p = p * Math.Sqrt(2))
  546. {
  547. if (p > dn && p <= 50.5)
  548. {
  549. double dd = Math.Round(p);
  550. sizestr += dd.ToString() + ",";
  551. }
  552. }
  553. sizestr = sizestr.Remove(sizestr.Length - 1, 1);
  554. }
  555. else
  556. {
  557. string pathe = m_DataMgr.m_RptConfigFile.PartSizeFileFolder + m_DataMgr.m_RptConfigFile.PartSizeFile;
  558. DataSet ds = XMLoperate.GetXml(pathe);
  559. sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
  560. }
  561. for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
  562. {
  563. if (sizestr.Split(',')[i].Length > 0)
  564. {
  565. double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
  566. double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
  567. listName.Add(d1.ToString() + "~" + d2.ToString());
  568. }
  569. }
  570. double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
  571. listName.Add(d.ToString() + "~MAX");
  572. string filedAndParticl = "";
  573. string po = "";
  574. string con = "";
  575. int row = m_condition.m_conditionData.ConditionItemList.Count;
  576. if (condition != -1)
  577. {
  578. List<string> li = new List<string>() { "DMAX", "DMIN", "Area", "FERET" };
  579. con = li[condition];
  580. }
  581. else
  582. {
  583. if (row < 4)
  584. {
  585. con = "DMAX";
  586. }
  587. else
  588. {
  589. con = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName( OTS_REPORT_PROP_GRID_ITEMS.SIZE_CAL_METHOD_TYPE).ToString();
  590. }
  591. }
  592. switch (con)
  593. {
  594. case "DMAX":
  595. po = "DMAX";
  596. break;
  597. case "DMIN":
  598. po = "DMIN";
  599. break;
  600. case "ECD":
  601. po = "Area";
  602. break;
  603. case "FERET":
  604. po = "DFERET";
  605. break;
  606. }
  607. //particledata.GetParticleAllHaveXray("");
  608. //DataTable dtp = particledata.GetParticleHaveXray(filedAndParticl);//旧
  609. DataTable dtp = particledata.GetParticleAllHaveXray(filedAndParticl);//新
  610. //创建一个临时表
  611. DataTable ret_dt = new DataTable();
  612. //然后额外存放三列,用于存放拆分后三个顶点的值
  613. ret_dt.Columns.Add("sizeLevel");
  614. ret_dt.Columns.Add("particleLocation");
  615. ret_dt.Columns.Add("top");
  616. ret_dt.Columns.Add("left");
  617. ret_dt.Columns.Add("right");
  618. ret_dt.Columns.Add("Color_position");
  619. ret_dt.Columns.Add("DMAX");
  620. ret_dt.Columns.Add("DMIN");
  621. for (int k = 0; k < listName.Count; k++)
  622. {
  623. string str = listName[k];
  624. string d1 = str.Split('~')[0];
  625. string d2 = str.Split('~')[1];
  626. if (d2 == "MAX")
  627. {
  628. d2 = "999";
  629. }
  630. DataRow[] datas = dtp.Select(getWhere(d2, d1, po));
  631. foreach (var item in datas)
  632. {
  633. DataRow dr = ret_dt.NewRow();
  634. dr["sizeLevel"] = str;
  635. string element = item["Element"].ToString();
  636. string strRet = getStrRet(nameList, element);
  637. dr["particleLocation"] = strRet;
  638. dr["top"] = strRet.Split(',')[0];
  639. dr["left"] = strRet.Split(',')[1];
  640. dr["right"] = strRet.Split(',')[2];
  641. dr["Color_position"] = k.ToString();
  642. string strDMAX = item["DMAX"].ToString();
  643. string strDMIN = item["DMIN"].ToString();
  644. dr["DMAX"] = strDMAX;
  645. dr["DMIN"] = strDMIN;
  646. ret_dt.Rows.Add(dr);
  647. }
  648. }
  649. return ret_dt;
  650. }
  651. private string getWhere(string max, string min, string col)
  652. {
  653. if (col == "Area")
  654. {
  655. return col + ">=" + ((Convert.ToDouble(min) / 2) * (Convert.ToDouble(min) / 2) * Math.PI).ToString() + " and " + col + "<=" + ((Convert.ToDouble(max) / 2) * (Convert.ToDouble(max) / 2) * Math.PI).ToString();
  656. }
  657. else
  658. {
  659. return col + ">=" + min + " and " + col + "<=" + max; ;
  660. }
  661. }
  662. private string getStrRet(List<string> template, string element)
  663. {
  664. List<string> name = new List<string>()
  665. { "h","he",
  666. "li","be","b","c","n","o","f","ne",
  667. "na","mg","al","si","p","s","cl","ar",
  668. "k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr",
  669. "rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe",
  670. "cs","ba","la",
  671. "ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu",
  672. "hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn",
  673. "fr","ra","ac",
  674. "th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr"
  675. };
  676. List<double> value = new List<double>()
  677. { 1.008,4.003,
  678. 6.941,9.012,10.811,12.011,14.007,15.999,18.998,20.180,
  679. 22.990,24.305,26.982,28.086,30.974,32.066,35.453,39.948,
  680. 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,
  681. 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,
  682. 132.905,137.33,138.906,
  683. 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,
  684. 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,
  685. 223.00,226.025,227.028,
  686. 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
  687. };
  688. double d_ASum = 0;
  689. double d_BSum = 0;
  690. double d_CSum = 0;
  691. string aElements = template[0];
  692. string bElements = template[1];
  693. string cElements = template[2];
  694. foreach (var item in element.Split(';'))
  695. {
  696. if (item.Contains(aElements + "-"))
  697. {
  698. if (aElements != "")
  699. {
  700. d_ASum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(aElements.ToLower())];
  701. }
  702. }
  703. for (int i = 0; i < bElements.Split(',').Length; i++)
  704. {
  705. string e = bElements.Split(',')[i];
  706. if (item.Contains(e + "-"))
  707. {
  708. d_BSum = d_BSum + Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(e.ToLower())];
  709. }
  710. }
  711. if (item.Contains(cElements + "-"))
  712. {
  713. if (cElements != "")
  714. d_CSum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(cElements.ToLower())];
  715. }
  716. }
  717. double allNums = d_ASum + d_BSum + d_CSum;
  718. string strRet = "";
  719. if ((allNums) == 0)
  720. {
  721. strRet = "0,0,0";
  722. }
  723. else
  724. {
  725. double aPercent = 0, bPercent = 0, cPercent = 0;
  726. if (d_ASum != 0)
  727. {
  728. aPercent = d_ASum / allNums;
  729. }
  730. if (d_BSum != 0)
  731. {
  732. bPercent = d_BSum / allNums;
  733. }
  734. if (d_CSum != 0)
  735. {
  736. cPercent = d_CSum / allNums;
  737. }
  738. strRet = aPercent.ToString() + "," + bPercent.ToString() + "," + cPercent.ToString();
  739. }
  740. return strRet;
  741. }
  742. private void ExportDataTableToExcel(List<DataTable> dataTable, string filePath)
  743. {
  744. // 创建一个新的工作簿
  745. IWorkbook workbook = new XSSFWorkbook(); // 对于.xlsx格式
  746. // IWorkbook workbook = new HSSFWorkbook(); // 对于.xls格式
  747. for (int a=0;a< dataTable.Count;a++)
  748. {
  749. // 添加一个新的工作表
  750. ISheet sheet = workbook.CreateSheet("Sheet"+(a+1).ToString());
  751. // 创建表头行(第一行)
  752. IRow headerRow = sheet.CreateRow(0);
  753. ICell cell1 = headerRow.CreateCell(0);
  754. cell1.SetCellValue(TernaryName[0].ToString());
  755. ICell cell2 = headerRow.CreateCell(1);
  756. cell2.SetCellValue(TernaryName[1].ToString());
  757. ICell cell3 = headerRow.CreateCell(2);
  758. cell3.SetCellValue(TernaryName[2].ToString());
  759. ICell cell4 = headerRow.CreateCell(3);
  760. cell4.SetCellValue("DMAX");
  761. ICell cell5 = headerRow.CreateCell(4);
  762. cell5.SetCellValue("DMIN");
  763. // 填充数据行
  764. for (int i = 0; i < dataTable[a].Rows.Count; i++)
  765. {
  766. IRow row = sheet.CreateRow(i + 1);
  767. for (int j = 0; j < dataTable[a].Columns.Count; j++)
  768. {
  769. ICell cell = row.CreateCell(j);
  770. if (dataTable[a].Rows[i][j] != DBNull.Value)
  771. {
  772. cell.SetCellValue(dataTable[a].Rows[i][j].ToString());
  773. }
  774. }
  775. }
  776. // 自动调整列宽(可选)
  777. // 注意:NPOI的AutoSizeColumn方法不如EPPlus的AutoFitColumns方法那样精确
  778. // 你可能需要手动设置列宽或者接受默认的宽度
  779. // 但以下是一个尝试自动调整列宽的示例,它可能不完全符合你的期望
  780. for (int i = 0; i < dataTable[a].Columns.Count; i++)
  781. {
  782. sheet.AutoSizeColumn(i);
  783. // 或者,你可以设置一个固定的宽度(以字符为单位)
  784. // sheet.SetColumnWidth(i, 20 * 256); // 20是字符宽度,256是转换因子
  785. }
  786. }
  787. // 保存Excel文件
  788. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  789. {
  790. workbook.Write(fileStream);
  791. }
  792. // 释放资源(对于.xlsx格式,通常不需要显式调用Dispose,因为XSSFWorkbook实现了IDisposable但内部已经处理了资源释放)
  793. // 但为了良好的编程习惯,你可以显式调用(尽管这里不是必需的)
  794. // workbook.Dispose(); // 如果你使用的是实现了IDisposable的对象,并且想确保资源被释放
  795. }
  796. private void button1_Click_1(object sender, EventArgs e)
  797. {
  798. ExportExcelDocument();
  799. }
  800. }
  801. }