c#画图之雷达图
2021-05-18 14:29
标签:flags 区域 bit 创建 fill aging color graphic 五个 c#画图之雷达图 标签:flags 区域 bit 创建 fill aging color graphic 五个 原文地址:https://www.cnblogs.com/zhoushangwu/p/11741868.htmlpublic JsonResult DrawRadar()
{
List
int>> lineData = new List
int>> {
new Listint>(){ 12,23,15,44,32 },
new Listint>(){ 9,33,6,21,22 }
};
for (int i = 0; i )
{
int tempMaxCount = lineData[i].Max();
if (tempMaxCount > maxCount)
{
maxCount = tempMaxCount;
}
}
maxCount = maxCount == 0 ? 5 : maxCount;
#endregion
//单位转换对象
Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new Spire.Pdf.Graphics.PdfUnitConvertor();
//生成图像对象
Bitmap image = new Bitmap(width + margin_left + margin_right + lineNameWidth, height + margin_top + margin_bottom);
//创建画布
Graphics g = Graphics.FromImage(image);
//消除锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
//黑色画笔--主轴颜色
Brush blackBrush = new SolidBrush(Color.FromArgb(255, 102, 102, 102));
Pen blackPen = new Pen(blackBrush, 1);
//灰色画笔--辅助线条颜色
Brush grayBrush = new SolidBrush(Color.FromArgb(255, 200, 200, 200));
Pen grayPen = new Pen(grayBrush, 1);
//填充区域内容
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width + margin_left + margin_right + lineNameWidth, height + margin_top + margin_bottom);
Font font = new Font("宋体", unitCvtr.ConvertUnits(fontsize, Spire.Pdf.Graphics.PdfGraphicsUnit.Pixel, Spire.Pdf.Graphics.PdfGraphicsUnit.Point));
//圆心
int centerX = height / 2 + margin_left;
int centerY = width / 2 + margin_top;
//角的个数,规定是几边形
int len = radarNameData.Length;
//角度
int angle = 360 / len;
// 外围半径
int radius = width / 2;
// 每次递减的半径
int subRadius = radius / 5;
for (int k = 0; k 5; k++)
{
Point[] points = new Point[len + 1];
for (int i = 0; i )
{
double angleHude = i * angle * Math.PI / 180;/*角度变成弧度*/
int x = (int)((radius - k * subRadius) * Math.Cos(angleHude)) + centerX;
int y = (int)((radius - k * subRadius) * Math.Sin(angleHude)) + centerY;
Point point = new Point(x, y);
points[i] = point;
if (i == 0)
{
RectangleF sumRec = new RectangleF(x - 30, y - 15, 30, 15);
g.DrawString((100 - k * 20).ToString(), font, blackBrush, sumRec);
}
}
points[len] = points[0];
if (k == 0)
{
g.DrawLines(blackPen, points);
for (int i = 0; i 1; i++)
{
g.DrawLine(grayPen, points[i], new Point(centerX, centerY));
//90 270
StringFormat format = new StringFormat();
//两条竖线
int currAngle = i * angle;
int txtX = 0;
int txtY = 0;
if (currAngle % 180 == 90)
{
format.Alignment = StringAlignment.Center;
txtX = points[i].X;
if (currAngle == 90)
{
txtY = points[i].Y - 20;
}
else
{
txtY = points[i].Y + 20;
}
}
else
{
txtY = points[i].Y;
if (currAngle > 90 && currAngle 270)
{
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
txtX = points[i].X - 50;
}
else
{
format.FormatFlags = 0;
txtX = points[i].X + 10;
}
}
// 文字
Rectangle recText = new Rectangle(txtX, txtY, 40, fontsize);
g.DrawString(radarNameData[i], font, blackBrush, recText, format);
}
}
else
{
g.DrawLines(grayPen, points);
}
}
//开始数据
for (int i = 0; i )
{
/*
* 这个是色块
*/
//颜色快代表的内容
Color tempColor = colors[i];//GetRandomColor();
//文字内容
StringFormat txtFormat = new StringFormat();
//format.Alignment = StringAlignment.Center; //居中
//画笔
SolidBrush txtBrush = new SolidBrush(tempColor);
// 名称处理
// 颜色块
Rectangle rectangle = new Rectangle(margin_left + width + 50, margin_top + i * 25, 20, 20);
g.FillRectangle(txtBrush, rectangle);
// 文字
RectangleF rec = new RectangleF(margin_left + width + 75, margin_top + i * 25 + 4, 80, 20);
g.DrawString(lineName[i], font, blackBrush, rec, txtFormat);
Point[] points = new Point[lineData[i].Count + 1];
for (int j = 0; j )
{
int currRadius = Convert.ToInt32(lineData[i][j] / 100.0 * radius);
double angleHude = j * angle * Math.PI / 180;/*角度变成弧度*/
int x = (int)(currRadius * Math.Cos(angleHude)) + centerX;
int y = (int)(currRadius * Math.Sin(angleHude)) + centerY;
Point point = new Point(x, y);
points[j] = point;
}
points[lineData[i].Count] = points[0];
txtBrush.Color = Color.FromArgb(Convert.ToInt32(0.7 * 255), tempColor.R, tempColor.G, tempColor.B);
g.FillPolygon(txtBrush, points);
}
string relativePath = @"\draw-image\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
string path = Server.MapPath(relativePath);
image.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
//return relativePath;
return Json(relativePath, JsonRequestBehavior.AllowGet);
}