Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # broken Line Graph Control

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the C# broken line control, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the C# broken line control. Let's take a look.

Simple explanation

This is the first time to write a blog, but also the first time to publish their own code, there are shortcomings, please forgive me.

The source code refers to some resources found in the network search.

Because the line chart data I need is not less than 0, I am lazy in the calculation. Only data greater than 0 is supported.

The picture above

How to insert a beautiful code chip

Because of self-taught programming, code comments and naming are relatively messy, please forgive me.

This is the code for the new control. You need to add a FoldLineDiagram_Resize event to the control.

Using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace vc_farm {/ Line chart control / / Note: / 1, data columns are at least 2 columns. / 2. The length of the data column and the data header column must be the same / 3, the maximum length of the data header is 100 / 4, and the number of lines cannot be more than 10 / public partial class FoldLineDiagram: UserControl {private Bitmap mImage; / / line chart private FoldLineData mData / / record broken line data, and recalculate private List mSelectionArea = new List () when the window size changes; / / selectable area [useless here, originally used as recording data point, easy to judge whether the cursor selects a data line] private SelectionArea mNowSelectionArea; / / currently selected area [useless here] public FoldLineDiagram () {InitializeComponent () } # region prohibits base class attributes from displaying [Browsable (false)] [EditorBrowsable (EditorBrowsableState.Never)] public override Image BackgroundImage {get {return base.BackgroundImage;} set {base.BackgroundImage = value }} # endregion / get the broken line image (it can only be obtained correctly after using the ShowFoldLineDiagram method) / public Bitmap Image {get {return mImage }} / display Polyline data object public void ShowFoldLineDiagram (FoldLineData aData) {this.mData = aData; mImage = CreateImageS (aData); this.BackgroundImage = new Bitmap (mImage) / / the background is the copied picture / / this.BackgroundImageLayout = ImageLayout.Stretch / / stretch display display} / Save the line chart picture (it can be saved correctly only after using the ShowFoldLineDiagram method) / the path to the file / saved format public void SaveImage (string aSavePath) System.Drawing.Imaging.ImageFormat aImageFormat) {new Bitmap (mImage) .Save (aSavePath, aImageFormat) } private Bitmap CreateImageS (FoldLineData data) {# region data validation if (data.DataTitleText.Count 100) return null; / / limit the number of columns cannot be greater than 100 if (data.listFoldLineDataStyle.Count > 10) return null; / / limit the number of broken lines cannot be greater than 10 int temp = data.DataTitleText.Count / / get the data title length for (int I = 0; I

< data.listFoldLineDataStyle.Count; i++) //循环所有数据 { if (data.listFoldLineDataStyle[i].Data.Count !=temp) //当前数据长度 与数据标题长度不一致 { return null; } } #endregion #region 函数内部变量赋值 this.mSelectionArea.Clear(); //记录数据清空 int height = this.Height, width = this.Width; //设置图片大小 //设置左右上下边框距离图片边框间距 int left = (int)(width * 0.1); int right = (int)(width * 0.1); int top = (int)(height * 0.1); int bottom; if (data.ShowLegend == true) bottom = (int)(height * 0.15); //显示图例时,下边框为0.2 else bottom = (int)(height * 0.1); #endregion Bitmap image = new Bitmap(width, height); //新建一张图片 Graphics g = Graphics.FromImage(image); g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; try { #region 绘图准备工作 g.Clear(Color.White); //清空图片背景色 Font font = data.DataTitleTextFont; //设置 X与Y轴 标题字体 Font font1 = data.FoldLineTextFont; //设置 标题 字体 //Font font2 = aLineDataFont; //设置 数据显示 字体 LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(0, 0, image.Width, image.Height), data.BackgroundBorderColor, data.BackgroundBorderColor, 1.2f, true); g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height); #endregion #region 画折线图标题 Brush brush2 = new SolidBrush(data.FoldLineTextColor); SizeF sizeF = g.MeasureString(data.FoldLineText, font1); //计算标题文字大小 g.DrawString(data.FoldLineText, font1, brush2, (width - sizeF.Width) / 2, (top - sizeF.Height) / 2); //画标题 #endregion #region 绘制框线 //画图片的边框线 g.DrawRectangle(new Pen(data.BackgroundBorderColor), 0, 0, image.Width - 1, image.Height - 1); Pen mypen = new Pen(brush, 1); //边框线画笔 //绘制纵向线条 int xLineSpacing = (width - left - right) / (data.DataTitleText.Count - 1); //计算X轴 线条间距 int xPosition = left; //X轴开始位置 for (int i = 0; i < data.DataTitleText.Count; i++) { g.DrawLine(mypen, xPosition, top, xPosition, height - bottom); //画X轴竖线 sizeF = g.MeasureString(data.DataTitleText[i], font); //计算X轴文字大小 g.DrawString(data.DataTitleText[i], font, new SolidBrush(data.DataTitleTextColor), xPosition - (sizeF.Width / 2), height - bottom + 5); //设置文字内容及输出位置 xPosition += +xLineSpacing; //累加间距 } //Pen mypen1 = new Pen(Color.Blue, 3); xPosition = left; g.DrawLine(mypen, xPosition, top, xPosition, height - bottom); //画X轴第1条线(粗线) //绘制横向线条 List yName = ReckonYLine(data.listFoldLineDataStyle); int mLineCount = yName.Count; //计算Y轴行数 int yLineSpacing = (height - bottom - top) / (yName.Count - 1); //计算Y轴 线条间距 int yPosition = height - bottom; //Y轴开始点 for (int i = 0; i < yName.Count; i++) { g.DrawLine(mypen, left, yPosition, width - right, yPosition); sizeF = g.MeasureString(yName[i].ToString(), font); g.DrawString(yName[i].ToString(), font, new SolidBrush(data.DataTitleTextColor), left - sizeF.Width - 5, yPosition - (sizeF.Height / 2)); //设置文字内容及输出位置 yPosition -= yLineSpacing; } yPosition = height - bottom; g.DrawLine(mypen, left, yPosition, width - right, yPosition); //Y轴最下面一天线加粗 #endregion #region 画折线,及数据 for (int i = 0; i < data.listFoldLineDataStyle.Count; i++) { //显示折线效果 Pen mypen2 = new Pen(data.listFoldLineDataStyle[i].FoldLineColor, 2); //折线画笔 List pointData = data.listFoldLineDataStyle[i].Data; //取出折线数据 xPosition = left; float yMultiple = (float)(height - top - bottom) / (float)yName.Max(); //计算Y轴比例因子 List linePoint = new List(); //定义折线节点坐标 for (int j = 0; j < pointData.Count; j++) { Point point = new Point(); point.X = xPosition; point.Y = top + (int)((yName.Max() - pointData[j]) * yMultiple); xPosition += xLineSpacing; linePoint.Add(point); g.FillEllipse(new SolidBrush(data.listFoldLineDataStyle[i].FoldLineColor), point.X - 5, point.Y - 5, 10, 10); //画节点的圆点 g.DrawString(pointData[j].ToString(), data.listFoldLineDataStyle[i].FoldLineDataFont, new SolidBrush(data.listFoldLineDataStyle[i].FoldLineDataColor), point.X, point.Y + 10); //绘制节点文字 } g.DrawLines(mypen2, linePoint.ToArray()); //绘制折线 //记录画图区域 SelectionArea sa = new SelectionArea(); sa.linePoint = linePoint; //sa.rect = new Rectangle(); this.mSelectionArea.Add(sa); } #endregion #region 画图例 if (data.ShowLegend ==true) { int length = 0; //绘制的长度 for (int i = 0; i < data.listFoldLineDataStyle.Count; i++) { //显示折线效果 Pen mypen2 = new Pen(data.listFoldLineDataStyle[i].FoldLineColor, 2); //折线画笔 if (data.listFoldLineDataStyle[i].DataName == "折线") { data.listFoldLineDataStyle[i].DataName += i.ToString(); //如果是默认名称,则给默认名称加数字 } sizeF = g.MeasureString(data.listFoldLineDataStyle[i].DataName, data.DataTitleTextFont); //计算字体长度 //20:两个图例的间距,30:图例中颜色表示区宽度 ,10:图例颜色标识区与文本区间距 length += 20 + 30 + 10 + (int)sizeF.Width; } length += 20; //加上最后的间距 int startX = (width - length) / 2; int startY = (int)(height * 0.92); for (int i = 0; i < data.listFoldLineDataStyle.Count; i++) { //显示折线效果 Pen mypen2 = new Pen(data.listFoldLineDataStyle[i].FoldLineColor, 2); //折线画笔 if (data.listFoldLineDataStyle[i].DataName == "折线") { data.listFoldLineDataStyle[i].DataName += i.ToString(); //如果是默认名称,则给默认名称加数字 } sizeF = g.MeasureString(data.listFoldLineDataStyle[i].DataName, data.DataTitleTextFont); //计算字体长度 g.FillRectangle(new SolidBrush(data.listFoldLineDataStyle[i].FoldLineColor), startX, startY, 30, 10); //绘制小矩形 g.DrawString(data.listFoldLineDataStyle[i].DataName, data.DataTitleTextFont, new SolidBrush(data.listFoldLineDataStyle[i].FoldLineColor), startX + 30 + 10, startY); startX += 30 + 10 + (int)sizeF.Width+20; //记录画图区域的 图例显示区域 Rectangle rect = new Rectangle(startX, startY, 30, 10); SelectionArea sa = this.mSelectionArea[i]; sa.rect = rect; this.mSelectionArea[i] = sa; } } #endregion return new Bitmap(image); } finally { g.Dispose(); image.Dispose(); } } /// /// Y轴横线 及 Y轴标题内如 计算 /// /// /// private List ReckonYLine(List flData) { List AllData = new List(); //所有数据汇总在一起 foreach (FoldLineDataStyle item in flData) { AllData.AddRange(item.Data); } //定义最大值与最小值 int max = AllData.Max(); int min = AllData.Min(); List yName = new List(); int csMax = 0; //测算上限 /*如果需要增加小于0数据判断,则需要在此次增加一些判断。 *就是取最小值,判断是否为负数,是则取绝对值进行计算,不是则和现在计算方式一样 */ if (max.ToString().Length >

1) / / if greater than 9 {/ / the measured maximum upper limit string ling = ""; for (int I = 0; I < max.ToString (). Length-1; iTunes +) / / adds 0 ling + = "0" at the end of the number String temp = max.ToString () .Substring (0,1); / / take out the highest digit csMax = Int32.Parse ((Int32.Parse (temp) + 1) + ling); / / if max=75162, convert to 80000 for (int I = 0; I = rect.Y & & pt.X)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report