0

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();
        }
    }
}
Ren
  • 437
  • 4
  • 17
  • Thanks for the reply Soner, just edited the post. I'd like to know, if it's possible to access the rendered Color of a pie slice.. – Ren Sep 06 '13 at 12:43

1 Answers1

2

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.

Andi Krusch
  • 1,383
  • 8
  • 6
  • Hi Andi, I don't think I made my requirement clear. I need to display the Data Points in a seperate tabular view, outside the chart (instead of a legend). Getting the color of each Data Point would help a lot.. – Ren Sep 06 '13 at 13:26
  • But if you set your own Colors and save that array, you know the color of each slice. Edited my answer. – Andi Krusch Sep 06 '13 at 13:27
  • Well Andi, the number of slices would be unknown. Also, I group the slices which fall below 1% of Y Value to "OTHER". (Plz check the preview attached) – Ren Sep 06 '13 at 13:38
  • Just added two pictures to it. – Ren Sep 06 '13 at 13:47
  • For your chart you use the Pastel palette. The colors for that palette are (in correct order and corresponding http://blogs.msdn.com/b/alexgor/archive/2009/10/06/setting-chart-series-colors.aspx ): 87CEEB,32CD32,BA55D3,F08080,4682B4,9ACD32,40E0D0,FF69B4,F0E68C,D2B48C,8FBC8B,6495ED,DDA0DD,5F9EA0,FFDAB9,FFA07A – Andi Krusch Sep 06 '13 at 13:50