How to color each piece in a pie chart ?

Thread is closed for posting
2 posts, 0 answers
  1. Alexandre
    Alexandre avatar
    7 posts
    Member since:
    Jun 2013

    Posted 14 Jun 2013 Link to this post

    Hi,

    Im using the following pie chart mark:

    <telerik:RadHtmlChart ID="rhcGraficoResumoLeitos" SkinsOverrideStyles="true" runat="server" Width="350px" Height="300px">
                                            <ChartTitle Text="Ocupação dos leitos">
                                            </ChartTitle>
                                            <PlotArea>
                                                <Series>
                                                    <telerik:PieSeries DataFieldY="Quantidade" ColorField="Cor" NameField="Situação" ExplodeField="IsExploded">
                                                        <LabelsAppearance DataFormatString="{0:N0}">
                                                        </LabelsAppearance>
                                                    </telerik:PieSeries>
                                                </Series>
                                            </PlotArea>
                                        </telerik:RadHtmlChart>

    And code behind:

    private void CarregarResumoLeitos()
            {
                rhcGraficoResumoLeitos.DataSource = GetData();
                rhcGraficoResumoLeitos.DataBind();
            }
     
            private DataSet GetData()
            {
                ControladorInternacao controladorInternacao = new ControladorInternacao();
                List<SalaAtendimentoDTO> listaSalaAtendimento = controladorInternacao.ListarResumoLeitos(AmbienteConexao.Usuario().Cnes);
     
                Color[] barColors = new Color[8]{
                   Color.Purple,
                   Color.SteelBlue,
                   Color.Aqua,
                   Color.Yellow,
                   Color.Navy,
                   Color.Green,
                   Color.Blue,
                   Color.Red
               };
     
                DataSet ds = new DataSet("Estabelecimento");
                DataTable dt = new DataTable("SalaAtendimento");
     
                dt.Columns.Add("Gênero");
                dt.Columns.Add("Situação");
                dt.Columns.Add("Quantidade");
                dt.Columns.Add("Cor");
                dt.Columns.Add("IsExploded");
     
                int i = 0;
                foreach (SalaAtendimentoDTO salaAtendimentoDTO in listaSalaAtendimento)
                {
                    dt.Rows.Add(salaAtendimentoDTO.Genero == string.Empty ? "teste" : salaAtendimentoDTO.Genero, salaAtendimentoDTO.SituacaoSala, salaAtendimentoDTO.Quantidade.ToString(), barColors[i++], true);
                }
     
                ds.Tables.Add(dt);
                return ds;
            }
     
            private static Color CorAleatoria()
            {
                Random randonGen = new Random();
                KnownColor[] names = (KnownColor[]) Enum.GetValues(typeof(KnownColor));
                KnownColor randomColorName = names[randonGen.Next(names.Length)];
                Color randomColor = Color.FromKnownColor(randomColorName);
                return randomColor;
            }


    But im not able to color each item in the pie chart.

    Can anyone help me please ?

    Thanks in advance.
  2. Danail Vasilev
    Admin
    Danail Vasilev avatar
    1493 posts

    Posted 19 Jun 2013 Link to this post

    Hi Alexandre,

    You must use the ToKnownColor() method in order to convert the color structure to a known color. For example:
    foreach (SalaAtendimentoDTO salaAtendimentoDTO in listaSalaAtendimento)
    {
        dt.Rows.Add(salaAtendimentoDTO.Genero == string.Empty ? "teste" : salaAtendimentoDTO.Genero, salaAtendimentoDTO.SituacaoSala, salaAtendimentoDTO.Quantidade.ToString(), barColors[i++].ToKnownColor(), true);
    }

    You can also find a modified version of your example with this approach in the attached archive.

    I have also changed the product of this forum thread as it is related to RadHtmlChart and not RadChart.

     

    Regards,
    Danail Vasilev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top