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

3.5k views Asked by At

I have an control, with a Pie Series. Is there a way to retrieve the Pie Slice Color of each Data Point in the DataBound event of the Series? Any suggestion is appreciated.

The reason I need that is that I need to show a whole lot of other data for each Data Point. I don't want to put that in the legend as, it takes too much of space and there could be lot of other data to be displayed in the future.

enter image description here

enter image description here

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

The code behind file is:

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();
        }
    }
}
1

There are 1 answers

5
Andi Krusch On BEST ANSWER

Have you tried to set a custom palette for your chart?

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

You could now use the color array for future operations. the color of the first slice is myColors[0] etc.