DEVChart_Trianglediagram.cs 38 KB

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