DEVChart_Trianglediagram.cs 35 KB

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