日期:2014-05-20  浏览次数:21052 次

owc画折线图问题
怎样才能把折线图做出的效果和Excel中画的折线图一样呢?要做出的效果是:如果数据库中的值为空的时候,则停止画该折线,当下一个数据出现的时候隔几个空的无值的坐标点重新开始画另外一条折线,我做出来的当值出现空的时候就从零开始画了。

------解决方案--------------------
下面是我才写的 就是不连续画线 当Y为空或0时 就停止画线 
C# code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;

public partial class _Default : System.Web.UI.Page
{
    int x = 0;
    int y = 0;
    int x_1_v = 0;
    int y_1_v = 0;
    int x_df = 0;
    int y_df = 0;
    int width = 0;
    int height = 0;
    double y_max = 0;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateImage();
       
    }

    void CreateImage()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        _Init(100, 100, 40*id, 25, 24, 10);
        Bitmap image = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(image);
        Pen pen1 = new Pen(Color.Turquoise, 1);
        Pen pen2 = new Pen(System.Drawing.Color.DimGray, 1);
        g.FillRectangle(Brushes.Black, 0, 0, width, height);
        CreateMap(g, pen1, pen2);
        String[] m = { "100", "90", "80", "70", "60", "50", "40", "30", "20", "10" };
        Font font = new Font("Verdana", 12);
        Brush b = Brushes.PowderBlue;

        Creat_XY_Values(m, b, font, g);
        
        y_max = double.Parse(m[0]);
        DataTable dt = GetTable();
        Pen mypen = new Pen(System.Drawing.Color.MediumSpringGreen, 1);
        Brush bb = Brushes.Chartreuse;
        Font font_line = new System.Drawing.Font("Arial", 10, FontStyle.Bold);

        DrawLine(dt, "value", "time", font_line, bb, mypen, g);


        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());
    
    }
    void DrawLine(DataTable dt, string str_y, string str_x, Font font, Brush brush, Pen pen, Graphics g)
    {
        bool b = true;
        Point[] point = new Point[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (i != 0)
            {
                if (double.Parse(dt.Rows[i][str_y].ToString()) != 0)
                {
                    double xx = ChangeTime(dt.Rows[i][str_x].ToString());
                    //double xx = double.Parse(dt.Rows[i][str_x].ToString());
                    point[i] = Points(xx, int.Parse(dt.Rows[i][str_y].ToString()));
                    //g.DrawString(dt.Rows[i][str_y].ToString(), font, brush, point[i].X - 10, point[i].Y - 20);
                    if (b)
                    {
                        g.DrawLine(pen, point[i - 1], point[i]);

                    }
                    else
                    { b = true; }

                }
                else
                {
                    double xx = ChangeTime(dt.Rows[i][str_x].ToString());
                    //double xx = double.Parse(dt.Rows[i][str_x].ToString());
                    point[i] = Points(xx, int.Parse(dt.Rows[i][str_y].ToString()));
                    b = false;
                }

            }
            else
            {
                double xx = ChangeTime(dt.Rows[i][str_x].ToString());
                //double xx = double.Parse(dt.Rows[i][str_x].ToString());
                point[i] = Points(xx, int.Parse(dt.Rows[i][str_y].ToString()));

            }



        }


    }
    Point Points(double _x, int _y)
    {

        Point p = new Point();
        p.X = (int)(x + x_1_v * _x);
        p.Y = (int)(y + y_1_v * y_df - (_y * (y_1_v * y_df / y_max)));

        return p;

    }
    double ChangeTime(string time)
    {
        string[] s = time.Split(':');
        int x = int.Parse(s[0].ToString());
        double ff = int.Parse(s[1].ToString());
        double mm = int.Parse(s[2].ToString());
        double f = (ff * 60 + mm) / 3600;
        double xx = x + f;
        return xx;


    }
    void _Init(int _x, int _y, int _x_1_v, int _y_1_v, int _x_df, int _y_df)
    {
        x = _x;
        y = _y;
        x_1_v = _x_1_v;
        y_1_v = _y_1_v;
        x_df = _x_df;
        y_df = _y_df;
        width = x + x_1_v * x_df + 100;
        height = y + y_1_v * y_df + 100;

    }
    void CreateMap(Graphics g, Pen pen1, Pen pen2)
    {
        int _x = x;
        int _y = y;
        //X轴网线
        for (int i = 0; i <= x_df; i++)
        {
            if (i == 0 || i == x_df)

                g.DrawLine(pen1, _x, y, _x, y + y_1_v * y_df);
            else
                g.DrawLine(pen2, _x, y, _x, y + y_1_v * y_df);
            _x += x_1_v;

        }
        //Y轴网线
        for (int j = 0; j <= y_df; j++)
        {
            if (j == 0 || j == y_df)
                g.DrawLine(pen1, x, _y, x + x_1_v * x_df, _y);
            else

                g.DrawLine(pen2, x, _y, x + x_1_v * x_df, _y);
            _y += y_1_v;

        }


    }
    void Creat_XY_Values(string[] Y_Values, Brush b, Font font, Graphics g)
    {
        int _x = x - 10;
        int _y = y - 10;
        for (int i = 0; i <= x_df; i++)
        {
            g.DrawString(i.ToString(), font, b, _x, y + y_1_v * y_df);
            _x += x_1_v;
        }
        for (int j = 0; j < Y_Values.Length; j++)
        {
            g.DrawString(Y_Values[j], font, b, x - 11 * Y_Values[j].Length, _y);
            _y += y_1_v;


        }



    }


    DataTable GetTable()
    {
        SqlConnection Con = new SqlConnection("Server=(Local);Database=test;Uid=sa;Pwd=rose");
        Con.Open();
        string cmdtxt2 = "select * from test_2";
        SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Result");
        DataTable dt = ds.Tables["Result"];
        return dt;

    }


}