asp.net中繪製柱狀圖

來源:互聯網
上載者:User
  圖形的繪製涉及到的類並不是很多,如果要繪製圖形,特別是這種資料來源動態變化的圖形,座標位置的控制非常關鍵。
  這個Demo採用oracle資料庫為資料來源:
 

CREATE TABLE IVAN_TEST
(
  MAJOR  VARCHAR2(50),
  GRADE  NUMBER,
  ID     NUMBER(10)
)

Html中加入:

 <asp:Button ID="btnColumn" runat="server" Text="DrawColumn" OnClick="btnColumn_Click" /><br />
        <asp:Image ID="imgColumn" runat="server" />

CS:()protected void btnColumn_Click(object sender, EventArgs e)
    ...{
        Bitmap img = new Bitmap(300,300);
        Graphics g = Graphics.FromImage(img);
         StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
        StringFormat drawFormat1 = new System.Drawing.StringFormat(StringFormatFlags.DisplayFormatControl);
        g.Clear(ColorTranslator.FromHtml("#F0F0F0"));
        g.DrawString("Student Grade Column Chart", new Font("Arial",1, FontStyle.Bold),Brushes.Black , 100, 0,drawFormat1);

        //get datasource
        string sql = "select * from ivan_test";
        DAL.ISDApp01 cDal = new DAL.ISDApp01();
        DataSet ds = cDal.ExecuteQuery(sql);


        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        ...{
            int grade = Convert.ToInt32(ds.Tables[0].Rows[i][1]);  //grade
            string Major = ds.Tables[0].Rows[i][0].ToString();

           //注意座標的控制
            g.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 150 - grade, 20,
            grade+50 );

            g.DrawRectangle(new Pen(Color.Black), (i * 35) + 15, 150 - grade, 20,
            grade + 50);

            g.DrawString(Major, new Font("Arial", 12, FontStyle.Bold), Brushes.Red, (i * 35) + 20, 200,drawFormat);
            
            g.DrawString(grade.ToString(), new Font("Arial", 12, FontStyle.Bold), Brushes.Red, (i * 35) + 20, 130 - grade );
 
        }
         //show chart

        string Filepath = Server.MapPath("Images") + "//" + "Column.jpg";
        if (File.Exists(Filepath))
        ...{
            File.Delete(Filepath);
        }
        img.Save(Filepath, ImageFormat.Jpeg);
        img.Dispose();
        g.Dispose();
        this.imgColumn.ImageUrl = Filepath;
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.