一个月又过去了,总是不轻松啊。接着来[MES]制造执行系统(五)后续....好事总是不间段,接连发生。国庆都不得安宁。B40生产现场需求。为了提供管理希望在看板程式上显示每台热压机的良率变化曲线图。

 

好不容易使系统稳定下来,心里打算终于可以喘口气了。想不的需求又来了。咳,真是没办法。User第一。只能够想办法...找资料...测试...

看看之前的看板样式:

先想到的.net的GDI+,因为之前也有碰到过画图程式,但那是C/S。我想B/S应该也可以吧。所以网上baidu,google一下在说。网页画图。有了MSChart量身打造的。有感兴趣的同学可以参考: 

http://code.msdn.microsoft.com/mschart 。当然我也看了下,但需要:.NET Framework 3.5 sp1可以安裝一個插件,可以產生圖檔.NET Framework 4.0已經有內建囉,可以直接用。还是比较麻烦。还是算了。


 

 还要安装就免了,用自己比较熟悉的GDI+吧。最后实现效果图如下:

是刚刚抓的。

关键代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Drawing.Imaging;
using System.Drawing; 
/// <summary>
/// DrawingCurve 的摘要描述
/// </summary>
public class DrawingCurve
{
	public int intXLong = 465;   //图片大小 长
    public int intYLong = 45;   //图片大小 高
    public int intXMultiple = 1;    //零刻度的值 X
    public int intYMultiple = 80;    //零刻度的值 Y
    public int intXMax = 29;    //最大刻度(点数) X
    public int intYMax = 100;    //最大刻度(点数) Y

    public int intLeft = 0;   //左边距
    public int intRight = 0; //右边距
    public int intTop = 0;    //上边距
    public int intEnd = 0;    //下边距

    public string strXText = "";//"时间(单位:)";    //单位 X
    public string strYText = "";//"百分比(单位:)";    //单位 Y
    public string strTitle = "";//"趋势线图";    //标题
    public DataTable tbData;    //要统计的数据


    private int intXScale;// = 30;    //一刻度长度 X
    private int intYScale;// = 30;    //一刻度高度 Y
    //private int intX = 0;   //0点 X坐标
    //private int intY = 0;   //0点 Y坐标
    public int intData = 0;    //记录数

    public DrawingCurve()
    {

        intXScale = (intXLong - intLeft - intRight) / (intXMax - intXMultiple + 1);//一刻度长度 X
        intYScale = (intYLong - intTop - intEnd) / (intYMax - intYMultiple + 1);//一刻度高度 Y

        //intX = intXLong - intLeft;   //0点 X坐标
        //intY = intYLong - intEnd;   //0点 Y坐标
    }

    public Bitmap DrawingImg()
    {

        Bitmap img = new Bitmap(intXLong, intYLong); //图片大小
        Graphics g = Graphics.FromImage(img);
        g.Clear(Color.Snow);
        g.DrawString(strTitle, new Font("宋体", 14), Brushes.Black, new Point(5, 5));
        g.DrawLine(new Pen(Color.Black, 2), intLeft, intYLong - intEnd, intXLong - intRight, intYLong - intEnd); //绘制横向
        g.DrawLine(new Pen(Color.Black, 2), intLeft, intTop, intLeft, intYLong - intEnd);   //绘制纵向

        //绘制纵坐标
        g.DrawString(strYText, new Font("宋体", 12), Brushes.Black, new Point(intLeft, intTop));//Y 单位
        Point p1 = new Point(intLeft - 10, intYLong - intEnd);
        for (int j = 0; j <= intYMax; j++)
        {
            p1.Y = intYLong - intEnd - j * intYScale;
            Point pt = new Point(p1.X + 10, p1.Y);
            //绘制纵坐标的刻度和直线
            g.DrawLine(Pens.Black, pt, new Point(p1.X + 15, p1.Y));
            //绘制纵坐标的文字说明
            g.DrawString(Convert.ToString(j + intYMultiple), new Font("宋体", 12), Brushes.Black, new Point(p1.X - 25, p1.Y - 8));
        }

        //绘制横坐标
        g.DrawString(strXText, new Font("宋体", 12), Brushes.Black, new Point(intXLong - intRight, intYLong - intEnd));//X 单位
        Point p = new Point(intLeft, intYLong - intEnd);
        for (int i = 0; i < intXMax; i++)
        {
            p.X = intLeft + i * intXScale;
            //绘制横坐标刻度和直线
            g.DrawLine(Pens.Black, p, new Point(p.X, p.Y - 5));
            //绘制横坐标的文字说明
            g.DrawString(Convert.ToString(i + intXMultiple), new Font("宋体", 12), Brushes.Black, p);
        }


        intData = tbData.Rows.Count;
        if (intData > 0)
        {
            //趋势线图
            for (int i = 0; i < intData - 1; i++)
            {
                DataRow Row1 = tbData.Rows[i];
                DataRow Row2 = tbData.Rows[i + 1];
                //定义起点
                Point rec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row1[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row1[1].ToString()) - intYMultiple) * intYScale));
                //定义终点
                Point dec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row2[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row2[1].ToString()) - intYMultiple) * intYScale));
                //绘制趋势折线
                g.DrawLine(new Pen(Color.Red), rec, dec);
            }
        }

        return img;
    }

    //转换数字
    private double TurnNumber(string str)
    {
        double dubReturn;
        try
        {
            dubReturn = Convert.ToDouble(str);
        }
        catch
        {
            dubReturn = 0;
        }
        return dubReturn;

    }
}

当然,我不能够用简单的,不要耗我服务器资源咯。要不又跨了怎么办啊。所以,我都放在客户端的。呵呵,安全,稳定第一。