using OTSCommon.DBOperate.Model;
using OTSIncAReportApp.DataOperation.DataAccess;
using OTSIncAReportApp.OTSRstMgrFunction;
using OTSIncAReportApp.OTSSampleReportInfo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static OTSIncAReportApp.OTSReport_Export;
namespace OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration
{
///
/// 三元图
///
class TernaryDiagram
{
///
/// 三元图列表
///
List TemplateList;
frmReportConditionChoose m_condition;
public int condition = -1;
//绘制图例
public Bitmap DrawATernaryDiagramLegend(List Color_list, OTSReport_Export m_otsreport_export, string a_PartSizeFile)
{
Bitmap map = new Bitmap(260, 115);
Graphics g = Graphics.FromImage(map);
SolidBrush sbrush_White = new SolidBrush(Color.White);
SolidBrush sbrush = new SolidBrush(Color.Black);
g.FillRectangle(sbrush_White, 0, 0, 260, 115);
Font myFont = new Font("Arial", 10, FontStyle.Regular);
Font myFont2 = new Font("Arial", 10, FontStyle.Regular);
g.DrawString("(length,μm)", myFont, sbrush, 88, 3);
//legend(length,microns)
Pen mypen = new Pen(Color.Black, 1);
g.DrawLine(mypen, 70, 19, 170, 19);
//设置标签名称
List listName = new List();
//获取粒级表
string pathe = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFileFolder + a_PartSizeFile;
DataSet ds = XMLoperate.GetXml(pathe);
string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
{
if (sizestr.Split(',')[i].Length > 0)
{
double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
listName.Add(d1.ToString() + "~" + d2.ToString());
}
}
double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
listName.Add(d.ToString() + "~MAX");
int PositionJeight = 27;
//string str_unit = " μm";
string str_unit = "";
for (int i = 0; i < listName.Count; i = i + 3)
{
if (i < listName.Count)
{
SolidBrush sbrush_Color = new SolidBrush(Color_list[i]);
g.DrawRectangle(mypen, 0, PositionJeight, 8, 8);
g.FillRectangle(sbrush_Color, 1, PositionJeight + 1, 7, 7);
g.DrawString(listName[i].ToString() + str_unit, myFont2, sbrush, 12, PositionJeight - 2);
}
if (i + 1 < listName.Count)
{
SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 1]);
g.DrawRectangle(mypen, 94, PositionJeight, 8, 8);
g.FillRectangle(sbrush_Color, 95, PositionJeight + 1, 7, 7);
g.DrawString(listName[i + 1].ToString() + str_unit, myFont2, sbrush, 106, PositionJeight - 2);
}
if (i + 2 < listName.Count)
{
SolidBrush sbrush_Color = new SolidBrush(Color_list[i + 2]);
g.DrawRectangle(mypen, 188, PositionJeight, 8, 8);
g.FillRectangle(sbrush_Color, 189, PositionJeight + 1, 7, 7);
g.DrawString(listName[i + 2].ToString() + str_unit, myFont2, sbrush, 199, PositionJeight - 2);
}
PositionJeight = PositionJeight + 15;
}
return map;
}
public Bitmap DrawATernaryPicture(int selectindex, c_TemplateClass m_mbszclass, List Color_list, OTSReport_Export m_otsreport_export, string a_PartSizeFile)
{
initialization(m_otsreport_export);
ResultFile resultFile = m_otsreport_export.m_ReportApp.m_rstDataMgr.ResultFilesList[m_otsreport_export.m_ReportApp.m_rstDataMgr.GetWorkingResultId()];
string template = TemplateList[selectindex];
if (template == "")
{ //三元相图模板
template = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.TRIO_CHART_TYPE).ToString();
}
//获取粒级表
string pathtpfs = Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
List nameList = new List();
DataSet ds2 = XMLoperate.GetXmlData(pathtpfs, "XMLData");
DataTable dt = ds2.Tables["Member"];
string TemplateName = "";
//遍历第一层节点
foreach (DataRow element in dt.Rows)
{
TemplateName = element["TemplateName"].ToString();
if (TemplateName == template)
{
string Element = element["Element"].ToString();
nameList.Add(Element.Split('.')[0]);
nameList.Add(Element.Split('.')[1]);
nameList.Add(Element.Split('.')[2]);
break;
}
}
DataTable dt_point_sort = new DataTable();
dt_point_sort.Columns.Add("X", typeof(double));
dt_point_sort.Columns.Add("Y", typeof(double));
dt_point_sort.Columns.Add("Color_position");
DataTable particles = GetParticles(resultFile.FilePath, nameList, 0, m_otsreport_export, m_mbszclass);
foreach (DataRow item in particles.Rows)
{
if (item["particleLocation"].ToString() == "0,0,0")
{
continue;
}
double top = Convert.ToDouble(item["top"]);
double left = Convert.ToDouble(item["left"]);
double right = Convert.ToDouble(item["right"]);
//x=right+top/2,y=(√3/2)*top
double Y = 0.866 * top * 500;
double X = (right + top / 2) * 500;
double[] point_ = { Math.Round(X, 2), Math.Round(Y) };
DataRow dr = dt_point_sort.NewRow();
dr["X"] = Math.Round(X, 2);
dr["Y"] = Math.Round(Y);
dr["Color_position"] = item["Color_position"];
dt_point_sort.Rows.Add(dr);
}
#region 图形
int Line = 500;
Bitmap map = new Bitmap(530, 530);
Graphics g = Graphics.FromImage(map);
SolidBrush sbrush_White = new SolidBrush(Color.White);
g.FillRectangle(sbrush_White, 0, 0, 530, 530);
Pen mypen = new Pen(Color.Black, 1);
g.DrawImage(map, 0, 0, map.Width, map.Height);
g.DrawLine(mypen, new Point(Line + 10, Line), new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))));//"\"
g.DrawLine(mypen, new Point(Line / 2 + 10, Convert.ToInt32(Line - (Line / 2 * 1.732))), new Point(0 + 10, Line));//"/"
g.DrawLine(mypen, new Point(0 + 10, Line), new Point(Line + 10, Line));//"_"
mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
mypen.DashPattern = new float[] { 2, 2 };
mypen = new Pen(Color.FromArgb(212, 212, 212), 1);
int point = Line / 10;
for (int i = 1; i < 10; i++)
{
//"\"
int x1_a = point / 2 + ((point / 2) * (i - 1));
int x2_a = Line - (43 * i);
int y1_a = point * i;
g.DrawLine(mypen, new Point(x1_a + 10, x2_a), new Point(y1_a + 10, Line));
//"/"
int x1_b = Line - (point / 2 + ((point / 2) * (i - 1)));
int x2_b = Line - (43 * i);
int y1_b = Line - (point * i);
g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(y1_b + 10, Line));
//"_"
int x1_c = point / 2 + ((point / 2) * (i - 1));
int x2_c = Line - (43 * i);
int y1_c = Line - (point / 2 + ((point / 2) * (i - 1)));
int y2_c = Line - (43 * i);
g.DrawLine(mypen, new Point(x1_c + 10, x2_c), new Point(y1_c + 10, y2_c));
g.DrawLine(mypen, new Point(y1_a + 10, Line + 10), new Point(y1_a + 10, Line));
g.DrawLine(mypen, new Point(x1_b + 10, x2_b), new Point(x1_b + 10 + 10, x2_b));
g.DrawLine(mypen, new Point(x1_c + 10 - 10, x2_c), new Point(y1_c + 10, y2_c));
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));
g.DrawString((i * 10).ToString(), new Font("Arial Unicode MS", 8, FontStyle.Bold), new SolidBrush(Color.Black), new Point(y1_b, Line + 8));
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));
}
for (int i = 0; i < dt_point_sort.Rows.Count; i++)
{
mypen = new Pen(Color.Black, 1);
SolidBrush mysbrush = new SolidBrush(Color_list[Convert.ToInt32(dt_point_sort.Rows[i]["Color_position"])]);
g.FillRectangle(mysbrush, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 8, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 4, 5);
g.DrawRectangle(mypen, Convert.ToInt32(dt_point_sort.Rows[i]["X"]) + 7, 500 - Convert.ToInt32(dt_point_sort.Rows[i]["Y"]) - 2, 5, 5);
}
Font myFont = new Font("Arial Unicode MS", 13, FontStyle.Bold);
Font Font_features = new Font("Arial Unicode MS", 10, FontStyle.Bold);
SolidBrush sbrush = new SolidBrush(Color.Black);
g.DrawString("features:" + dt_point_sort.Rows.Count.ToString(), Font_features, sbrush, 400, 200);
g.DrawString(TemplateName, myFont, sbrush, 265 - (TemplateName.Length * 10 / 2), 10);
g.DrawString(TemplateName.Split('.')[0].ToString(), myFont, sbrush, 265 - (TemplateName.Split('.')[0].ToString().Length * 10 / 2), 45);//顶点名字
g.DrawString(TemplateName.Split('.')[1].ToString(), myFont, sbrush, 0, 510);//左侧名字
g.DrawString(TemplateName.Split('.')[2].ToString(), myFont, sbrush, 500 - (TemplateName.Split('.')[0].ToString().Length * 10), 510);//左侧名字
#endregion
return map;
}
private void initialization(OTSReport_Export m_otsreport_export)
{
//加载三元相图各项
//string pathtpf = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TrigTemplateFileFolder + /*"\\" +*/ a_PartSizeFile;// Application.StartupPath + "\\Config\\ProData\\DefaultTriTemplateFile.tpf";
string pathtpf = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TrigTemplateFileFolder + "\\" + m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.TriTempFile;
TemplateList = new List();
DataSet ds = DataOperation.DataAccess.XMLoperate.GetXmlData(pathtpf, "XMLData");
DataTable dt = ds.Tables["Member"];
foreach (DataRow item in dt.Rows)
{
if (item["TemplateName"].ToString() != "")
{
TemplateList.Add(item["TemplateName"].ToString());
}
}
m_condition = m_otsreport_export.m_ReportApp.m_conditionChoose;
}
private DataTable GetParticles(string filepath, List nameList, int sel, OTSReport_Export m_otsreport_export, c_TemplateClass m_mbszclass)
{
ParticleData particledata = new ParticleData(filepath);
//设置标签名称
List listName = new List();
//获取粒级表
string pathe = m_otsreport_export.m_ReportApp.m_rstDataMgr.m_RptConfigFile.PartSizeFileFolder + m_mbszclass.M_SYXT.str_cb_syxt_ljb.ToString();
DataSet ds = XMLoperate.GetXml(pathe);
string sizestr = ds.Tables[0].Rows[0]["Sizes"].ToString();
for (int i = 0; i < sizestr.Split(',').Length - 1; i++)
{
if (sizestr.Split(',')[i].Length > 0)
{
double d1 = Convert.ToDouble(sizestr.Split(',')[i]);
double d2 = Convert.ToDouble(sizestr.Split(',')[i + 1]);
listName.Add(d1.ToString() + "~" + d2.ToString());
}
}
double d = Convert.ToDouble(sizestr.Split(',')[sizestr.Split(',').Length - 1]);
listName.Add(d.ToString() + "~MAX");
string filedAndParticl = "";
string po = "";
string con = "";
int row = m_condition.m_conditionData.ConditionItemList.Count;
if (condition != -1)
{
List li = new List() { "DMAX", "DMIN", "Area", "FERET" };
con = li[condition];
}
else
{
if (row < 4)
{
con = "DMAX";
}
else
{
con = m_condition.m_conditionData.GetPropItemDisplayValueByPropItemName(OTS_REPORT_PROP_GRID_ITEMS.SIZE_CAL_METHOD_TYPE).ToString();
}
}
switch (con)
{
case "DMAX":
po = "DMAX";
break;
case "DMIN":
po = "DMIN";
break;
case "ECD":
po = "Area";
break;
case "FERET":
po = "DFERET";
break;
}
DataTable dtp = particledata.GetParticleAllHaveXray(filedAndParticl);
for (int i = 0; i < dtp.Rows.Count; i++)
{
if (Convert.ToInt32(dtp.Rows[i]["TypeId"]) < 0 || dtp.Rows[i]["TypeName"].ToString() == "Invalid")
{
dtp.Rows[i].Delete();
}
}
dtp.AcceptChanges();
//创建一个临时表
DataTable ret_dt = new DataTable();
//然后额外存放三列,用于存放拆分后三个顶点的值
ret_dt.Columns.Add("sizeLevel");
ret_dt.Columns.Add("particleLocation");
ret_dt.Columns.Add("top");
ret_dt.Columns.Add("left");
ret_dt.Columns.Add("right");
ret_dt.Columns.Add("Color_position");
for (int k = 0; k < listName.Count; k++)
{
string str = listName[k];
string d1 = str.Split('~')[0];
string d2 = str.Split('~')[1];
if (d2 == "MAX")
{
d2 = "999";
}
DataRow[] datas = dtp.Select(getWhere(d2, d1, po));
foreach (var item in datas)
{
DataRow dr = ret_dt.NewRow();
dr["sizeLevel"] = str;
string element = item["Element"].ToString();
string strRet = getStrRet(nameList, element);
dr["particleLocation"] = strRet;
dr["top"] = strRet.Split(',')[0];
dr["left"] = strRet.Split(',')[1];
dr["right"] = strRet.Split(',')[2];
dr["Color_position"] = k.ToString();
ret_dt.Rows.Add(dr);
}
}
return ret_dt;
}
private string getWhere(string max, string min, string col)
{
return col + ">=" + min + " and " + col + "<=" + max;
}
private string getStrRet(List template, string element)
{
List name = new List()
{ "h","he",
"li","be","b","c","n","o","f","ne",
"na","mg","al","si","p","s","cl","ar",
"k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as","se","br","kr",
"rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe",
"cs","ba","la",
"ce","pr","nd","pm","sm","eu","gd","tb","dy","ho","er","tm","yb","lu",
"hf","ta","w","re","os","ir","pt","au","hg","tl","pb","bi","po","at","rn",
"fr","ra","ac",
"th","pa","u","np","pu","am","cm","bk","cf","es","fm","md","no","lr"
};
List value = new List()
{ 1.008,4.003,
6.941,9.012,10.811,12.011,14.007,15.999,18.998,20.180,
22.990,24.305,26.982,28.086,30.974,32.066,35.453,39.948,
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,
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,
132.905,137.33,138.906,
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,
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,
223.00,226.025,227.028,
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
};
double d_ASum = 0;
double d_BSum = 0;
double d_CSum = 0;
string aElements = template[0];
string bElements = template[1];
string cElements = template[2];
foreach (var item in element.Split(';'))
{
if (item.Contains(aElements + "-"))
{
if (aElements != "")
{
d_ASum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(aElements.ToLower())];
}
}
for (int i = 0; i < bElements.Split(',').Length; i++)
{
string e = bElements.Split(',')[i];
if (item.Contains(e + "-"))
{
d_BSum = d_BSum + Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(e.ToLower())];
}
}
if (item.Contains(cElements + "-"))
{
if (cElements != "")
d_CSum = Convert.ToDouble(item.Split('-')[1]) / value[name.IndexOf(cElements.ToLower())];
}
}
double allNums = d_ASum + d_BSum + d_CSum;
string strRet = "";
if ((allNums) == 0)
{
strRet = "0,0,0";
}
else
{
double aPercent = 0, bPercent = 0, cPercent = 0;
if (d_ASum != 0)
{
aPercent = d_ASum / allNums;
}
if (d_BSum != 0)
{
bPercent = d_BSum / allNums;
}
if (d_CSum != 0)
{
cPercent = d_CSum / allNums;
}
strRet = aPercent.ToString() + "," + bPercent.ToString() + "," + cPercent.ToString();
}
return strRet;
}
///
/// 三角形绘制
///
/// 中心点位置
/// 边长
/// 颜色
/// Graphics
private void Triangle(PointF center, float sideLength, Color Blue, Graphics g)
{
Pen pen = new Pen(Color.Black, 2);
// 计算正三角形的高度(从中心到底边的垂直距离)
float height = (float)(Math.Sqrt(3) / 2 * sideLength);
// 计算三个顶点的坐标
float cenrerX = center.X;
float cenrerY = center.Y;
PointF topVertex = new PointF(cenrerX, cenrerY - height / 2);
PointF leftVertex = new PointF(cenrerX - sideLength / 2, cenrerY + height / 2 * (float)Math.Sqrt(3) / 3);
PointF rightVertex = new PointF(cenrerX + sideLength / 2, cenrerY + height / 2 * (float)Math.Sqrt(3) / 3);
// 为了演示,这里我们仍然使用上面计算得到的顶点
PointF[] vertices = { topVertex, leftVertex, rightVertex };
g.DrawPolygon(pen, vertices);
// 创建一个Brush对象来定义填充颜色
Brush brush = new SolidBrush(Blue); // 例如,填充颜色为蓝色
// 使用Graphics.FillPolygon方法填充正三角形
g.FillPolygon(brush, vertices);
}
///
/// 矩形
///
/// 中心点位置
/// 边长
/// 颜色
/// Graphics
private void Rectangle(PointF center, float sideLength, Color Blue, Graphics g)
{
PointF pointF = new PointF(center.X - (sideLength/2), center.Y - (sideLength / 2));
Brush brush = new SolidBrush(Blue);
Pen pen = new Pen(Color.Black, 2);
g.DrawRectangle(pen, pointF.X, pointF.Y, sideLength, sideLength);
g.FillRectangle(brush, pointF.X, pointF.Y, sideLength, sideLength);
}
///
/// 圆形
///
/// 中心点位置
/// 边长
/// 颜色
/// Graphics
private void circle(PointF center, float sideLength, Color Blue, Graphics g)
{
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(Blue); // 例如,填充颜色为蓝色
PointF pointF = new PointF(center.X - (sideLength / 2), center.Y - (sideLength / 2));
g.DrawEllipse(pen, pointF.X , pointF.Y , sideLength , sideLength);
g.FillEllipse(brush, pointF.X, pointF.Y, sideLength, sideLength);
}
///
/// 菱形
///
/// 中心点位置
/// 边长
/// 颜色
/// Graphics
private void rhombus(PointF center, float sideLength, Color Blue, Graphics g)
{
// 保存当前的Graphics状态,以便稍后恢复
GraphicsState state = g.Save();
// 将坐标系移动到旋转的中心点,并旋转45度
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(45);
// 在旋转后的坐标系中绘制正方形
Rectangle rect = new Rectangle((int)(-sideLength / 2), (int)(-sideLength / 2), (int)sideLength, (int)sideLength);
Pen pen = new Pen(Color.Black, 2);
g.DrawRectangle(pen, rect);
Brush brush = new SolidBrush(Blue);
g.FillRectangle(brush, rect);
// 恢复原始的Graphics状态
g.Restore(state);
}
public void ShapeList(int order, PointF center, float sideLength, Color Blue, Graphics g)
{
if (order ==0)
{
//三角
Triangle(center, sideLength+2, Blue, g);
}
else if(order==1)
{
//矩形
Rectangle(center, sideLength, Blue, g);
}
else if (order==2)
{
//圆形
circle(center, sideLength, Blue, g);
}
else if (order ==3)
{
//菱形
rhombus(center, sideLength, Blue, g);
}
}
}
}