天天看點

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"

                    <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="-90" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"

                </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"

                    <FontSpec Angle="-180" Family="Arial" FontColor="Black" IsBold="True" IsItalic="False"

                        <FontSpec Angle="90" Family="Arial" FontColor="Black" IsBold="False" IsItalic="False"

                </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">

                            IsVisible="True" RangeMax="0" RangeMin="0" Type="Solid" />

                        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 市";

            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);

    //根據市名代碼擷取相應的中文名稱

    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;

        string strSQL = "select Menu_ID from UDS_Menu where Menu_Name='" + name + "'";

            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];

            // 标題

            myPane.Title.Text = "安徽省"+year+"年各市項目個數(個/市)";

            myPane.XAxis.Title.Text = "市名";

            myPane.YAxis.Title.Text = "項目數目(個)";

            // 坐标對集

            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"); //在柱狀圖上方顯示統計數

            myPane.Title.Text = "安徽省"+year+"年各市項目總投資(萬元/市)";

            myPane.YAxis.Title.Text = "項目總投資(萬元)";

            string strSQL = "select distinct(市) as city,sum(投資) as 投資 from mb_proj_tj where 年份='"+year+"' group by 市";

                list.Add(Convert.ToDouble(i), Convert.ToDouble(ds.Tables[0].Rows[i]["投資"]));

            BarItem myBar = myPane.AddBar("項目總投資(萬元)", list, Color.Blue);

            BarItem.CreateBarLabels(myPane, false, "0.0000"); //在柱狀圖上方顯示統計數

示例效果如下,不過有個缺點就是x軸文字不能豎直水準排列,很是不好,搜尋許久,也沒找到解決辦法:

asp.net中zedgraph柱狀圖的使用總結

繼續閱讀