using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using CoreFS.CA06; using Core.Mes.Client.Comm.Server; using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Tool; using Infragistics.Win.UltraWinGrid; using Infragistics.Win; using Infragistics.Win.UltraWinDataSource; namespace Core.StlMes.Client.BuyBillet { public partial class frmDDTJChart : FrmBase { private int angle = 0; private int angle1 = 0; private int colorsIndex = 0; private DataTable dtNew; //ctrl shift 键被按下 private bool ctrlKeyDown; private bool shiftKeyDown; //自定义图颜色 private Color[] chartColors; //当前活动 chart private Chart chartActive; public frmDDTJChart() { InitializeComponent(); } private void Initial_ChartColors() { chartColors = new Color[] { Color.FromArgb(0,112,192), //蓝色 预留给作业时间 Color.FromArgb(255,0,0), //橙红 预留给作业率 Color.FromArgb(134,164,74),//橄榄色 预留给 停机时间 Color.FromArgb(227,160,93), Color.FromArgb(178,90,126), Color.FromArgb(113,175,164), Color.FromArgb(3,54,73), Color.FromArgb(89,69,61), Color.FromArgb(1,77,103), Color.FromArgb(91,74,66), Color.FromArgb(90,13,67), Color.FromArgb(87,96,105), Color.FromArgb(147,224,255), Color.FromArgb(250,227,113), Color.FromArgb(107,194,53), Color.FromArgb(205,164,158), Color.FromArgb(23,44,60), Color.FromArgb(87,105,60), Color.FromArgb(28,120,135), Color.FromArgb(131,175,155), Color.FromArgb(192,0,0), //深红 预留给作业率 Color.FromArgb(247,150,70),//橙色 Color.FromArgb(165,151,185),//淡紫 Color.FromArgb(206,142,141),//玫瑰红 Color.FromArgb(142,165,203),//冰蓝 Color.FromArgb(181,202,146),//浅绿 Color.FromArgb(112,48,160),//紫色 Color.FromArgb(61,150,174) //水绿色 }; } private void Hide_Dept() { foreach (var row in ucb1.DisplayLayout.Rows) { if (row.Cells["plinecode"].Value.ToString() == "-1") { row.Hidden = true; break; } } } private void frmDDTJChart_Load(object sender, EventArgs e) { initial_UltraDataSource1(); this.ctrlKeyDown = false; this.shiftKeyDown = false; Initial_ChartColors(); //chart 绑定鼠标滚轮缩放事件 Chart1.MouseWheel += Chart_MouseWheel; Chart2.MouseWheel += Chart_MouseWheel; Chart3.MouseWheel += Chart_MouseWheel; //chart1、2、3 鼠标进入时事件注册 Chart1.MouseEnter += Chart_MouseEnter; Chart2.MouseEnter += Chart_MouseEnter; Chart3.MouseEnter += Chart_MouseEnter; //this.KeyDown += frmDDTJChart_KeyDown; //this.KeyUp += frmDDTJChart_KeyUp; this.panel1.MouseWheel += panel1_MouseWheel; //Chart1、2、3 注册 键盘事件 Chart1.KeyUp +=Chart_KeyUp; Chart1.KeyDown +=Chart_KeyDown; Chart2.KeyUp += Chart_KeyUp; Chart2.KeyDown += Chart_KeyDown; Chart3.KeyUp += Chart_KeyUp; Chart3.KeyDown += Chart_KeyDown; Hide_Dept(); //隐藏图控件,当有数据时显示 Chart_Hide_Show(false); this.startDate.Value = this.endDate.Value.AddMonths(-1); //ucb1.DisplayLayout.Bands[0].Columns["dept"].Width = 135; //ucb1.DisplayLayout.Bands[0].Columns["plinename1"].Width = 50; //ucb1.DisplayLayout.Bands[0].Columns["plinename2"].Width = 70; ucb1.DisplayLayout.Bands[0].Columns["dept"].Width = 200; ucb1.DisplayLayout.Bands[0].Columns["plinename1"].Width = 100; ucb1.DisplayLayout.Bands[0].Columns["plinename2"].Width = 100; ucb1.DisplayLayout.Bands[0].Columns["dept"].CellAppearance.TextHAlign = HAlign.Center; ucb1.DisplayLayout.Bands[0].Columns["plinename1"].CellAppearance.TextHAlign = HAlign.Center; ucb1.DisplayLayout.Bands[0].Columns["plinename2"].CellAppearance.TextHAlign = HAlign.Center; ucb1.DisplayLayout.Bands[0].Columns["dept"].CellAppearance.TextVAlign = VAlign.Middle; ucb1.DisplayLayout.Bands[0].Columns["plinename1"].CellAppearance.TextVAlign = VAlign.Middle; ucb1.DisplayLayout.Bands[0].Columns["plinename2"].CellAppearance.TextVAlign = VAlign.Middle; ucb1.DisplayLayout.Bands[0].Columns["dept"].CellAppearance.BackColor = Color.Wheat; ucb1.DisplayLayout.Bands[0].Columns["plinename1"].CellAppearance.BackColor = Color.YellowGreen; ucb1.DisplayLayout.Bands[0].Columns["plinename2"].CellAppearance.BackColor = Color.SkyBlue; ucb1.Width = 273; //以下设置图控件初始大小 this.Chart1.Width = Convert.ToInt32(Math.Ceiling(this.Width * 0.8))>900?Convert.ToInt32(Math.Ceiling(this.Width * 0.8)):900; this.Chart2.Width = this.Width - this.Chart1.Width > 900 ? this.Width - this.Chart1.Width : 900; this.Chart3.Width = this.Chart2.Width-200; UltraGridBand band = this.ucb1.DisplayLayout.Bands[0]; foreach (UltraGridRow row in band.GetRowEnumerator(GridRowType.DataRow)) { if (row.Cells["dept"].Value.ToString().Contains("全部")) { row.Cells["dept"].Appearance.FontData.Bold = DefaultableBoolean.True; } if (row.Cells["plinename2"].Value.ToString().Contains("全部")) { row.Cells["plinename2"].Appearance.FontData.Bold = DefaultableBoolean.True; } } } void panel1_MouseWheel(object sender, MouseEventArgs e) { HandledMouseEventArgs h = e as HandledMouseEventArgs; if (h != null) { h.Handled = false; } } void frmDDTJChart_KeyUp(object sender, KeyEventArgs e) { this.ctrlKeyDown = e.Control; this.shiftKeyDown = e.Shift; } void frmDDTJChart_KeyDown(object sender, KeyEventArgs e) { this.ctrlKeyDown = e.Control; this.shiftKeyDown = e.Shift; } void Chart_MouseWheel(object sender, MouseEventArgs e) { //var chart = (Chart)sender; //var xAxis = chart.ChartAreas[0].AxisX; //var yAxis = chart.ChartAreas[0].AxisY; //try //{ // if (e.Delta < 0) // Scrolled down. // { // //xAxis.ScaleView.ZoomReset(); // //yAxis.ScaleView.ZoomReset(); // Chart1.Width -= 20; // } // else if (e.Delta > 0) // Scrolled up. // { // //var xMin = xAxis.ScaleView.ViewMinimum; // //var xMax = xAxis.ScaleView.ViewMaximum; // //var yMin = yAxis.ScaleView.ViewMinimum; // //var yMax = yAxis.ScaleView.ViewMaximum; // //var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4; // //var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4; // //var posYStart = yAxis.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4; // //var posYFinish = yAxis.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4; // //xAxis.ScaleView.Zoom(posXStart, posXFinish); // //yAxis.ScaleView.Zoom(posYStart, posYFinish); // Chart1.Width += 20; // } //} //catch { } Chart chart = sender as Chart; HandledMouseEventArgs h = e as HandledMouseEventArgs; if (h != null) { h.Handled = true; } bool IsGoUp = e.Delta > 0 ? true : false; if (this.ctrlKeyDown) { if (chart != null) { if (IsGoUp) { chartActive.Width += 20; } if (!IsGoUp) { chartActive.Width -= 20; } } } else if (this.shiftKeyDown) { //int hStep = (int)(this.pictureBox2.Image.Width * 0.02); //int vStep = (int)(this.pictureBox2.Image.Height * 0.02); if (IsGoUp) { //this.pictureBox2.Width += hStep; //this.pictureBox2.Height += vStep; } else { //this.pictureBox2.Width -= hStep; //this.pictureBox2.Height -= vStep; } } else { //if (IsGoUp && this.panel1.VerticalScroll.Value > 5) //{ // this.panel1.VerticalScroll.Value -= 5; //} //if (!IsGoUp && this.panel1.VerticalScroll.Value < this.panel1.VerticalScroll.Maximum - 5) //{ // this.panel1.VerticalScroll.Value += 5; //} } } //显示或隐藏图控件 private void Chart_Hide_Show(bool flag) { if (flag) { this.Chart1.Show(); this.Chart2.Show(); this.Chart3.Show(); } else { this.Chart1.Hide(); this.Chart2.Hide(); this.Chart3.Hide(); } } public override void ToolBar_Click(object sender, string ToolbarKey) { base.ToolBar_Click(sender, ToolbarKey); switch (ToolbarKey) { case "Query": Query(); break; case "Close": this.Close(); break; } } private void Query() { string plinecode = ucb1.Value==null?string.Empty:ucb1.Value.ToString(); if(string.IsNullOrEmpty(plinecode)){ MessageUtil.ShowTips("请选择机组产线!"); return; } int flag = DateTime.Compare(this.startDate.Value, this.endDate.Value); string startDate = this.startDate.Value.ToString("yyyyMMdd"); string endDate = this.endDate.Value.ToString("yyyyMMdd"); if (Convert.ToInt32(startDate) > Convert.ToInt32(endDate)) { MessageUtil.ShowWarning("起始日期不能大于截止日期!"); return; } int days = DateDiff(this.startDate.Value.AddDays(-1), this.endDate.Value); startDate = this.startDate.Value.AddDays(-1).ToString("yyyyMMdd"); DataTable dt = ServerHelper.GetData("com.steering.pss.buybillet.Buybillet.get_FrmddTJChart_data", new object[] { startDate, endDate, days.ToString(), plinecode }, this.ob); //dt.Columns["optime"].DataType = Type.GetType("System.Decimal"); dtNew = dt.Clone();  //复制baidt表数据结构 dtNew.Columns["optime"].DataType = Type.GetType("System.Decimal"); for (int i = 0; i < dt.Rows.Count; i++) { dtNew.Rows.Add(dt.Rows[i].ItemArray);  //添加数du据行 } if (dtNew != null && dtNew.Rows.Count > 0) { Build_Chart(dtNew); Build_ChartPie1(dtNew); Build_ChartPie2(dtNew); Chart_Hide_Show(true); index = 0; colorsIndex = 3; timer1.Enabled = false; //Chart1.Width = 1000; //Chart1.Width = 800; } else { MessageUtil.ShowTips("未查询到相关数据!"); } } private void Chart_Initial(Chart chart) { if (chart.Series.Count > 0) { chart.Series.Clear(); } if (chart.Titles.Count > 0) { chart.Titles.Clear(); } //以下是图例属性的设置 chart.Legends[0].Enabled = true; //是否显示图例 //Chart1.Legends[0].Position.Auto = true; chart.Legends[0].Docking = Docking.Bottom; //Docking是图例的停靠位置 chart.Legends[0].Alignment = StringAlignment.Center;//Alignment属性是图例的对齐方式 //设置控件背景、图区域、图例的背景色为透明 同产销Form一致 chart.BackColor = Color.Transparent; chart.ChartAreas[0].BackColor = Color.Transparent; chart.Legends[0].BackColor = Color.Transparent; //if (chart.Name == "Chart1") //{ // chart.Width = 800; //} //if (chart.Name == "Chart2") //{ // chart.Width = 600; //} } //以下代码生成左侧柱状曲线图 private void Build_Chart(DataTable dt) { //初始化图控件,设置公共属性 Chart_Initial(Chart1); //if (Chart1.Series.Count > 0) //{ // Chart1.Series.Clear(); //} //if (Chart1.Titles.Count > 0) //{ // Chart1.Titles.Clear(); //} //Get the DISTINCT Countries. List depts = (from p in dt.AsEnumerable() select p.Field("dept")).Distinct().ToList(); //Remove the Default Series. //if (Chart1.Series.Count() == 1) //{ // Chart1.Series.Remove(Chart1.Series[0]); //} //Loop through the Countries. foreach (string dept in depts) { //Get the Year for each Country. DateTime[] x = (from p in dt.AsEnumerable() where p.Field("dept") == dept orderby p.Field("opdate") ascending select p.Field("opdate")).ToArray(); //Get the Total of Orders for each Country. decimal[] y = (from p in dt.AsEnumerable() where p.Field("dept") == dept orderby p.Field("opdate") ascending select p.Field("optime")).ToArray(); //Add Series to the Chart. Chart1.Series.Add(new Series(dept)); Chart1.Series[dept].IsValueShownAsLabel = true; Chart1.Series[dept].ChartType = SeriesChartType.StackedColumn; Chart1.Series[dept].Points.DataBindXY(x, y); for (int i = 0; i < y.Length; i++) { if (y[i] == 0) { Chart1.Series[dept].Points[i].IsValueShownAsLabel = false; } Chart1.Series[dept].Points[i].IsValueShownAsLabel = false; } } foreach (string dept in depts) { if (dept != "作业时间") continue; //Get the Year for each Country. DateTime[] x = (from p in dt.AsEnumerable() where p.Field("dept") == dept orderby p.Field("opdate") ascending select p.Field("opdate")).ToArray(); //Get the Total of Orders for each Country. decimal[] y = (from p in dt.AsEnumerable() where p.Field("dept") == dept orderby p.Field("opdate") ascending select p.Field("optime")).ToArray(); //Add Series to the Chart. string str = "作业率"; Chart1.Series.Add(new Series(str)); Chart1.Series[str].IsValueShownAsLabel = true; Chart1.Series[str].ChartType = SeriesChartType.Line; Chart1.Series[str].Points.DataBindXY(x, y); for (int i = 0; i < y.Length; i++) { Chart1.Series[str].Points[i].IsValueShownAsLabel = false; //Chart1.Series[str].Points[i].Color = chartColors[1]; } } Chart1.Series[0].YAxisType = AxisType.Primary; //Chart1.Series[5].YAxisType = AxisType.Secondary; Chart1.Series["作业率"].MarkerStyle = MarkerStyle.Circle; Chart1.Series["作业率"].Color = Color.Red; Chart1.Series["作业率"].MarkerSize = 9; Chart1.Series["作业率"].MarkerBorderWidth = 9; Chart1.Series["作业率"].BorderWidth = 3; Chart1.Series["作业率"].IsValueShownAsLabel = false; //Chart1.Series["作业率"].SetCustomProperty("LineTension", "0.05"); //foreach (string country in countries) //{ // if (country != "Brazil") // continue; // //Get the Year for each Country. // int[] x = (from p in dt.AsEnumerable() // where p.Field("ShipCountry") == country // orderby p.Field("Year") ascending // select p.Field("Year")).ToArray(); // //Get the Total of Orders for each Country. // //int[] y = (from p in dt.AsEnumerable() // // where p.Field("ShipCountry") == country // // orderby p.Field("Year") ascending // // select p.Field("Total")).ToArray(); // double[] y = new double[] { 0.25, 0.45, 0.79 }; // //Add Series to the Chart. // string str = country + "test"; // Chart1.Series.Add(new Series(str)); // //Chart1.Series[str].IsValueShownAsLabel = true; // Chart1.Series[str].ChartType = SeriesChartType.Line; // Chart1.Series[str].Points.DataBindXY(x, y); //} Chart1.Series[0].YAxisType = AxisType.Primary; //Chart1.Series[3].YAxisType = AxisType.Secondary; //Chart1.Series[3].MarkerStyle = MarkerStyle.Diamond; //Chart1.Series[3].MarkerSize = 35; //Chart1.Series[3].MarkerBorderWidth = 35; //Chart1.ChartAreas[0].AxisY2.LineColor = System.Color.Transparent; //Chart1.ChartAreas[0].AxisY2.MajorGrid.Enabled = false; //Chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True; //Chart1.ChartAreas[0].AxisY2.IsStartedFromZero = Chart1.ChartAreas[0].AxisY.IsStartedFromZero; //Chart1.ChartAreas[0].AxisY2.Minimum = 0; //Chart1.ChartAreas[0].AxisY2.Minimum = 1; ////Chart1.ChartAreas[0].AxisY2.Interval = 0.1; //Chart1.ChartAreas[0].AxisY2.LabelStyle.Format = "0%"; //Chart1.Series[3].Label = "#VAL{P}"; Chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//不显示网格线 Chart1.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;//不显示网格线 Chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True; Chart1.ChartAreas[0].AxisY2.LabelStyle.Format = "0%";//格式化,为了显示百分号 Chart1.ChartAreas[0].AxisY2.Interval = 0.1;//设置刻度间隔为5% Chart1.ChartAreas[0].AxisY2.Maximum = 1; //Chart1.Series[3].Label = "#VAL{P}"; Chart1.ChartAreas[0].Area3DStyle.Enable3D = false;//设置3D效果 //chart2.ChartAreas[0].Area3DStyle.PointDepth = Chart1.ChartAreas[0].Area3DStyle.PointGapDepth = 50;//设置一下深度,看起来舒服点…… Chart1.ChartAreas[0].Area3DStyle.WallWidth = 0;//设置墙的宽度为0; ////以下是图例属性的设置 //Chart1.Legends[0].Enabled = true; //是否显示图例 ////Chart1.Legends[0].Position.Auto = true; //Chart1.Legends[0].Docking = Docking.Bottom; //Docking是图例的停靠位置 //Chart1.Legends[0].Alignment = StringAlignment.Center;//Alignment属性是图例的对齐方式 //Chart1.Series[0]["PointWidth"] = "1"; //Chart1.Series[1]["PointWidth"] = "1"; //Chart1.Series[2]["PointWidth"] = "1"; //Chart1.Series[3]["PointWidth"] = "1"; //Chart1.Series[4]["PointWidth"] = "1"; //Chart1.Series[0].Label = "#PERCENT -- #VALY"; Chart1.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None; //设置当前X轴Label自动设置格式=关闭 Chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//设置当前X轴Label的显示间隔周期=1 (1个项目) (=2的意思: 间隔2个项目显示...以此类推) Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false;//设置当前X轴Label的双行显示格式= 打开. (双行显示的效果如下图) Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -60;//x轴倾斜的角度。 Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "MM-dd";//x轴Label 格式化。 Chart1.ChartAreas[0].AxisY.Maximum = 24; Chart1.ChartAreas[0].AxisY.Interval = 3;//设置刻度间隔为3 Chart1.ChartAreas[0].BackColor = Color.Transparent; Chart1.Legends[0].BackColor = Color.Transparent; //Chart1.ChartAreas[0].AxisY.Title = "生产厂停机"; //Chart1.ChartAreas[0].AxisY.TitleFont = new Font("Arial Unicode MS", float.Parse("10"),FontStyle.Regular); //Chart1.ChartAreas[0].AxisY.TitleForeColor = Color.FromName("Black"); //Chart1.ChartAreas[0].AxisY.Title = "简体中文"; //Chart1.ChartAreas[0].AxisY.IsLabelAutoFit = true; //Chart1.ChartAreas[0].AxisY2.IsLabelAutoFit = true; //以下设置Chart四周空白面积 Chart1.ChartAreas[0].Position.X = 0; Chart1.ChartAreas[0].Position.Width = 100; Chart1.ChartAreas[0].Position.Height = 75; Chart1.ChartAreas[0].Position.Y = 6; //Chart1.ChartAreas[0].AxisX.IsMarginVisible = true; //Chart1.ChartAreas[0].AxisX.IntervalOffset = 1; //Chart1.ChartAreas[0].AxisX.Minimum = Double.NaN; //Chart1.ChartAreas[0].AxisX.Maximum = Double.NaN; //Chart1.ChartAreas[0].AxisX.IsMarginVisible = false; //Chart1.ChartAreas[0].AxisX.IsMarginVisible = false; //Chart1.ChartAreas[0].AxisY.IsMarginVisible = false; //Chart1.ChartAreas[0].AxisY2.IsMarginVisible = false; //Chart1.ChartAreas[0].InnerPlotPosition.Y = 100; //Chart1.ChartAreas[0].AxisX.LabelStyle.IntervalOffset = -1; //Chart1.ChartAreas[0].AxisX.MajorGrid.IntervalOffset = 0.5; //Chart1.ChartAreas[0].AxisX.MajorTickMark.IntervalOffset = 0.5; string deptName = ucb1.Text; this.Chart1.Titles.Add(new Title(deptName + "运行停机图", Docking.Top, new Font("Arial Unicode MS", float.Parse("15"), FontStyle.Regular), Color.FromName("Black"))); //设置柱状图宽度 foreach (var series in Chart1.Series) { series["PixelPointWidth"] = "10"; //Chart1.Series[0]["PixelPointWidth"] = "15"; } //设置chart 颜色 //for (int i = 0; i < Chart1.Series[0].Points.Count; i++) //{ // DataPoint p = Chart1.Series[0].Points[i]; // if (p.AxisLabel == "作业时间") // { // p.Color = chartColors[0]; // continue; // } // if (colorsIndex > chartColors.Length - 1) // { // break; // } // else // { // p.Color = chartColors[colorsIndex++]; // } //} //设置chart column 颜色 //for (int i = 0; i < Chart1.Series.Count; i++) //{ // if (Chart1.Series[i].Name == "作业时间") // { // //foreach (DataPoint p in Chart1.Series[i].Points) // //{ // // p.Color = chartColors[0]; // //} // Chart1.Series[i].Color = chartColors[0]; // } // else if (Chart1.Series[i].Name == "作业率") // { // //foreach (DataPoint p in Chart1.Series[i].Points) // //{ // // p.Color = chartColors[0]; // //} // Chart1.Series[i].Color = chartColors[1]; // } // else // { // if (colorsIndex > chartColors.Length - 1) // { // break; // } // else // { // //foreach (DataPoint p in Chart1.Series[i].Points) // //{ // // p.Color = chartColors[colorsIndex]; // //} // Chart1.Series[i].Color = chartColors[colorsIndex]; // colorsIndex++; // } // } //} } //以下代码生成右侧饼图(Pie) private void Build_ChartPie(DataTable dt) { //初始化图控件,设置公共属性 Chart_Initial(Chart2); var query = from r in dt.AsEnumerable() group r by r.Field("DEPT") into s select new { DEPT = s.Select(m => m.Field("DEPT")).First(), OPTIME = s.Sum(p => p.Field("OPTIME")) }; //Get the names of Cities. string[] x = query.Select(p => p.DEPT).ToArray(); //Get the Total of Orders for each City. decimal[] y = query.Select(p => p.OPTIME).ToArray(); Chart2.Series.Add(new Series()); Chart2.Series[0].ChartType = SeriesChartType.Pie; Chart2.Series[0].Points.DataBindXY(x, y); //foreach (DataPoint p in Chart2.Series[0].Points) //{ // p.Label = x[0] + " #PERCENT{P2}"; //} //Chart2.Series[0]["PieLabelStyle"] = "Outside"; //Chart2.Legends[0].Enabled = true; Chart2.Series[0]["PieLabelStyle"] = "Outside"; for (int i=0;i< Chart2.Series[0].Points.Count;i++) { DataPoint p = Chart2.Series[0].Points[i]; p.Label = x[i] + " #PERCENT{P2}"; p.LegendText = x[i]; } Chart2.ChartAreas[0].Area3DStyle.Enable3D = true; string deptName = ucb1.Text; Chart2.Titles.Add(new Title(deptName + "运行停机比", Docking.Top, new Font("Arial Unicode MS", float.Parse("15"), FontStyle.Regular), Color.FromName("Black"))); //Chart2.Legends[0].Docking = Docking.Bottom; //Docking是图例的停靠位置 //Chart2.Legends[0].Alignment = StringAlignment.Center;//Alignment属性是图例的对齐方式 //Chart2.BackColor = Color.Transparent; //Chart2.ChartAreas[0].BackColor = Color.Transparent; //Chart2.Legends[0].BackColor = Color.Transparent; foreach (DataPoint point in Chart2.Series[0].Points) { point["Exploded"] = "false"; if (point.AxisLabel == "作业时间") { point["Exploded"] = "true"; point.Color = chartColors[0]; } if (point.AxisLabel == "停机时间") { point.Color = chartColors[0]; } } } //以下代码生成右侧饼图(Pie 作业时间 + 停机时间) private void Build_ChartPie1(DataTable dt) { //初始化图控件,设置公共属性 Chart_Initial(Chart2); var query = from r in dt.AsEnumerable() where r.Field("DEPT") == "作业时间" group r by r.Field("DEPT") into s select new { DEPT = s.Select(m => m.Field("DEPT")).First(), OPTIME = s.Sum(p => p.Field("OPTIME")) }; var query1 = from r in dt.AsEnumerable() where r.Field("DEPT") != "作业时间" group r by r.Field("DEPT") into s select new { DEPT = s.Select(m => m.Field("DEPT")).First(), OPTIME = s.Sum(p => p.Field("OPTIME")) }; //Get the names of Cities. decimal zytime = dt.AsEnumerable().Where(r=>r.Field("DEPT") == "作业时间").Sum(r => r.Field("OPTIME")); decimal tjtime = dt.AsEnumerable().Where(r => r.Field("DEPT") != "作业时间").Sum(r => r.Field("OPTIME")); string[] x = {"作业时间","停机时间"}; //Get the Total of Orders for each City. decimal[] y = {zytime,tjtime}; Chart2.Series.Add(new Series()); Chart2.Series[0].ChartType = SeriesChartType.Pie; Chart2.Series[0].Points.DataBindXY(x, y); foreach (DataPoint p in Chart2.Series[0].Points) { if (p.AxisLabel == "作业时间") { p.Color = chartColors[0]; } if (p.AxisLabel == "停机时间") { p.Color = chartColors[2]; } } //Chart2.Series[0]["PieLabelStyle"] = "Outside"; //Chart2.Legends[0].Enabled = true; //Chart2.Series[0]["PieLabelStyle"] = "Outside"; for (int i = 0; i < Chart2.Series[0].Points.Count; i++) { DataPoint p = Chart2.Series[0].Points[i]; //p.Label = x[i] + " #PERCENT{P2}"; p.Label = " #PERCENT{P2}"; p.LegendText = x[i]; } Chart2.ChartAreas[0].Area3DStyle.Enable3D = true; string deptName = ucb1.Text; Chart2.Titles.Add(new Title(deptName + "作业停机图", Docking.Top, new Font("Arial Unicode MS", float.Parse("15"), FontStyle.Regular), Color.FromName("Black"))); //Chart2.Legends[0].Docking = Docking.Bottom; //Docking是图例的停靠位置 //Chart2.Legends[0].Alignment = StringAlignment.Center;//Alignment属性是图例的对齐方式 //Chart2.BackColor = Color.Transparent; //Chart2.ChartAreas[0].BackColor = Color.Transparent; //Chart2.Legends[0].BackColor = Color.Transparent; foreach (DataPoint point in Chart2.Series[0].Points) { point["Exploded"] = "false"; if (point.AxisLabel == "作业时间") { point["Exploded"] = "true"; } } } //以下代码生成右侧饼图(Pie 停机时间) private void Build_ChartPie2(DataTable dt) { //初始化图控件,设置公共属性 Chart_Initial(Chart3); var query = from r in dt.AsEnumerable() where r.Field("DEPT") != "作业时间" group r by r.Field("DEPT") into s select new { DEPT = s.Select(m => m.Field("DEPT")).First(), OPTIME = s.Sum(p => p.Field("OPTIME")) }; //Get the names of Cities. string[] x = query.Select(p => p.DEPT).ToArray(); //Get the Total of Orders for each City. decimal[] y = query.Select(p => p.OPTIME).ToArray(); Chart3.Series.Add(new Series()); Chart3.Series[0].ChartType = SeriesChartType.Pie; Chart3.Series[0].Points.DataBindXY(x, y); //foreach (DataPoint p in Chart3.Series[0].Points) //{ // p.Label = x[0] + " #PERCENT{P2}"; //} Chart3.Series[0]["PieLabelStyle"] = "Outside"; Chart3.Legends[0].Enabled = true; //Chart3.Series[0]["PieLabelStyle"] = "Outside"; Chart3.Series[0]["3DLabelLineSize"] = "30"; Chart3.Series[0]["MinimumRelativePieSize"] = "30"; for (int i = 0; i < Chart3.Series[0].Points.Count; i++) { DataPoint p = Chart3.Series[0].Points[i]; //p.Label = x[i] +Environment.NewLine+ " #PERCENT{P2}"; //p.Label = " #PERCENT{P2}"; p.Label = x[i] + " #PERCENT{P2}"; p.LegendText = x[i]; } Chart3.ChartAreas[0].Area3DStyle.Enable3D = true; Chart3.Series[0].SmartLabelStyle.Enabled = true; Chart3.Series[0].SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.No; Chart3.Series[0].SmartLabelStyle.CalloutLineAnchorCapStyle = LineAnchorCapStyle.None; Chart3.Series[0].SmartLabelStyle.CalloutLineColor = Color.Red; Chart3.Series[0].SmartLabelStyle.CalloutLineWidth = 1; Chart3.Series[0].SmartLabelStyle.CalloutStyle = LabelCalloutStyle.None; string deptName = ucb1.Text; Chart3.Titles.Add(new Title(deptName + "停机责任图", Docking.Top, new Font("Arial Unicode MS", float.Parse("15"), FontStyle.Regular), Color.FromName("Black"))); //Chart3.Legends[0].Docking = Docking.Bottom; //Docking是图例的停靠位置 //Chart3.Legends[0].Alignment = StringAlignment.Center;//Alignment属性是图例的对齐方式 //Chart3.BackColor = Color.Transparent; //Chart3.ChartAreas[0].BackColor = Color.Transparent; //Chart3.Legends[0].BackColor = Color.Transparent; foreach (DataPoint point in Chart3.Series[0].Points) { point["Exploded"] = "false"; if (point.AxisLabel == "作业时间") { point["Exploded"] = "true"; } } } //以下代码相应左侧柱状图鼠标移动事件 private void Chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e) { if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { int i = e.HitTestResult.PointIndex; DataPoint dp = e.HitTestResult.Series.Points[i]; string dept = e.HitTestResult.Series.Name; if (dept == "作业率") { string percent = Math.Round((Chart1.Series["作业时间"].Points[i].YValues[0] * 100 / 24), 2).ToString("0.00") + "%"; e.Text = string.Format("日期:{0};\n{2}:{1:F2} ", DateTime.FromOADate(dp.XValue).ToString("yy-M-dd"), percent, dept); } else { //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。 e.Text = string.Format("日期:{0};\n{2}:{1:F2}小时 ", DateTime.FromOADate(dp.XValue).ToString("yy-M-dd"), dp.YValues[0], dept); } } } private int DateDiff(DateTime dateStart, DateTime dateEnd) { DateTime start = Convert.ToDateTime(dateStart.ToShortDateString()); DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString()); TimeSpan sp = end.Subtract(start); return sp.Days; } private void btn_month_Click(object sender, EventArgs e) { this.startDate.Value = this.endDate.Value.AddMonths(-1); } private void ucb1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { this.ucb1.DisplayMember = "plinename"; this.ucb1.ValueMember = "plinecode"; } private void timer1_Tick(object sender, EventArgs e) { angle += 1; if (angle >= 360) { angle = 0; } Chart2.Series[0]["PieStartAngle"] = angle.ToString(); PieExploed(angle); } private void timer2_Tick(object sender, EventArgs e) { angle1 += 1; if (angle1 >= 360) { angle1 = 0; } Chart3.Series[0]["PieStartAngle"] = angle1.ToString(); PieExploed1(angle); } //图点索引 private int index = 0; private void PieExploed(int angle) { if (angle == 1) { Chart2.Series[0].Points[0]["Exploded"] = "true"; } if (angle % 30 == 0) { foreach (DataPoint point in Chart2.Series[0].Points) { point["Exploded"] = "false"; } Chart2.Series[0].Points[index]["Exploded"] = "true"; if (index == Chart2.Series[0].Points.Count - 1) { index = 0; } else { index++; } } } private int index1 = 0; private void PieExploed1(int angle) { if (angle == 1) { Chart3.Series[0].Points[0]["Exploded"] = "true"; } if (angle % 30 == 0) { foreach (DataPoint point in Chart3.Series[0].Points) { point["Exploded"] = "false"; } Chart3.Series[0].Points[index1]["Exploded"] = "true"; if (index1 == Chart3.Series[0].Points.Count - 1) { index1 = 0; } else { index1++; } } } //旋转停止 private void toolStripMenuItem1_Click(object sender, EventArgs e) { if (chartActive != null) { if (chartActive.Name == "Chart2") { this.timer1.Enabled = !this.timer1.Enabled; } if (chartActive.Name == "Chart3") { this.timer2.Enabled = !this.timer2.Enabled; } } } //另存为 private void toolStripMenuItem4_Click(object sender, EventArgs e) { if (chartActive != null) { // Create a new save file dialog SaveFileDialog saveFileDialog1 = new SaveFileDialog(); // Sets the current file name filter string, which determines // the choices that appear in the "Save as file type" or // "Files of type" box in the dialog box. saveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; // Set image file format if (saveFileDialog1.ShowDialog() == DialogResult.OK) { ChartImageFormat format = ChartImageFormat.Bmp; if (saveFileDialog1.FileName.EndsWith("bmp")) { format = ChartImageFormat.Bmp; } else if (saveFileDialog1.FileName.EndsWith("jpg")) { format = ChartImageFormat.Jpeg; } else if (saveFileDialog1.FileName.EndsWith("emf")) { format = ChartImageFormat.Emf; } else if (saveFileDialog1.FileName.EndsWith("gif")) { format = ChartImageFormat.Gif; } else if (saveFileDialog1.FileName.EndsWith("png")) { format = ChartImageFormat.Png; } else if (saveFileDialog1.FileName.EndsWith("tif")) { format = ChartImageFormat.Tiff; } chartActive.BackColor = Color.White; chartActive.ChartAreas[0].BackColor = Color.White; chartActive.Legends[0].BackColor = Color.White; // Save image chartActive.SaveImage(saveFileDialog1.FileName, format); } chartActive.BackColor = Color.Transparent; chartActive.ChartAreas[0].BackColor = Color.Transparent; chartActive.Legends[0].BackColor = Color.Transparent; } } //拷贝 private void toolStripMenuItem3_Click(object sender, EventArgs e) { if (chartActive != null) { chartActive.BackColor = Color.White; chartActive.ChartAreas[0].BackColor = Color.White; chartActive.Legends[0].BackColor = Color.White; // create a memory stream to save the chart image System.IO.MemoryStream stream = new System.IO.MemoryStream(); // save the chart image to the stream chartActive.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Bmp); // create a bitmap using the stream Bitmap bmp = new Bitmap(stream); // save the bitmap to the clipboard Clipboard.SetDataObject(bmp); chartActive.BackColor = Color.Transparent; chartActive.ChartAreas[0].BackColor = Color.Transparent; chartActive.Legends[0].BackColor = Color.Transparent; MessageUtil.ShowTips("图片已拷贝至剪切板!"); } } //鼠标进入 chart控件获取焦点 private void Chart_MouseEnter(object sender, EventArgs e) { Chart chart = sender as Chart; if (chart != null) { chartActive = chart; chart.Focus(); } } private void Chart_KeyDown(object sender, KeyEventArgs e) { this.ctrlKeyDown = e.Control; this.shiftKeyDown = e.Shift; } private void Chart_KeyUp(object sender, KeyEventArgs e) { this.ctrlKeyDown = e.Control; this.shiftKeyDown = e.Shift; } //右键菜单打开事件(正在打开时) private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { ContextMenuStrip ctx = sender as ContextMenuStrip; Control control = ctx.SourceControl; Chart chart = control as Chart; if (chart != null) { //Chart1 柱状图屏蔽 转动命令 if (chart.Name == "Chart1") { ctx.Items[0].Visible = false; } else { ctx.Items[0].Visible = true; } } } //初始化数据源,为部门、产销下拉列表准备 private void initial_UltraDataSource1() { UltraDataRow row; // Initialize rows with data. // Get the first row. this.ultraDataSource1.Rows.SetCount(37); int index = 0; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "全部"; row["plinecode"] = "-1"; row["plinename"] = "全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "炼钢厂全部"; row["plinecode"] = "A"; row["plinename"] = "炼钢厂全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "炼钢厂"; row["plinename2"] = "一炼钢"; row["plinecode"] = "C001"; row["plinename"] = "一炼钢"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "炼钢厂"; row["plinename2"] = "二炼钢"; row["plinecode"] = "C003"; row["plinename"] = "二炼钢"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "轧管厂全部"; row["plinecode"] = "D"; row["plinename"] = "轧管厂全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "轧管厂"; row["plinename2"] = "250机组"; row["plinecode"] = "C008"; row["plinename"] = "250机组"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "轧管厂"; row["plinename2"] = "258机组"; row["plinecode"] = "C009"; row["plinename"] = "258机组"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "轧管厂"; row["plinename2"] = "168机组"; row["plinecode"] = "C010"; row["plinename"] = "168机组"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "轧管厂"; row["plinename2"] = "460机组"; row["plinecode"] = "C017"; row["plinename"] = "460机组"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉全部"; row["plinecode"] = "F0"; row["plinename"] = "管加工热处理炉全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "本部"; row["plinename2"] = "全部"; row["plinecode"] = "F01"; row["plinename"] = "管加工热处理炉本部全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "本部"; row["plinename2"] = "1#热处理"; row["plinecode"] = "C0220"; row["plinename"] = "1#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "本部"; row["plinename2"] = "2#热处理"; row["plinecode"] = "C0230"; row["plinename"] = "2#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "本部"; row["plinename2"] = "3#热处理"; row["plinecode"] = "C0240"; row["plinename"] = "3#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "本部"; row["plinename2"] = "4#热处理"; row["plinecode"] = "C0250"; row["plinename"] = "4#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "本部"; row["plinename2"] = "5#热处理"; row["plinecode"] = "C0260"; row["plinename"] = "5#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "深加工"; row["plinename2"] = "全部"; row["plinecode"] = "F02"; row["plinename"] = "管加工热处理炉深加工全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "深加工"; row["plinename2"] = "6#热处理"; row["plinecode"] = "C0540"; row["plinename"] = "6#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "深加工"; row["plinename2"] = "7#热处理"; row["plinecode"] = "C0550"; row["plinename"] = "7#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "深加工"; row["plinename2"] = "8#热处理"; row["plinecode"] = "C0480"; row["plinename"] = "8#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "元通"; row["plinename2"] = "全部"; row["plinecode"] = "F03"; row["plinename"] = "管加工热处理炉元通全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "元通"; row["plinename2"] = "9#热处理"; row["plinecode"] = "C0490"; row["plinename"] = "9#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理炉"; row["plinename1"] = "元通"; row["plinename2"] = "10#热处理"; row["plinecode"] = "C0500"; row["plinename"] = "10#热处理炉"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤全部"; row["plinecode"] = "F1"; row["plinename"] = "管加工热处理探伤全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "本部"; row["plinename2"] = "全部"; row["plinecode"] = "F11"; row["plinename"] = "管加工热处理探伤本部全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "本部"; row["plinename2"] = "1#热处理"; row["plinecode"] = "C0221"; row["plinename"] = "1#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "本部"; row["plinename2"] = "2#热处理"; row["plinecode"] = "C0231"; row["plinename"] = "2#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "本部"; row["plinename2"] = "3#热处理"; row["plinecode"] = "C0241"; row["plinename"] = "3#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "本部"; row["plinename2"] = "4#热处理"; row["plinecode"] = "C0251"; row["plinename"] = "4#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "本部"; row["plinename2"] = "5#热处理"; row["plinecode"] = "C0261"; row["plinename"] = "5#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "深加工"; row["plinename2"] = "全部"; row["plinecode"] = "F12"; row["plinename"] = "管加工热处理探伤深加工全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "深加工"; row["plinename2"] = "6#热处理"; row["plinecode"] = "C0541"; row["plinename"] = "6#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "深加工"; row["plinename2"] = "7#热处理"; row["plinecode"] = "C0551"; row["plinename"] = "7#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "深加工"; row["plinename2"] = "8#热处理"; row["plinecode"] = "C0481"; row["plinename"] = "8#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "元通"; row["plinename2"] = "全部"; row["plinecode"] = "F13"; row["plinename"] = "管加工热处理探伤元通全部"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "元通"; row["plinename2"] = "9#热处理"; row["plinecode"] = "C0491"; row["plinename"] = "9#热处理探伤"; row = this.ultraDataSource1.Rows[index++]; row["dept"] = "管加工热处理探伤"; row["plinename1"] = "元通"; row["plinename2"] = "10#热处理"; row["plinecode"] = "C0501"; row["plinename"] = "10#热处理探伤"; } } }