asp.net中zedgraph柱状图的使用总结

    <div>
        <table>
            <tr>
                <td>选择年份</td>
                <td><asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True"></asp:DropDownList></td>
                <td>统计内容</td>
                <td><asp:DropDownList ID="ddlType" runat="server" AutoPostBack="True">
                    <asp:ListItem>项目个数</asp:ListItem>
                    <asp:ListItem>项目总投资</asp:ListItem>
                </asp:DropDownList></td>
            </tr>
        </table>
        <asp:Panel ID="p1" runat="server" Width="100%">
            <cc1:zedgraphweb id="ZedGraphWeb1" runat="server" OnRenderGraph="ZedGraphWeb1_RenderGraph1" Height="400" Width="800" ChartBorder-IsVisible="false" XAxis-IsTicsBetweenLabels="false">
                <XAxis AxisColor="Black" Cross="0" CrossAuto="True" IsOmitMag="False" IsPreventLabelOverlap="True"
                    IsShowTitle="True" IsTicsBetweenLabels="True" IsUseTenPower="False" IsVisible="True"
                    IsZeroLine="False" MinSpace="0" Title="" Type="Linear">
                    <FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
                        IsUnderline="False" Size="14" StringAlignment="Center">
                        <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                        <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                            IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                    </FontSpec>
                    <MinorGrid Color="Black" DashOff="5" DashOn="1" IsVisible="False" PenWidth="1" />
                    <MajorGrid Color="Black" DashOff="5" DashOn="1" IsVisible="False" PenWidth="1" />
                    <MinorTic Color="Black" IsInside="True" IsOpposite="True" IsOutside="True" PenWidth="1"
                        Size="5" />
                    <MajorTic Color="Black" IsInside="True" IsOpposite="True" IsOutside="True" PenWidth="1"
                        Size="5" />
                    <Scale Align="Center" Format="g" FormatAuto="True" IsReverse="False" Mag="0" MagAuto="True"
                        MajorStep="1" MajorStepAuto="True" MajorUnit="Day" Max="0" MaxAuto="True" MaxGrace="0.1"
                        Min="0" MinAuto="True" MinGrace="0.1" MinorStep="1" MinorStepAuto="True" MinorUnit="Day">
                        <FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
                            IsUnderline="False" Size="14" StringAlignment="Center">
                            <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                            <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                                IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                        </FontSpec>
                    </Scale>
                </XAxis>
                <Y2Axis AxisColor="Black" Cross="0" CrossAuto="True" IsOmitMag="False" IsPreventLabelOverlap="True"
                    IsShowTitle="True" IsTicsBetweenLabels="True" IsUseTenPower="False" IsVisible="False"
                    IsZeroLine="True" MinSpace="0" Title="" Type="Linear">
                    <FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
                        IsUnderline="False" Size="14" StringAlignment="Center">
                        <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                        <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                            IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                    </FontSpec>
                    <MinorGrid Color="Black" DashOff="5" DashOn="1" IsVisible="False" PenWidth="1" />
                    <MajorGrid Color="Black" DashOff="5" DashOn="1" IsVisible="False" PenWidth="1" />
                    <MinorTic Color="Black" IsInside="True" IsOpposite="True" IsOutside="True" PenWidth="1"
                        Size="5" />
                    <MajorTic Color="Black" IsInside="True" IsOpposite="True" IsOutside="True" PenWidth="1"
                        Size="5" />
                    <Scale Align="Center" Format="g" FormatAuto="True" IsReverse="False" Mag="0" MagAuto="True"
                        MajorStep="1" MajorStepAuto="True" MajorUnit="Day" Max="0" MaxAuto="True" MaxGrace="0.1"
                        Min="0" MinAuto="True" MinGrace="0.1" MinorStep="1" MinorStepAuto="True" MinorUnit="Day">
                        <FontSpec Angle="-90" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
                            IsUnderline="False" Size="14" StringAlignment="Center">
                            <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                            <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                                IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                        </FontSpec>
                    </Scale>
                </Y2Axis>
                <FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
                    IsUnderline="False" Size="16" StringAlignment="Center">
                    <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                    <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                        IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                </FontSpec>
                <MasterPaneFill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100"
                    IsScaled="True" IsVisible="True" RangeMax="0" RangeMin="0" Type="Solid" />
                <YAxis AxisColor="Black" Cross="0" CrossAuto="True" IsOmitMag="False" IsPreventLabelOverlap="True"
                    IsShowTitle="True" IsTicsBetweenLabels="True" IsUseTenPower="False" IsVisible="True"
                    IsZeroLine="True" MinSpace="0" Title="" Type="Linear">
                    <FontSpec Angle="-180" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"
                        IsUnderline="False" Size="14" StringAlignment="Center">
                        <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                        <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                            IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                    </FontSpec>
                    <MinorGrid Color="Black" DashOff="5" DashOn="1" IsVisible="False" PenWidth="1" />
                    <MajorGrid Color="Black" DashOff="5" DashOn="1" IsVisible="False" PenWidth="1" />
                    <MinorTic Color="Black" IsInside="True" IsOpposite="True" IsOutside="True" PenWidth="1"
                        Size="5" />
                    <MajorTic Color="Black" IsInside="True" IsOpposite="True" IsOutside="True" PenWidth="1"
                        Size="5" />
                    <Scale Align="Center" Format="g" FormatAuto="True" IsReverse="False" Mag="0" MagAuto="True"
                        MajorStep="1" MajorStepAuto="True" MajorUnit="Day" Max="0" MaxAuto="True" MaxGrace="0.1"
                        Min="0" MinAuto="True" MinGrace="0.1" MinorStep="1" MinorStepAuto="True" MinorUnit="Day">
                        <FontSpec Angle="90" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
                            IsUnderline="False" Size="14" StringAlignment="Center">
                            <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                            <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                                IsVisible="True" RangeMax="0" RangeMin="0" Type="None" />
                        </FontSpec>
                    </Scale>
                </YAxis>
                <Legend IsHStack="True" IsReverse="False" IsVisible="True" Position="Top">
                    <Location AlignH="Left" AlignV="Center" CoordinateFrame="ChartFraction" Height="0"
                        Width="0" X="0" Y="0">
                        <TopLeft X="0" Y="0" />
                        <BottomRight X="0" Y="0" />
                    </Location>
                    <FontSpec Angle="0" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"
                        IsUnderline="False" Size="12" StringAlignment="Center">
                        <Border Color="Black" InflateFactor="0" IsVisible="False" Width="1" />
                        <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                            IsVisible="True" RangeMax="0" RangeMin="0" Type="Solid" />
                    </FontSpec>
                    <Fill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                        IsVisible="True" RangeMax="0" RangeMin="0" Type="Brush" />
                    <Border Color="Black" InflateFactor="0" IsVisible="True" Width="1" />
                </Legend>
                <PaneFill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                    IsVisible="True" RangeMax="0" RangeMin="0" Type="Solid" />
                <ChartFill AlignH="Center" AlignV="Center" Color="White" ColorOpacity="100" IsScaled="True"
                    IsVisible="True" RangeMax="0" RangeMin="0" Type="Brush" />
                <ChartBorder Color="Black" InflateFactor="0" IsVisible="True" Width="1" />
                <MasterPaneBorder Color="Black" InflateFactor="0" IsVisible="True" Width="1" />
                <Margins Bottom="10" Left="10" Right="10" Top="10" />
                <PaneBorder Color="Black" InflateFactor="0" IsVisible="True" Width="1" />
            </cc1:zedgraphweb>
        </asp:Panel>
    </div>

----------------------------------------------------------------------------------------------------------------------------------------------------

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DM dm = new DM();
            string strSQL = "select distinct(年份) as year from mb_proj_tj";
            DataSet ds = dm.GetData(strSQL);
            this.ddlYear.DataSource = ds;
            this.ddlYear.DataTextField = "year";
            this.ddlYear.DataValueField = "year";
            this.ddlYear.DataBind();
        }
    }

    //获取数据
    private DataSet GetData(string year,string cityName)
    {
        if (this.ddlType.SelectedItem.Value == "项目个数")
        {
            string strSQL = "select distinct(市) as city,count(*) as total from mb_proj_tj where 年份='" + year + "' group by 市";
            DM dm = new DM();
            DataSet ds = dm.GetData(strSQL);
            return ds;
        }
        else
        {
            string strSQL = string.Format(@"select distinct(市) as city,sum(投资) as 投资 from mb_proj_tj where 年份='{0}' and 市='{1}' group by 市", year, cityName);
            DM dm = new DM();
            DataSet ds = dm.GetData(strSQL);
            return ds;
        }
    }

    //根据市名代码获取相应的中文名称
    public string getCityChineseName(string code)
    {
        string ret = string.Empty;
        DM dm = new DM();
        string strSQL = "select Menu_Name from UDS_Menu where Menu_ID='" + code.Substring(0, 6).Trim() + "'";
        DataSet ds = dm.GetData(strSQL);
        foreach (DataRowView drv in ds.Tables[0].DefaultView)
        {
            ret = drv["Menu_Name"].ToString();
        }
        return ret;
    }

    //根据市名相应的中文名称获取所对应的代码
    public string getCityCode(string name)
    {
        string code = string.Empty;
        DM dm = new DM();
        string strSQL = "select Menu_ID from UDS_Menu where Menu_Name='" + name + "'";
        DataSet ds = dm.GetData(strSQL);
        foreach (DataRowView drv in ds.Tables[0].DefaultView)
        {
            code = drv["Menu_ID"].ToString().Trim() + "000000";
        }
        return code;
    }

    protected void ZedGraphWeb1_RenderGraph1(ZedGraphWeb webObject, Graphics g, MasterPane pane)
    {
        string year = this.ddlYear.SelectedItem.Text;
        GraphPane myPane = pane[0];
        if (this.ddlType.SelectedItem.Value == "项目个数")
        {
            // 标题
            myPane.Title.Text = "安徽省"+year+"年各市项目个数(个/市)";
            myPane.XAxis.Title.Text = "市名";
            myPane.YAxis.Title.Text = "项目数目(个)";
 
            string strSQL = "select distinct(市) as city,count(*) as total from mb_proj_tj where 年份='" + year + "' group by 市";
            DM dm = new DM();
            DataSet ds = dm.GetData(strSQL);
            // 坐标对集
            PointPairList list = new PointPairList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(Convert.ToDouble(i), Convert.ToDouble(ds.Tables[0].Rows[i]["total"]));
            }
            BarItem myBar = myPane.AddBar("项目数目(个)", list, Color.Blue);
            myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);
            myPane.XAxis.MajorTic.IsBetweenLabels = true;
            // X轴Label
            string[] labels = new string[ds.Tables[0].Rows.Count];
            ArrayList al = new ArrayList();
            foreach (DataRowView drv in ds.Tables[0].DefaultView)
            {
                al.Add(getCityChineseName(drv["city"].ToString()));
            }
            for (int i = 0; i < al.Count; i++)
            {
                labels[i] = al[i].ToString();
            }
            //设置x轴刻度
            myPane.XAxis.Scale.TextLabels = labels;
            myPane.XAxis.Type = AxisType.Text;
            myPane.XAxis.Scale.FontSpec.Angle = 270;
            // 颜色填充
            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
            myPane.BarSettings.ClusterScaleWidth = 1.0;//这是来调整柱型的宽度的
            pane.AxisChange(g);
            myPane.GetImage(800, 600, 1000);
            myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;
            BarItem.CreateBarLabels(myPane, false, "f0"); //在柱状图上方显示统计数
        }
        else
        {
            // 标题
            myPane.Title.Text = "安徽省"+year+"年各市项目总投资(万元/市)";
            myPane.XAxis.Title.Text = "市名";
            myPane.YAxis.Title.Text = "项目总投资(万元)";
            string strSQL = "select distinct(市) as city,sum(投资) as 投资 from mb_proj_tj where 年份='"+year+"' group by 市";
            DM dm = new DM();
            DataSet ds = dm.GetData(strSQL);
            // 坐标对集
            PointPairList list = new PointPairList();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                list.Add(Convert.ToDouble(i), Convert.ToDouble(ds.Tables[0].Rows[i]["投资"]));
            }

            BarItem myBar = myPane.AddBar("项目总投资(万元)", list, Color.Blue);

            myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);

            myPane.XAxis.MajorTic.IsBetweenLabels = true;
            // X轴Label
            string[] labels = new string[ds.Tables[0].Rows.Count];
            ArrayList al = new ArrayList();
            foreach (DataRowView drv in ds.Tables[0].DefaultView)
            {
                al.Add(getCityChineseName(drv["city"].ToString()));
            }
            for (int i = 0; i < al.Count; i++)
            {
                labels[i] = al[i].ToString();
            }
            //设置x轴刻度
            myPane.XAxis.Scale.TextLabels = labels;
            myPane.XAxis.Type = AxisType.Text;
            myPane.XAxis.Scale.FontSpec.Angle = 270;
            // 颜色填充
            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
            myPane.BarSettings.ClusterScaleWidth = 1.0;//这是来调整柱型的宽度的
            pane.AxisChange(g);
            myPane.GetImage(800, 600, 1000);
            myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;
            BarItem.CreateBarLabels(myPane, false, "0.0000"); //在柱状图上方显示统计数
        }
    }

示例效果如下,不过有个缺点就是x轴文字不能竖直水平排列,很是不好,搜寻许久,也没找到解决办法:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值