C#中System.Drawing来画立体饼状图

来源:转载

C#中System.Drawing来画立体饼状图,有需要的朋友可以参考下。

核心代码:

 /// <summary> /// 创建立体感的饼状图 /// </summary> /// <param name="angles">扇区角度List集合</param> /// <param name="lightColors">扇区上面亮度List集合</param> /// <param name="darkColors">扇区下面暗度List集合</param> /// <returns></returns> public static Texture2D PieChart3d(ref Texture2D lp, List<float> angles,List<System.Drawing.Color> lightColors,List<System.Drawing.Color> darkColors,System.Drawing.Color light,System.Drawing.Color dark) {if (lp == null){lp = new Texture2D(512, 512);}Bitmap bitmap = new Bitmap(512, 512);System.Drawing.Graphics ccd = System.Drawing.Graphics.FromImage(bitmap);ccd.SmoothingMode = SmoothingMode.AntiAlias;ccd.Clear(System.Drawing.Color.Transparent);IntPtr ccdhdc = ccd.GetHdc(); //获取设备int vOffset = 30;int left = 10;int right = 492;int top = 10;int bottom = 348;int center_X = (left + right) / 2;int center_Y = (top + bottom) / 2;float width = (right - left);float height = (bottom - top);IntPtr brush = CreateSolidBrush(ColorTranslator.ToWin32(dark));IntPtr pen = CreatePen(5, 0, ColorTranslator.ToWin32(dark));//画下面的圆SelectObject(ccdhdc, brush);SelectObject(ccdhdc, pen);Pie(ccdhdc, left, top + vOffset, right, bottom + vOffset, right, center_Y + vOffset, right, center_Y + vOffset);//画上面的圆brush = CreateSolidBrush(ColorTranslator.ToWin32(light));SelectObject(ccdhdc, brush);Pie(ccdhdc, left, top, right, bottom, right, center_Y, right, center_Y);//画下面的扇形float startAngle = 0;int lastX = right;int lastY = center_Y + vOffset;GPoint[] ploygon = new GPoint[4];for (int i = 0; i != angles.Count; ++i){IntPtr fillBrush = CreateSolidBrush(ColorTranslator.ToWin32(darkColors[i]));SelectObject(ccdhdc, fillBrush);int x1 = Convert.ToInt32(center_X + width / 2.0 * Math.Cos(startAngle * Math.PI / 180));int y1 = Convert.ToInt32(center_Y + vOffset + height / 2.0 * Math.Sin(startAngle * Math.PI / 180));startAngle += angles[i];int x2 = Convert.ToInt32(center_X + width / 2.0 * Math.Cos(startAngle * Math.PI / 180));int y2 = Convert.ToInt32(center_Y + vOffset + height / 2.0 * Math.Sin(startAngle * Math.PI / 180));Pie(ccdhdc, left, top + vOffset, right, bottom + vOffset, x2, y2, x1, y1);//修补使连接无缝int xOffset = 3;if (i > 0){ IntPtr lastBrush = CreateSolidBrush(ColorTranslator.ToWin32(darkColors[i - 1])); SelectObject(ccdhdc, lastBrush); ploygon[0].x = center_X; ploygon[0].y = center_Y; ploygon[1].x = center_X; ploygon[1].y = center_Y + vOffset; ploygon[2].x = lastX - xOffset; ploygon[2].y = lastY; ploygon[3].x = lastX - xOffset; ploygon[3].y = lastY - vOffset; Polygon(ccdhdc, ploygon, 4);}IntPtr cBrush = CreateSolidBrush(ColorTranslator.ToWin32(darkColors[i]));SelectObject(ccdhdc, cBrush);ploygon[0].x = lastX - xOffset;ploygon[0].y = lastY;ploygon[1].x = lastX - xOffset;ploygon[1].y = lastY - vOffset;ploygon[2].x = x2;ploygon[2].y = y2 - vOffset;ploygon[3].x = x2;ploygon[3].y = y2;Polygon(ccdhdc, ploygon, 4);lastX = x2;lastY = y2;if (i == angles.Count - 1){ IntPtr xBrush = CreateSolidBrush(ColorTranslator.ToWin32(lightColors[i])); SelectObject(ccdhdc, xBrush); ploygon[0].x = center_X; ploygon[0].y = center_Y; //ploygon[1].x = center_X; //ploygon[1].y = center_Y + vOffset; ploygon[1].x = lastX; ploygon[1].y = lastY; ploygon[2].x = lastX; ploygon[2].y = lastY - vOffset; Polygon(ccdhdc, ploygon, 3); IntPtr yBrush = CreateSolidBrush(ColorTranslator.ToWin32(light)); SelectObject(ccdhdc, yBrush); ploygon[0].x = center_X; ploygon[0].y = center_Y; ploygon[1].x = center_X; ploygon[1].y = center_Y + vOffset; ploygon[2].x = lastX; ploygon[2].y = lastY; Polygon(ccdhdc, ploygon, 3);}}//画上面的扇形 startAngle = 0;for (int i = 0; i != angles.Count; ++i){IntPtr fillBrush = CreateSolidBrush(ColorTranslator.ToWin32(lightColors[i]));SelectObject(ccdhdc, fillBrush);int x1 = Convert.ToInt32(center_X + width / 2.0 * Math.Cos(startAngle * Math.PI / 180));int y1 = Convert.ToInt32(center_Y + height / 2.0 * Math.Sin(startAngle * Math.PI / 180));startAngle += angles[i];int x2 = Convert.ToInt32(center_X + width / 2.0 * Math.Cos(startAngle * Math.PI / 180));int y2 = Convert.ToInt32(center_Y + height / 2.0 * Math.Sin(startAngle * Math.PI / 180));Pie(ccdhdc, left, top, right, bottom, x2, y2, x1, y1);}ccd.ReleaseHdc();byte[] byteArray = new byte[0];using (MemoryStream stream = new MemoryStream()){bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);stream.Close();byteArray = stream.ToArray();}lp.LoadImage(byteArray);return lp; }


版权声明:本文为博主原创文章,未经博主允许不得转载。



分享给朋友:
您可能感兴趣的文章:
随机阅读: