/* * 画出一段间隔的数据 * @return 衔接点 */ public void drawPicture(MyFunPictureBox picture, Graphics g, Pen thinPen, Pen fatPen, List <MyBrush> fillBrushes) { List <MyPoint> startPoint = new List <MyPoint>(numOfFlgToFill + 1); //保存开始的一列点 for (int i = 0; i < numOfFlgToFill + 1; i++) { startPoint.Add(new MyPoint(zeroPoint.X, zeroPoint.Y)); } Int32 startIndex = getStartIndex(curYear, curMonth, curDay); Int32 drawMonth = curMonth; Int32 maxDayVal = getDayNum(curYear, curMonth) - 1; for (int drawDay = 0; drawDay < disDays; drawDay++) { //判断用户所选天是否超过当月最多天数,添加by孙凯 2016.3.21 if (startIndex >= picture.LevelLines[drawMonth].Rows.Count) { //MessageBox.Show("没有数据"); //如果超过当前月则进入下月 if (startIndex >= picture.LevelLines[drawMonth].Rows.Count) { drawMonth++; if (drawMonth > 11) { return; } startIndex = 0; for (int i = 0; i < numOfFlgToFill + 1; i++) { startPoint[i] = new MyPoint(zeroPoint.X + xInterval * oneDayPoints, zeroPoint.Y); } } continue; } startPoint = drawOneDay(picture, g, thinPen, fatPen, fillBrushes, drawMonth, startIndex, startPoint); startIndex += oneDayRows; //如果超过当前月则进入下月 if (startIndex >= picture.LevelLines[drawMonth].Rows.Count) { drawMonth++; if (drawMonth > 11) { return; } startIndex = 0; } } }
/* * 绘制Y轴的标注 * 返回Y轴标注间隔距离 */ private double drawYIdentity(MyFunPictureBox picture, Graphics g) { Point yAxesPoint = new Point(zeroPoint.X, picture.drawArea.Top); //字符字体及颜色 Font drawFont = new Font("宋体", picture.smallFontSize); SolidBrush drawBrush = new SolidBrush(Color.Black); StringFormat stringFormat = new StringFormat(); //画出Y轴标识 stringFormat.Alignment = StringAlignment.Far; double ystep = picture.drawArea.Height * 0.9 / 10.2; for (int i = 1; i < 11; i++) { // g.DrawLine(drawAxisPen, zeroPoint.X, zeroPoint.Y - (int)(i * ystep), zeroPoint.X - 5, zeroPoint.Y - (int)(i * ystep)); Rectangle rect = new Rectangle( zeroPoint.X - 70, zeroPoint.Y - (int)(i * ystep) - drawFont.Height / 2, 60, drawFont.Height); if (isDefaultUnit) { g.DrawString(((int)(yInterval * i * ystep) / 10 * 10).ToString(), drawFont, drawBrush, rect, stringFormat); } else { g.DrawString(((int)(yInterval * i * ystep * 0.1)).ToString(), drawFont, drawBrush, rect, stringFormat); } } if (isDefaultUnit) { g.DrawString("MW", drawFont, drawBrush, zeroPoint.X - 2 * drawFont.SizeInPoints, yAxesPoint.Y - drawFont.Height / 2); } else { g.DrawString("万kW", drawFont, drawBrush, zeroPoint.X - 4 * drawFont.SizeInPoints, yAxesPoint.Y - drawFont.Height / 2); } Font nameFont = new Font("宋体", picture.largeFontSize + 1, FontStyle.Bold); String[] tips = pictureTip.Split('\n'); for (int i = 0; i < tips.Length; i++) { g.DrawString(tips[i], nameFont, drawBrush, (float)(picture.drawArea.Left + picture.drawArea.Width * 0.1 + (picture.drawArea.Width * 0.9 + (tips[i].Length * 2) / 2 * 15) / 2) , (float)(picture.drawArea.Top + picture.drawArea.Height * 0.93 + i * 15), stringFormat); } nameFont.Dispose(); return(ystep); }
/* * 绘制X轴、Y轴 */ public void drawAxes(MyFunPictureBox picture, Graphics g) { Pen Axes = new Pen(Brushes.Black, 3.5F); Pen drawLinePen = new Pen(Brushes.Black, 3F); Point yAxesPoint = new Point(zeroPoint.X, picture.drawArea.Top); Point xAxesPoint = new Point(picture.drawArea.Right, zeroPoint.Y); //画出X轴横线 g.DrawLine(Axes, zeroPoint, xAxesPoint); g.DrawLine(drawLinePen, xAxesPoint, new Point(xAxesPoint.X - 10, xAxesPoint.Y - 5)); g.DrawLine(drawLinePen, xAxesPoint, new Point(xAxesPoint.X - 10, xAxesPoint.Y + 5)); //画出Y轴数线 g.DrawLine(Axes, zeroPoint, yAxesPoint); g.DrawLine(drawLinePen, yAxesPoint, new Point(yAxesPoint.X - 5, yAxesPoint.Y + 10)); g.DrawLine(drawLinePen, yAxesPoint, new Point(yAxesPoint.X + 5, yAxesPoint.Y + 10)); double yStep = drawYIdentity(picture, g); //画出X轴的标识 drawXIdentity(picture, g, yStep); }
/************************************************************************/ /* 画出X轴的标识 */ /************************************************************************/ private void drawXIdentity(MyFunPictureBox picture, Graphics g, double yStep) { //字符字体及颜色 Font drawFont = new Font("宋体", picture.smallFontSize); SolidBrush drawBrush = new SolidBrush(Color.Black); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; Pen drawAxisPen = new Pen(Brushes.Black, 1.5F); //X轴小竖线 //日期 Int32 day = curDay; Int32 month = curMonth; //绘制网格 Pen girdPen = new Pen(Brushes.Gray, 1); girdPen.DashStyle = DashStyle.Dash; //分钟、天、月的间隔像素 double minStep = xInterval * rowsOfOneFlg; double dayStep = xInterval * oneDayPoints; double monthStep = xInterval * oneDayPoints * getDayNum(curYear, curMonth); g.DrawString((month + 1) + "月" + (day + 1) + "日", drawFont, drawBrush, zeroPoint.X - 2 * drawFont.SizeInPoints, zeroPoint.Y + 3, stringFormat); //绘制网格横线 for (int i = 1; isDisGird && i < 11; i++) { g.DrawLine(girdPen, zeroPoint.X, zeroPoint.Y - (int)(yStep * i), zeroPoint.X + (int)(disDays * dayStep), zeroPoint.Y - (int)(yStep * i)); } if (minStep > 25) { for (int numOfDay = 1; numOfDay <= disDays; numOfDay++) { for (int numOfMin = 1; numOfMin < oneRowPoints; numOfMin++) { //绘制网格 if (isDisGird) { g.DrawLine(girdPen, zeroPoint.X + (int)(numOfMin * minStep), zeroPoint.Y, zeroPoint.X + (int)(numOfMin * minStep), zeroPoint.Y - (int)(yStep * 10)); } g.DrawLine(drawAxisPen, zeroPoint.X + (int)(numOfMin * minStep), zeroPoint.Y, zeroPoint.X + (int)(numOfMin * minStep), zeroPoint.Y - 10); g.DrawString(numOfMin.ToString(), drawFont, drawBrush, zeroPoint.X + (int)(numOfMin * minStep), zeroPoint.Y + 3, stringFormat); } day++; if (day >= getDayNum(curYear, month)) { month++; day = 0; //绘制网格 if (isDisGird) { g.DrawLine(girdPen, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y - (int)(yStep * 10)); } g.DrawLine(drawAxisPen, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y - 10); g.DrawString((month + 1) + "月" + (day + 1) + "日", drawFont, drawBrush, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y + 3, stringFormat); } else { //绘制网格 if (isDisGird) { g.DrawLine(girdPen, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y - (int)(yStep * 10)); } g.DrawLine(drawAxisPen, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y - 10); g.DrawString((day + 1) + "日", drawFont, drawBrush, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y + 3, stringFormat); } } } else { int numOfday; for (numOfday = 1; numOfday <= disDays / 2 && numOfday <= getDayNum(curYear, month) / 2; numOfday++) { double xStep = numOfday * dayStep; int numOfxStep = 0; if (xStep > 30) { for (int i = 1; i <= disDays; i += numOfday) { day += numOfday; numOfxStep++; if (day >= getDayNum(curYear, month)) { month++; day = 0; //绘制网格 if (isDisGird) { g.DrawLine(girdPen, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y - (int)(yStep * 10)); } g.DrawLine(drawAxisPen, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y - 10); if (month < 12) { g.DrawString((month + 1) + "月" + (day + 1) + "日", drawFont, drawBrush, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y + 3, stringFormat); } } else { //绘制网格 if (isDisGird) { g.DrawLine(girdPen, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y - (int)(yStep * 10)); } g.DrawLine(drawAxisPen, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y - 10); if (month < 12) { g.DrawString((day + 1) + "日", drawFont, drawBrush, zeroPoint.X + (int)(xStep * numOfxStep), zeroPoint.Y + 3, stringFormat); } } } break; } } if (numOfday > disDays / 2 || numOfday > getDayNum(curYear, month) / 2) { Int32 startDay = -1; for (int numOfDay = 0; numOfDay <= disDays && month < 11; numOfDay++) { if ((day + numOfDay) >= (startDay + getDayNum(curYear, month))) { startDay = day + numOfDay; day = 0; month++; //绘制网格 if (isDisGird) { g.DrawLine(girdPen, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y - (int)(yStep * 10)); } g.DrawLine(drawAxisPen, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y - 10); if (month < 12) { g.DrawString((month + 1) + "月" + (day + 1) + "日", drawFont, drawBrush, zeroPoint.X + (int)(numOfDay * dayStep), zeroPoint.Y + 3, stringFormat); } } } } } }
/* * 画出一天的数据 * Flg = 0,27,28对应的点startPoint的下标为12,13,14 * @return 衔接点 */ private List <MyPoint> drawOneDay(MyFunPictureBox picture, Graphics g, Pen thinPen, Pen fatPen, List <MyBrush> fillBrushes, Int32 drawMonth, Int32 startIndex, List <MyPoint> startPoint) { List <MyPoint> leftPoints = startPoint; //保存在左面的一列点 List <MyPoint> rightPoints = new List <MyPoint>(startPoint); //保存在右面的一列点 int[] indexOfBrush = new int[12]; //保存画笔下标 //每行数据确定画笔颜色下标 indexOfBrush = getBrushArray(); //绘制Flg=0,27,28曲线是的画笔 Pen dashPen0 = new Pen(Color.Black, 1.0f); dashPen0.DashStyle = DashStyle.Dash; Pen dashPen27 = new Pen(Color.Orange, 1.0f); dashPen27.DashStyle = DashStyle.Dash; Pen dashPen28 = new Pen(Color.HotPink, 1.0f); dashPen28.DashStyle = DashStyle.Dash; Int32 numOfInterval = 1; double xStartVal = startPoint[0].xVal; //打印各个Flg一行数据中所包含的点 for (int curH = 0; curH < oneRowPoints; curH++) { //打印一天的点 for (int curRow = 0; curRow < rowsOfOneFlg; curRow++) { double xVal = xStartVal + xInterval * numOfInterval; int preYVal = zeroPoint.Y; //leftPoints保存rightPoints的值 leftPoints = new List <MyPoint>(rightPoints); rightPoints[0] = new MyPoint(xVal, zeroPoint.Y); //打印一个竖列的点 for (int curFlg = 1; curFlg < rightPoints.Count; curFlg++) { Int32 yVal = 0; //curFlg = 12,13,14的虚线对应Flg=0,27,28 if (curFlg == 12) { yVal = zeroPoint.Y - (Int32)(float.Parse(picture.LevelLines[drawMonth].Rows[startIndex + curRow][curH + 1].ToString()) / yInterval); rightPoints[curFlg] = new MyPoint(xVal, yVal); g.DrawLine(dashPen0, leftPoints[curFlg].getPoint(), rightPoints[curFlg].getPoint()); continue; } else if (curFlg == 13 || curFlg == 14) { yVal = zeroPoint.Y - (Int32)(float.Parse(picture.LevelLines[drawMonth].Rows[startIndex + curRow + (curFlg - 1) * rowsOfOneFlg][curH + 1].ToString()) / yInterval); rightPoints[curFlg] = new MyPoint(xVal, yVal); if (curFlg == 13) { g.DrawLine(dashPen27, leftPoints[curFlg].getPoint(), rightPoints[curFlg].getPoint()); } else if (curFlg == 14) { g.DrawLine(dashPen28, leftPoints[curFlg].getPoint(), rightPoints[curFlg].getPoint()); } continue; } yVal = zeroPoint.Y - (Int32)(float.Parse(picture.LevelLines[drawMonth].Rows[startIndex + curRow + curFlg * rowsOfOneFlg][curH + 1].ToString()) / yInterval); ////用于确保Flg递增时其大小递增 Flg=21(对应于curFlg=6)时除外 if (yVal > preYVal && curFlg != 6) { yVal = preYVal; } rightPoints[curFlg] = new MyPoint(xVal, yVal); //Flg为22和5的构成电力不足 if (curFlg == 7) { // if (rightPoints[curFlg].yVal < rightPoints[curFlg-2].yVal) g.FillPolygon( fillBrushes[indexOfBrush[curFlg - 1]].myBrush, new Point[] { leftPoints[curFlg].getPoint(), rightPoints[curFlg].getPoint(), rightPoints[curFlg - 2].getPoint(), leftPoints[curFlg - 2].getPoint() }); } else if (curFlg == 6) { g.FillPolygon( fillBrushes[indexOfBrush[curFlg - 1]].myBrush, new Point[] { leftPoints[curFlg - 1].getPoint(), rightPoints[curFlg - 1].getPoint(), rightPoints[curFlg].getPoint(), leftPoints[curFlg].getPoint() }); } else { g.FillPolygon( fillBrushes[indexOfBrush[curFlg - 1]].myBrush, new Point[] { leftPoints[curFlg].getPoint(), rightPoints[curFlg].getPoint(), rightPoints[curFlg - 1].getPoint(), leftPoints[curFlg - 1].getPoint() }); } preYVal = yVal; } numOfInterval++; } } return(rightPoints); }