This is a migrated thread and some comments may be shown as answers.

How to color each piece in a pie chart ?

1 Answer 132 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Alexandre
Top achievements
Rank 1
Alexandre asked on 14 Jun 2013, 04:23 PM
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.

1 Answer, 1 is accepted

Sort by
0
Danail Vasilev
Telerik team
answered on 19 Jun 2013, 03:40 PM
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.
Tags
Chart (Obsolete)
Asked by
Alexandre
Top achievements
Rank 1
Answers by
Danail Vasilev
Telerik team
Share this question
or