Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label on the Chart using Microsoft Chart controls

I am creating a 3d chart using Microsoft Chart controls. Here is the image:

alt text
(source: highoncoding.com)

I want to show the point on the top of each bar graph. Like for Exam 1 on top of bar chart it should show 2 (as in 2 points) etc.

Here is the code:

private void BindData() {

            var exams = new List<Exam>()
            {
                new Exam() { Name = "Exam 1", Point = 10 }, 
                new Exam() { Name = "Exam 2", Point = 12 }, 
                new Exam() { Name = "Exam 3", Point = 15 }, 
                new Exam() { Name = "Exam 4", Point = 2 }
            };

            var series = ExamsChart.Series["ExamSeries"];         



            series.YValueMembers = "Point"; 
            series.XValueMember = "Name"; 

            //series.MarkerStyle = System.Web.UI.DataVisualization.Charting.MarkerStyle.Circle;
            //series.MarkerSize = 20;
            //series.LegendText = "hellow";
            //series.Label = "something";            


            var chartAreas = ExamsChart.ChartAreas["ChartArea1"];           


            ExamsChart.DataSource = exams;
            ExamsChart.DataBind(); 
        }

and here is the html code:

<asp:Chart ID="ExamsChart" Width="600" Height="320" runat="server">
      <Titles>
      <asp:Title Text="Exam Report" />
      </Titles>
        <Series>
          <asp:Series Name="ExamSeries" ChartType="Column">
          </asp:Series>
        </Series>
        <ChartAreas>

          <asp:ChartArea Name="ChartArea1">
          <Area3DStyle Enable3D="true"  WallWidth="10" />
          </asp:ChartArea>
        </ChartAreas>
      </asp:Chart>

UPDATE:

Here is the answer:

 foreach (var exam in exams) {

                var point = new DataPoint();
                point.SetValueXY(exam.Name, exam.Point);

                point.Label = exam.Name;

                series.Points.Add(point); 
            }  
like image 627
azamsharp Avatar asked Apr 21 '10 18:04

azamsharp


People also ask

How do you label a chart?

To properly label a graph, you should identify which variable the x-axis and y-axis each represent. Don't forget to include units of measure (called scale) so readers can understand each quantity represented by those axes. Finally, add a title to the graph, usually in the form "y-axis variable vs. x-axis variable."

How do I label a chart in Word?

Click the chart, and then click the Chart Design tab. Click Add Chart Element and select Data Labels, and then select a location for the data label option. Note: The options will differ depending on your chart type. If you want to show your data label inside a text bubble shape, click Data Callout.


1 Answers

Directly from the MS chart samples:

// Show data points values as labels
chart1.Series["Series1"].IsValueShownAsLabel = true;

// Set data point label
chart1.Series["Series1"].Points[2].Label = "My Point Label\nLabel Line #2";
like image 121
zeFrenchy Avatar answered Oct 09 '22 06:10

zeFrenchy