asp.net 实现柱状图和饼状图

调用页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="画图_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <img id="code" src="char.aspx" align="absmiddle"  style="cursor: pointer;" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;


public partial class 画图_char : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 在此处放置用户代码以初始化页面
        Bitmap objBitMap = new Bitmap(400, 200);
        Graphics objGraphics;
        objGraphics = Graphics.FromImage(objBitMap);
        objGraphics.Clear(Color.White);
        int[] arrValues = { 100, 135, 115, 125, 75, 120 };
        string[] arrValueNames = new string[] { "一月", "二月", "三月", "四月", "五月", "六月" };
        objGraphics.DrawString(" X 公司上半年销售情况",
            new Font("宋体", 16), Brushes.Black, new PointF(5, 5));
        PointF symbolLeg = new PointF(335, 20);
        PointF descLeg = new PointF(360, 16);
        //右边月份不同颜色表示
        for (int i = 0; i < arrValueNames.Length; i++)
        {
            objGraphics.FillRectangle(new SolidBrush(GetColor(i)), symbolLeg.X, symbolLeg.Y, 20, 10);
            objGraphics.DrawRectangle(Pens.Black, symbolLeg.X, symbolLeg.Y, 20, 10);
            objGraphics.DrawString(arrValueNames[i].ToString(), new Font("宋体", 10), Brushes.Black, descLeg);
            symbolLeg.Y += 15;
            descLeg.Y += 15;
        }
        //竖直条
        for (int i = 0; i < arrValues.Length; i++)
        {
            objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 200 - arrValues[i], 20,
                arrValues[i] + 5);
            objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 - arrValues[i], 20, arrValues[i] + 5);
        }
        float sglCurrentAngle = 0;
        float sglTotalAngle = 0;
        float sglTotalValues = 0;
        for (int i = 0; i <= arrValues.Length - 1; i++)
        {
            sglTotalValues += arrValues[i];
        }
        //饼状图
        for (int i = 0; i < arrValues.Length; i++)
        {
            sglCurrentAngle = arrValues[i] / sglTotalValues * 360;
            objGraphics.FillPie(new SolidBrush(GetColor(i)), 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
            objGraphics.DrawPie(Pens.Black, 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
            sglTotalAngle += sglCurrentAngle;
        }
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        objBitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray()); 

    }

    private Color GetColor(int itemIndex)
    {
        Color objColor;
        if (itemIndex == 0)
        {
            objColor = Color.Blue;
        }
        else if (itemIndex == 1)
        {
            objColor = Color.Red;
        }
        else if (itemIndex == 2)
        {
            objColor = Color.Yellow;
        }
        else if (itemIndex == 3)
        {
            objColor = Color.Purple;
        }
        else if (itemIndex == 4)
        {
            objColor = Color.Orange;
        }
        else if (itemIndex == 5)
        {
            objColor = Color.Brown;
        }
        else if (itemIndex == 6)
        {
            objColor = Color.Gray;
        }
        else if (itemIndex == 7)
        {
            objColor = Color.Maroon;
        }
        else if (itemIndex == 8)
        {
            objColor = Color.Maroon;
        }
        else
        {
            objColor = Color.Blue;
        }
        return objColor;
    }


}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值