簡體   English   中英

C#asp.net圖表控件,獲取數據點的餅圖顏色

[英]C# asp.net chart control, Get the pie slice color of data points

我有一個帶有Pie系列的控件。 有沒有一種方法可以檢索Series的DataBound事件中每個數據點的扇形切片顏色? 任何建議表示贊賞。

我需要這樣做的原因是,我需要為每個數據點顯示很多其他數據。 我不想將其放在圖例中,因為它占用了太多空間,並且將來可能還會顯示許多其他數據。

在此處輸入圖片說明

在此處輸入圖片說明

                <asp:Chart ID="TRsClosedByDevelopers" runat="server" Height="892px"
                    BorderlineDashStyle="Solid" BorderlineWidth="2" BorderSkin-SkinStyle="Emboss"
                    BorderlineColor="BlueViolet" Width="980px" Palette="Pastel"
                    style="margin-left:-8px; margin-top:-8px;" EnableViewState="True" 
                    SuppressExceptions="True" ondatabound="TRsClosedByDevelopers_DataBound">

                    <Titles>

                        <asp:Title BackColor="LightSteelBlue" BackGradientStyle="VerticalCenter" 
                            BorderColor="SteelBlue" BorderWidth="0" 
                            Font="Microsoft Sans Serif, 15.75pt, style=Bold" ForeColor="DarkSlateGray" 
                            IsDockedInsideChartArea="False" Name="PieChartTitle" 
                            Text="TR's Closed in the past ">
                        </asp:Title>

                    </Titles>

                    <Series>
                        <asp:Series Name="Testing1"
                            Font="Microsoft Sans Serif, 9.75pt, style=Bold" BorderColor="0, 0, 192" 
                            ChartType="Pie" LabelForeColor="DarkSlateGray" 
                            CustomProperties="MinimumRelativePieSize=50, CollectedSliceExploded=True, CollectedLegendText=OTHER, CollectedColor=ActiveBorder, CollectedLabel=OTHER   (#VALY)  (#PERCENT{P2}), PieLabelStyle=Outside, CollectedThreshold=1" 
                            YValueType="Int32" Label="#VALX"
                            XValueType="String">

                            <SmartLabelStyle CalloutLineAnchorCapStyle="Diamond" 
                                AllowOutsidePlotArea="Yes" />

                            <EmptyPointStyle IsValueShownAsLabel="false" IsVisibleInLegend="false" 
                                BorderWidth="0" CustomProperties="PieLabelStyle=Disabled" MarkerBorderWidth="0" 
                                MarkerSize="0" />

                        </asp:Series>
                    </Series>

                    <ChartAreas>
                        <asp:ChartArea Name="ChartArea2">
                            <Area3DStyle Enable3D="true" Inclination="0" />
                        </asp:ChartArea>
                    </ChartAreas>

                    <BorderSkin SkinStyle="Emboss" />

                </asp:Chart>
            </td>
            <td>
                <table cellspacing="2" cellpadding="2" style="width:100%; margin-left:-2px;">
                    <tr style="background-color:#cccccc; font-size:large;">
                        <th style="width:30px;">
                            &nbsp;
                        </th>
                        <th align="left" style="width:260px;">
                            Developer
                        </th>
                        <th>
                            TR's Closed
                        </th>
                    </tr>
                </table>

                <asp:Repeater ID="TRsClosedTbl" runat="server" OnItemDataBound="TRsClosedTbl_ItemDataBound">                                                
                    <HeaderTemplate>
                        <table cellspacing="2" cellpadding="2" style="width:100%; margin-left:-2px;" id="SummaryTbl">
                    </HeaderTemplate>

                    <ItemTemplate>
                        <tr style="background-color:#eeeeee; height:24px; font-size:large;" class="normalCell">
                            <td style="width:30px;" runat="server" id="ColorCell">&nbsp;</td>
                            <td style="width:260px;"><%#((DataRowView)Container.DataItem)["Developer"]%></td>
                            <td align="right" style="width:50px"><%#((DataRowView)Container.DataItem)["TRsClosed"]%></td>
                            <td align="right"><%#String.Format("{0:N2}", Math.Round(Convert.ToDouble(((DataRowView)Container.DataItem)["TRsClosed"])/Convert.ToDouble(srv_nTotalTRsClosed) * 100, 2))%> %</td>
                        </tr>
                    </ItemTemplate>

                    <AlternatingItemTemplate>
                        <tr style="background-color:#eeeeff; height:24px; font-size:large;" class="normalCell">
                            <td style="width:30px;" runat="server" id="ColorCell">&nbsp;</td>
                            <td style="width:260px;"><%#((DataRowView)Container.DataItem)["Developer"]%></td>
                            <td align="right" style="width:50px"><%#((DataRowView)Container.DataItem)["TRsClosed"]%></td>
                            <td align="right"><%#String.Format("{0:N2}", Math.Round(Convert.ToDouble(((DataRowView)Container.DataItem)["TRsClosed"])/Convert.ToDouble(srv_nTotalTRsClosed) * 100, 2))%> %</td>
                        </tr>
                    </AlternatingItemTemplate>

                    <FooterTemplate>                                                        
                            <tr style="background-color:#EEFFCC; height:24px; font-size:large;" class="normalCell">
                                <td align="right" colspan="2">TOTAL:</td>
                                <td align="right"><%=srv_nTotalTRsClosed%></td>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </td>
        </tr>
    </table>

