Search code examples
c#chartsmschartrectangles

Draw on MSChart control


I've got a simple line graph and I'd like to highlight some parts of this graph by drawing a rectangle around the line (ideally a filled rectangle with transparency...). I haven't any idea if this is possible with the MS chart control ?


Solution

  • I recommend you download the code samples from MS and checkout the section on annotations. In there you will find all the documentation you require to achieve what you described:

    private void AddRectangleAnnotation()
    {
    RectangleAnnotation annotation = new RectangleAnnotation();
    annotation.AnchorDataPoint = Chart1.Series[0].Points[2];
    annotation.Text = "I am a\nRectangleAnnotation";
    annotation.ForeColor = Color.Black;
    annotation.Font = new Font("Arial", 12);;
    annotation.LineWidth = 2;
    annotation.BackColor = Color.PaleYellow;
    annotation.LineDashStyle = ChartDashStyle.Dash;
    
    Chart1.Annotations.Add(annotation);
    }