You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
240 lines
9.4 KiB
240 lines
9.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Imaging;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GummingCommon
|
|
{
|
|
public class KChart
|
|
{
|
|
private int _width;
|
|
private int _height;
|
|
private int _marginleft;
|
|
private int _margintop;
|
|
private int _chartwidth;
|
|
private int _chartheight;
|
|
private float _ymaxvalue;
|
|
private int _bottomline;
|
|
private int _centerline;
|
|
private bool _iscenterline;
|
|
private float _ystep;
|
|
private float _xstep;
|
|
private float _xelementsize;
|
|
private Font font = new Font("Airal", 10);
|
|
private SolidBrush brush = new SolidBrush(Color.Black);
|
|
private Bitmap _expbmp;
|
|
|
|
public KChart(int width, int height, int marginleft, int margintop, int marginbottom, int marginright)
|
|
{
|
|
_width = width;
|
|
_height = height;
|
|
_marginleft = marginleft;
|
|
_margintop = margintop;
|
|
_chartwidth = width - marginleft - marginright;
|
|
_chartheight = height -margintop - marginbottom;
|
|
_bottomline = height - marginbottom;
|
|
_centerline = _chartheight / 2 + margintop;
|
|
|
|
_expbmp = new Bitmap(_width, _height, PixelFormat.Format24bppRgb);
|
|
_expbmp.SetResolution(96, 96);
|
|
}
|
|
|
|
public Bitmap Publish()
|
|
{
|
|
return _expbmp;
|
|
}
|
|
|
|
public void CreateBaseCoordinate(bool iscenter)
|
|
{
|
|
_iscenterline = iscenter;
|
|
Graphics g = Graphics.FromImage(_expbmp);
|
|
g.Clear(Color.White);
|
|
g.DrawLine(Pens.Black, new Point(_marginleft, _bottomline), new Point(_marginleft, _margintop));
|
|
if (iscenter)
|
|
{
|
|
g.DrawLine(Pens.Black, new Point(_marginleft, _centerline), new Point(_marginleft + _chartwidth, _centerline));
|
|
g.DrawString("0", font, brush, _marginleft - 5, _centerline - 7, new StringFormat() { Alignment = StringAlignment.Far });
|
|
}
|
|
else
|
|
{
|
|
g.DrawLine(Pens.Black, new Point(_marginleft, _bottomline), new Point(_marginleft + _chartwidth, _bottomline));
|
|
g.DrawString("0", font, brush, _marginleft - 5, _bottomline - 7, new StringFormat() { Alignment = StringAlignment.Far });
|
|
}
|
|
|
|
g.Flush();
|
|
g.Dispose();
|
|
}
|
|
|
|
public void CreateVCoordinateLine(string title, float ymaxvalue, int count, bool showradix)
|
|
{
|
|
_ymaxvalue = ymaxvalue;
|
|
Graphics g = Graphics.FromImage(_expbmp);
|
|
|
|
_ystep = (int)Math.Floor(_chartheight * 1.0 / count);
|
|
decimal displaystep;
|
|
if (_ymaxvalue > 1 && !showradix)
|
|
{
|
|
displaystep = (decimal)Math.Floor(_ymaxvalue * 1.0 / count);
|
|
}
|
|
else
|
|
{
|
|
displaystep = (decimal)Math.Round(_ymaxvalue * 1.0 / count, 2);
|
|
}
|
|
|
|
SizeF wordsize = g.MeasureString("温", font);
|
|
int start = (int)((_chartheight - wordsize.Height* title.Length) / 2);
|
|
|
|
for (int i = 0; i < title.Length; i++)
|
|
{
|
|
g.DrawString(title.Substring(i, 1), font, brush, 10, start + wordsize.Height * i + 2, new StringFormat() { Alignment = StringAlignment.Center });
|
|
}
|
|
if (_iscenterline)
|
|
{
|
|
for (int i = 1; i < (count + 1) /2; i++)
|
|
{
|
|
g.DrawLine(Pens.LightGray, new Point(_marginleft + 1, (int)(_centerline - _ystep * i)), new Point(_marginleft + _chartwidth, (int)(_centerline - _ystep * i)));
|
|
g.DrawString(((decimal)(displaystep * i)).ToString(), font, brush, _marginleft - 5, _centerline - _ystep * i - 8, new StringFormat() { Alignment = StringAlignment.Far });
|
|
|
|
g.DrawLine(Pens.LightGray, new Point(_marginleft + 1, (int)(_centerline + _ystep * i)), new Point(_marginleft + _chartwidth, (int)(_centerline + _ystep * i)));
|
|
g.DrawString(((decimal)(displaystep * i *-1)).ToString(), font, brush, _marginleft - 5, _centerline + _ystep * i - 8, new StringFormat() { Alignment = StringAlignment.Far });
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 1; i < count + 1; i++)
|
|
{
|
|
g.DrawLine(Pens.LightGray, new Point(_marginleft + 1, (int)(_bottomline - _ystep * i)), new Point(_marginleft + _chartwidth, (int)(_bottomline - _ystep * i)));
|
|
|
|
g.DrawString(((decimal)(displaystep * i)).ToString(), font, brush, _marginleft - 5, _bottomline - _ystep * i - 8, new StringFormat() { Alignment = StringAlignment.Far });
|
|
}
|
|
}
|
|
g.Flush();
|
|
g.Dispose();
|
|
}
|
|
|
|
public void CreateHCoordinateLine(List<string> titles, bool withline)
|
|
{
|
|
Graphics g = Graphics.FromImage(_expbmp);
|
|
int count = titles.Count + 1;
|
|
int step = (int)(_chartwidth / (count));
|
|
|
|
int currentline = _bottomline;
|
|
if (_iscenterline)
|
|
{
|
|
currentline = _centerline;
|
|
}
|
|
|
|
for (int i = 1; i < count; i++)
|
|
{
|
|
if (withline)
|
|
{
|
|
g.DrawLine(Pens.LightGray, new Point(_marginleft + step * i, currentline), new Point(_marginleft + step * i, currentline - 5));
|
|
}
|
|
//g.DrawString(titles[i - 1], font, brush, _marginleft + step * i, currentline + 3, new StringFormat() { Alignment = StringAlignment.Center });
|
|
|
|
if (count > 40)
|
|
{
|
|
if (i % 2 == 0)
|
|
{
|
|
g.DrawString(titles[i - 1], font, brush, _marginleft + step * i, currentline + 18, System.Drawing.StringFormat.GenericTypographic);
|
|
}
|
|
else
|
|
{
|
|
g.DrawString(titles[i - 1], font, brush, _marginleft + step * i, currentline + 3, System.Drawing.StringFormat.GenericTypographic);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DrawString(g, titles[i - 1], font, brush, new PointF(_marginleft + step * i, currentline + 3), new StringFormat() { Alignment = StringAlignment.Center }, -45);
|
|
}
|
|
}
|
|
|
|
g.Flush();
|
|
g.Dispose();
|
|
}
|
|
|
|
public void DrawText(string title, PointF p)
|
|
{
|
|
Graphics g = Graphics.FromImage(_expbmp);
|
|
DrawString(g, title, font, brush, p, new StringFormat() { Alignment = StringAlignment.Center }, 0);
|
|
}
|
|
|
|
public void DrawString(Graphics g, string s, Font font, Brush brush, PointF point, StringFormat format, float angle)
|
|
{
|
|
// Save the matrix
|
|
Matrix mtxSave = g.Transform;
|
|
|
|
Matrix mtxRotate = g.Transform;
|
|
mtxRotate.RotateAt(angle, point);
|
|
g.Transform = mtxRotate;
|
|
|
|
g.DrawString(s, font, brush, point, format);
|
|
|
|
// Reset the matrix
|
|
g.Transform = mtxSave;
|
|
}
|
|
|
|
public void CreateBarChart(List<float> points, int size, Color color)
|
|
{
|
|
_xelementsize = size;
|
|
Graphics g = Graphics.FromImage(_expbmp);
|
|
int count = points.Count + 1;
|
|
int step = (int)(_chartwidth / (count));
|
|
|
|
int charth = _chartheight;
|
|
if (_iscenterline)
|
|
{
|
|
charth = _chartheight / 2;
|
|
}
|
|
for (int i = 1; i < count; i++)
|
|
{
|
|
int barheight = (int)(points[i - 1] * _chartheight / _ymaxvalue);
|
|
if (barheight < 0)
|
|
{
|
|
g.FillRectangle(new SolidBrush(color), new Rectangle(_marginleft + step * i - size / 2, _centerline, size, Math.Abs(barheight)));
|
|
}
|
|
else
|
|
{
|
|
g.FillRectangle(new SolidBrush(color), new Rectangle(_marginleft + step * i - size / 2, _margintop + (charth - barheight), size, barheight));
|
|
}
|
|
}
|
|
|
|
g.Flush();
|
|
g.Dispose();
|
|
}
|
|
|
|
public void CreateLineChart(List<float> points, int size, Color color)
|
|
{
|
|
_xelementsize = size;
|
|
Graphics g = Graphics.FromImage(_expbmp);
|
|
int count = points.Count + 1;
|
|
int step = (int)(_chartwidth / (count));
|
|
|
|
int charth = _chartheight;
|
|
if (_iscenterline)
|
|
{
|
|
charth = _chartheight / 2;
|
|
}
|
|
for (int i = 1; i < count ; i++)
|
|
{
|
|
int previousP = (int)(points[i - 1] * _chartheight / _ymaxvalue);
|
|
g.FillEllipse(new SolidBrush(Color.FromArgb(30, 30, 255)), new Rectangle(_marginleft + step * i - 2, _margintop + (charth - previousP) - 2, 4, 4));
|
|
|
|
if (i < count - 1)
|
|
{
|
|
int currentP = (int)(points[i] * _chartheight / _ymaxvalue);
|
|
g.DrawLine(new Pen(color), new Point(_marginleft + step * i, _margintop + (charth - previousP)),
|
|
new Point(_marginleft + step * (i + 1), _margintop + (charth - currentP)));
|
|
}
|
|
}
|
|
|
|
g.Flush();
|
|
g.Dispose();
|
|
}
|
|
}
|
|
}
|