We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Drawing a single bar graph

I was able to create a single bar graph by drawing two rectangles on top of each other. However, I am unable to get exactly what I want. I want to draw a dashed box and then fill the box a certain percent.

Like:

0% 50% 100%
-------------------------------------
| | |
|Filled (gradient) | White |
| | |
-------------------------------------

Any suggestions?

6 Replies

SS Sri Subhashini M Syncfusion Team August 28, 2009 08:17 AM UTC

Hi Aaron,

Thank you for your interest in Essential PDF.

We can draw Rectangle with dashed border by setting DashStyle of the PdfPen as Dash. And we can fill the rectangle with gradient color with the help of the PdfLinearGradientBrush. Kindly refer the below code snippet,


// Create a new page.
PdfPage page = doc.Pages.Add();
PdfGraphics g = page.Graphics;

PdfPen pen = new PdfPen(Color.Black);
pen.DashStyle = PdfDashStyle.Dash;

RectangleF rect = new RectangleF(10, 120, 200, 100);
PdfLinearGradientBrush gBrush = new PdfLinearGradientBrush(rect, Color.LightGreen, Color.Brown, PdfLinearGradientMode.BackwardDiagonal);
pen.Color = Color.Black;
g.DrawRectangle(pen, gBrush, rect);

rect = new RectangleF(200, 120, 100, 100);
g.DrawRectangle(pen, PdfBrushes.White, rect);


Could you please try the below sample from the below specified location and let me know if this helps,

http://files.syncfusion.com/samples/PDF.Windows/PDF_Window_F89535.zip

Regards,
Suba


AH Aaron Hood August 28, 2009 03:36 PM UTC

That worked. However, I tweaked it a bit to make it easier to move the percent filled for dynamic purposes. It works except I am unable to make the gradientbrush rectangle to change pen color to gray. For some reason it's always black. Any suggestions?

Here is what I have:

PdfPen pen = new PdfPen(Color.Gray);
pen.DashStyle = PdfDashStyle.Dash;
pen.Color = Color.Gray;

RectangleF rect = new RectangleF(0, 120, 300, 13);
g.DrawRectangle(pen, PdfBrushes.White, rect);

rect = new RectangleF(0, 120, 200, 13);
PdfLinearGradientBrush gBrush = new PdfLinearGradientBrush(rect, Color.LightGreen, Color.White, PdfLinearGradientMode.Vertical);
pen.Color = Color.Gray;
g.DrawRectangle(pen, gBrush, rect);


AH Aaron Hood August 28, 2009 05:33 PM UTC

I think I found the problem but I do not understand why it's a problem.

Why do I get a black border when I draw a the same colored border on top of each other?

Example 1 (I get a gray border around the gradient box):
PdfPen pen = new PdfPen(Color.LightGray);
pen.DashStyle = PdfDashStyle.Dash;
pen.Color = Color.LightGray;

RectangleF rect = new RectangleF(0, 120, 300, 13);
g.DrawRectangle(pen, PdfBrushes.White, rect);

pen.Color = Color.Gray;
rect = new RectangleF(0, 120, 200, 13);
PdfLinearGradientBrush gBrush = new PdfLinearGradientBrush(rect, Color.LightGreen, Color.White, PdfLinearGradientMode.Vertical);
g.DrawRectangle(pen, gBrush, rect);

Example 2 (I get a black border):
PdfPen pen = new PdfPen(Color.LightGray);
pen.DashStyle = PdfDashStyle.Dash;
pen.Color = Color.LightGray;

RectangleF rect = new RectangleF(0, 120, 300, 13);
g.DrawRectangle(pen, PdfBrushes.White, rect);

pen.Color = Color.LightGray;
rect = new RectangleF(0, 120, 200, 13);
PdfLinearGradientBrush gBrush = new PdfLinearGradientBrush(rect, Color.LightGreen, Color.White, PdfLinearGradientMode.Vertical);
g.DrawRectangle(pen, gBrush, rect);


SS Sri Subhashini M Syncfusion Team September 1, 2009 08:35 AM UTC

Hi Aaron,

It seems that you have drawn the rectangle two times on the same area. So, the Gray border has been drawn twice and the border color looks like black. Could you please try the below code snippet and let me know if this helps?


PdfPen pen = new PdfPen(Color.LightGray);
pen.DashStyle = PdfDashStyle.Dash;
pen.Color = Color.LightGray;

ectangleF rect = new RectangleF(0, 120, 300, 13);
//g.DrawRectangle(pen, PdfBrushes.White, rect);

pen.Color = Color.LightGray;
rect = new RectangleF(0, 120, 200, 13);
PdfLinearGradientBrush gBrush = new PdfLinearGradientBrush(rect, Color.LightGreen, Color.White, PdfLinearGradientMode.Vertical);
g.DrawRectangle(pen, gBrush, rect);


Regards,
Suba


AH Aaron Hood September 1, 2009 03:32 PM UTC

I just tried your example and it has a black border as well.


SS Sri Subhashini M Syncfusion Team September 2, 2009 07:18 AM UTC

Hi Aaron,

I am afraid that I was not able to reproduce the issue in our testing environment. I have created a simple sample to test this issue and it is available in the following link.

http://files.syncfusion.com/samples/Pdf.Windows/PDF_Window_F89535_1.zip

Please have a look at the above sample and if still the issue exists, could you please try reproducing it in the above sample or send us your working sample (if possible) so that we could sort out the cause of the issue and provide you a solution?

Regards,
Suba

Loader.
Live Chat Icon For mobile
Up arrow icon