3

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 ?

GuillaumeA
  • 3,493
  • 4
  • 35
  • 69

3 Answers3

3

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);
}
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • Awsome ! Do you have an idea now to offset the rectangle in order the left bottom corner is the anchor point ? – GuillaumeA May 10 '13 at 13:36
  • Finally I did not use the AnchorOffset properties as the behavior was incoherent (setting the x offset resulting in an offset in y too...??). Instead, I set the position with the X and Y properties without anchoring which is not a problem in my case. – GuillaumeA May 14 '13 at 15:51
  • _Do you have an idea now to offset the rectangle in order the left bottom corner is the anchor point ?_ - I think you can set a negativeHeight to achieve this. – TaW Sep 09 '18 at 14:47
0

Do you mean:

using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
    using(Brush brush = new SolidBrush(your_color))
    {
        g.FillRectangle(brush , x, y, width, height);
    }
}

or you can use

Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))

where alpha goes from 0 to 255, so a value of 128 for your alpha will give you 50% opactity.

Civa
  • 2,058
  • 2
  • 18
  • 30
  • well, I meant using the control System.Windows.Forms.DataVisualiztion.Charting.Chart and overlaying a rectangle. – GuillaumeA May 07 '13 at 14:27
0

When you'd like to draw on a chart you can take add a LineAnnotation or RectangleAnnotation. if however you'd like more control you can use the chart's PrePaint and PostPaint events. And if you can paint, well then you can paint anything. Also using this will make the chart "printing" and "exporting" look the same as you painted it. Paining over it will look funny when the chart's location is changed on the screen, so always paint in it.

Say you have a trading chart and you need to draw a line as to where you become profitable or as square as to state where you're "To Something" tzhen just add the coordinates from where to where you'd like to be and of you go...

enter image description here

MS Chart sample project shows how to do this with the following code (vb.net also available):

using System.Windows.Forms.DataVisualization.Charting;
...

private void chart1_PostPaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
    if(sender is ChartArea)
    {

        ChartArea area = (ChartArea)sender;
        if(area.Name == "Default")
        {
            // If Connection line is not checked return
            if( !ConnectionLine.Checked )
                return;

            double max;
            double min;
            double xMax;
            double xMin;

            // Find Minimum and Maximum values
            FindMaxMin( out max, out min, out xMax, out xMin );

            // Get Graphics object from chart
            Graphics graph = e.ChartGraphics.Graphics;

            // Convert X and Y values to screen position
            float pixelYMax = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.Y,max);
            float pixelXMax = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.X,xMax);
            float pixelYMin = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.Y,min);
            float pixelXMin = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.X,xMin);

            PointF point1 = PointF.Empty;
            PointF point2 = PointF.Empty;

            // Set Maximum and minimum points
            point1.X = pixelXMax;
            point1.Y = pixelYMax;
            point2.X = pixelXMin;
            point2.Y = pixelYMin;

            // Convert relative coordinates to absolute coordinates.
            point1 = e.ChartGraphics.GetAbsolutePoint(point1);
            point2 = e.ChartGraphics.GetAbsolutePoint(point2);

            // Draw connection line
            graph.DrawLine(new Pen(Color.Yellow,3), point1,point2);
        }
    }
}

private void chart1_PrePaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
    if(sender is ChartArea){

        ChartArea area = (ChartArea)sender;
        if(area.Name == "Default")
        {
            double max;
            double min;
            double xMax;
            double xMin;

            // Find Minimum and Maximum values
            FindMaxMin( out max, out min, out xMax, out xMin );

            // Get Graphics object from chart
            Graphics graph = e.ChartGraphics.Graphics;

            // Convert X and Y values to screen position
            float pixelYMax = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.Y,max);
            float pixelXMax = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.X,xMax);
            float pixelYMin = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.Y,min);
            float pixelXMin = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.X,xMin);

            // Specify width of triangle
            float width = 3;

            // Set Maximum points
            PointF [] points = new PointF[3];
            points[0].X = pixelXMax - width;
            points[0].Y = pixelYMax - width - 2;
            points[1].X = pixelXMax + width;
            points[1].Y = pixelYMax - width - 2;
            points[2].X = pixelXMax;
            points[2].Y = pixelYMax - 1;

            // Convert relative coordinates to absolute coordinates.
            points[0] = e.ChartGraphics.GetAbsolutePoint(points[0]);
            points[1] = e.ChartGraphics.GetAbsolutePoint(points[1]);
            points[2] = e.ChartGraphics.GetAbsolutePoint(points[2]);

            // Draw Maximum trangle
            graph.FillPolygon(new SolidBrush(Color.Red), points);

            // Set Minimum points
            points = new PointF[3];
            points[0].X = pixelXMin - width;
            points[0].Y = pixelYMin + width + 2;
            points[1].X = pixelXMin + width;
            points[1].Y = pixelYMin + width + 2;
            points[2].X = pixelXMin;
            points[2].Y = pixelYMin + 1;

            // Convert relative coordinates to absolute coordinates.
            points[0] = e.ChartGraphics.GetAbsolutePoint(points[0]);
            points[1] = e.ChartGraphics.GetAbsolutePoint(points[1]);
            points[2] = e.ChartGraphics.GetAbsolutePoint(points[2]);

            // Draw Minimum triangle
            graph.FillPolygon(new SolidBrush(Color.Blue), points);
        }
    }
}
Walter Verhoeven
  • 3,867
  • 27
  • 36