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

Set Series Item Name - Legend

1 Answer 158 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 16 Jun 2011, 08:58 PM
I have a chart that is almost generating like I'd like it to. The only thing that is missing is the Labels for the Legend. I currently have the Legend set to display ItemLabels. I thought I set the item labels when I added the item, but apparently not. How do I go about setting these ItemLabels? Below is the code I'm using to build the Chart, and attached is an image of what the chart currently looks like.

reader.GetValue(2) will pull the label text from the database.

CODE
protected void makechart()
        {
            RadChart invChart = new RadChart();
            invChart.ChartTitle.Visible = false;
 
            // Create a ChartSeries and assign its name and chart type
            ChartSeries chartSeries = new ChartSeries();
            chartSeries.Name = "TotalValue";
            chartSeries.Type = ChartSeriesType.Pie;
 
            // Display Item Labels in the Legend
            chartSeries.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels;
             
            // Open SQL Connection
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("GetInventoryValue", connection);
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = command.ExecuteReader();
 
            while (reader.Read())
            {
                // Set value and value text
                chartSeries.AddItem((Double)(reader.GetValue(0)), reader.GetValue(0).ToString());
            }
 
            // Close SQL Connection
            connection.Close();
 
            // add the series to the RadChart Series collection
            invChart.Series.Add(chartSeries);
            // add the RadChart to the page.
            this.invChart.Controls.Add(invChart);
        }
    }

1 Answer, 1 is accepted

Sort by
0
Accepted
HrMeibom
Top achievements
Rank 2
Iron
answered on 20 Jun 2011, 12:41 PM
Hi William,

I believe you'll need to set the name of the series item for each item you add. Something like:

 

 

while (reader.Read()) 
{
    // Set value and value text 
    ChartSeriesItem chartSeriesItem = new ChartSeriesItem((double)reader.GetValue(0), reader.GetValue(0).ToString()); 
    chartSeriesItem.Name = reader.GetValue(2).ToString()
    chartSeries.AddItem(chartSeriesItem);
}

I hope this does the trick... I do have legends on my Pie chart.
And thanks, you solved my problem with the LegendDisplayMode property :-)

Best regards
Tags
Chart (Obsolete)
Asked by
William
Top achievements
Rank 1
Answers by
HrMeibom
Top achievements
Rank 2
Iron
Share this question
or