C#asp.net图表控件,获取数据点的饼图切片颜色

时间:2013-09-06 12:30:50

标签: c# asp.net asp.net-charts

我有一个控制系统,有一个Pie系列。有没有办法检索系列的DataBound事件中每个数据点的饼图切片颜色?任何建议都表示赞赏。

我需要的原因是我需要为每个数据点显示大量其他数据。我不想把它放在图例中,因为它占用了太多的空间,将来可能还会显示很多其他数据。

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>

文件背后的代码是:

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 个答案:

答案 0 :(得分:1)

您是否尝试为图表设置自定义调色板?

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

您现在可以将颜色数组用于将来的操作。第一个切片的颜色是myColors [0]等。