文件后面的代碼是:

public partial class ER_RecentTRsClosedPieChart : System.Web.UI.Page
{
    public DataAccessLayer DAL;
    public DataSet srv_TRsClosedByDevDS;

    public string srv_sToday;
    public int srv_nNumDays = 0;
    public int srv_nTotalTRsClosed = 0;

    private int index = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        DAL = new DataAccessLayer();
        srv_TRsClosedByDevDS = new DataSet();

        srv_sToday = "09/14/2012";//DateTime.Now.ToString("MM/dd/yyyy");

        if (!int.TryParse(Request.QueryString["NumDays"], out srv_nNumDays))
            srv_nNumDays = 7;

        srv_TRsClosedByDevDS = DAL.CRM_RequestMQEx(srv_nNumDays, 0, 0, 0, 0, srv_sToday, "", "", "", "", 13);

        srv_nTotalTRsClosed = SumColumn(srv_TRsClosedByDevDS.Tables[0], "TRsClosed");

        TRsClosedByDevelopers.Titles[0].Text += srv_nNumDays.ToString() + " Days    (TOTAL: " + srv_nTotalTRsClosed.ToString() + ")";

        TRsClosedByDevelopers.Series["Testing1"].Points.DataBind(srv_TRsClosedByDevDS.Tables[0].AsEnumerable(), "Developer", "TRsClosed", string.Empty);

        srv_TRsClosedByDevDS.Tables[0].Columns.Add("PieColor");

        TRsClosedTbl.DataSource = srv_TRsClosedByDevDS.Tables[0];
        TRsClosedTbl.DataBind();
    }

    public int SumColumn(DataTable dt, string columnName)
    {
        return (from c in dt.AsEnumerable()
                where !c.IsNull(columnName)
                select c.Field<int>(columnName)
                ).Sum();
    }

    protected void TRsClosedTbl_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    protected void TRsClosedByDevelopers_DataBound(object sender, EventArgs e)
    {
        string r;
        string g;
        string b;

        for (int i = 0; i < TRsClosedByDevelopers.Series["Testing1"].Points.Count; i++)
        {
            string dev = TRsClosedByDevelopers.Series["Testing1"].Points[i].Color.ToString();

            //Here, I Want to add the rendered Pie Slice color to the DataColumn "PieColor" of each row.

            //r = TRsClosedByDevelopers.Series["Testing1"].Points[i].MarkerColor.R.ToString();
            //g = TRsClosedByDevelopers.Series["Testing1"].Points[i].MarkerColor.G.ToString();
            //b = TRsClosedByDevelopers.Series["Testing1"].Points[i].MarkerColor.B.ToString();
        }
    }
}

您是否嘗試過為圖表設置自定義調色板?

chart.Palette = ChartColorPalette.None;
Colors[] myColors = {Color.Red, Color.Green, Color.Blue, ...};
chart.PaletteCustomColors = myColors;

現在,您可以將顏色陣列用於將來的操作。 第一個切片的顏色是myColors [0]等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM