I would like to draw a vertical line along x=0. I've tried the code below, but instead of all the points being drawn with x=0, they are drawn with x values of 1, 2, 3, 4, 5, respectively. I am able to draw a vertical line at what appears to be any x value other than 0.
The code below also sort of works if I change the x value of any of the points to something other than 0. Is this some sort of bug with the Chart in .NET? Is there a way to draw a vertical line along x=0?
chart1.Series["Series1"].ChartType =
System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
chart1.Series["Series1"].Points.AddXY(0, 0);
chart1.Series["Series1"].Points.AddXY(0, 2.5);
chart1.Series["Series1"].Points.AddXY(0, 5);
chart1.Series["Series1"].Points.AddXY(0, 10);
chart1.Series["Series1"].Points.AddXY(0, 20);
Chart displays incorrectly when all x values are zero
Related
I'm trying to figure out how to draw a discontinued (non-continous) series. This is the code for the series:
Chart.Series["Limit"].Points.AddXY(20000, 30);
Chart.Series["Limit"].Points.AddXY(1000000, 30);
//no plotting wanted here
Chart.Series["Limit"].Points.AddXY(1500000, 40);
Chart.Series["Limit"].Points.AddXY(2500000, 40);
How do I stop it from plotting certain points, like the diagonal line shown in the image below?
You can visually break up a line chart by inserting an invisible DataPoint:
Chart.Series["Limit"].Points.AddXY(20000, 30);
Chart.Series["Limit"].Points.AddXY(1000000, 30);
//no plotting wanted (from previous point to this one) here
int index = Chart.Series["Limit"].Points.AddXY(1500000, 40);
Chart.Series["Limit"].Points[index].Color = Color.Transparent;
Chart.Series["Limit"].Points.AddXY(2500000, 40);
This makes the line that leads to the DataPoint transparent.
I don't know any way to set different lines parameters for the same Series, but you can create two different lines
Chart.Series["Limit"].Points.AddXY(20000, 30);
Chart.Series["Limit"].Points.AddXY(1000000, 30);
Chart.Series["Limit"].BorderColor = Color.Red
//no plotting wanted here
Chart.Series["Limit2"].Points.AddXY(1500000, 40);
Chart.Series["Limit2"].Points.AddXY(2500000, 40);
Chart.Series["Limit2"].BorderColor = Color.Red
I need to draw a string in a panel perfectly centered, horizontally and vertically.
Centering horizontally is not a big deal using the MeasureString function.
Bug, the MeasureString function returns a height that takes care of every possible char (like P and p), but not the real height of the actual string.
Here is the sample code I'm using :
using (Graphics gr = Graphics.FromImage(mBackImage))
{
gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
gr.FillRectangle(new SolidBrush(BackColor), new Rectangle(0, 0, this.Width, this.Height));
var f = new Font(Font.FontFamily, this.ClientSize.Height, FontStyle.Bold, GraphicsUnit.Pixel);
var sz = gr.MeasureString(Label, f);
var pt = new PointF((this.ClientSize.Width - sz.Width) / 2, (this.ClientSize.Height - sz.Height) / 2);
gr.DrawString(Label, f, new SolidBrush(ForeColor), pt);
}
My target is to draw numbers, and the result is that numbers are drawn a little bit too high.
Even if I change the text from "10" to "P", to "p", and I see that the text stays aligned on a "base line".
I really would like to center the text, depending on the real footprint.
How can I achieve this?
Note: this question is absolutely not duplicate of "Center text for receipt printing", because I'm talking about vertical centering, not horizontal centering.
MeasureString or MeasureCharacterRanges are returning sizes taller than the real printed chars, causing vertical alignment issues.
How do I make a custom vertical axis?
I'm making a game in C# and I have no idea how to make this idea work.
I want to there to be a invisible vertical line in the middle of the screen that mirrors specific things to the other side. I was thinking that it work in a way like this:
The new axis would be STH in this example, Left is negative numbers and right is positive.
let STHaxis = SetX(GetCenterX);
//I have no idea how to define this
let oppSTH = GetPlayerSTH;
//True Items
CreatePlayerShot01(GetPlayerSTH(), GetPlayerY(), 15, 270, 1.5, 3, 1);
//Mirrored Items ↓Ignore these numbers.↑
CreatePlayerShot01(oppSTH * -1, GetPlayerY(), 15, 270, 1.5, 3, 1);
But I need something that would make the STH axis exist...
Any ideas to have a mirror, even if it doesn't involve using this code at all. Thanks for reading this, I know I write a lot.
I could work with just moving the Y axis to the middle of my X axis.
let PX = GetPlayerX; //Gets your X
let MX = GetClipMaxX; // Gets the max X can be (complete right)
let MMX = GetClipMinX; // Gets the min X can be (complete left)
let agr = MMX + PX; // Agr = PX in this case
let mir = MX - PX; // Takes distance from minX and subtracts it from maxX
I am trying to draw a top view of an IC package, which should look like this (sorry I couldnt even draw it good enough using windows's paint!)
I am using a path obeject, but the result of my path object is no where near what I expect. Atleast the complete rectangle itself draws fine but I have problem to make that top arc you see in my example picture. Would be nice if you can point me to the right place. Here is my code:
private GraphicsPath DrawDilBounds(Size size)
{
var p = new GraphicsPath(FillMode.Alternate);
p.StartFigure();
p.AddLine(0, 0, 0, size.Height);
p.AddLine(0, size.Height, size.Width, size.Height);
p.AddLine(size.Width, size.Height, size.Width, 0);
p.AddLine(size.Width, 0, (size.Width/2) - 10, 0);
p.AddArc(size.Width/2 - 10, 0, 10, 10, 10, 10); //This arc looks like no arc!
p.AddLine((size.Width/2) + 10, 0, 0, 0);
p.CloseFigure();
return p;
}
So what I am doing here is starting some lines from top left corner , to bottom left corner, to right bottom corner and finaly to top right corner, then I added a line from top right corner to the middle of the top , minus 10 pixels then I want to add the arc with width of 20 pixels and then finish the drawing back to the top left corner.
You specify the arc by its bounding box. Using 10 as the radius gives a box of 20 x 20 (you used 10 x 10) whose upper left corner is located at (-10, -10) from the center of the arc (you used (-10, 0)). The last two arguments must be degrees, the starting and ending angle. Since you draw it from left-to-right that will be 0 and 180 degrees (you used 10 and 10). You also fumbled the lengths of the 2 lines at the top, they should be half the width -10 (you used +10). Fix:
p.AddLine(size.Width, 0, (size.Width / 2) + 10, 0);
p.AddArc(size.Width / 2 - 10, -10, 20, 20, 0, 180);
p.AddLine((size.Width / 2) - 10, 0, 0, 0);
Which gets you:
I have two Point structures and I need to draw an I-Beam based on those points, where each point represents the cross-section on either side of the I-Beam. The width of the end caps should be fixed and arbitrary.
Basically I need to draw three lines. First I'll DrawLine(Point1, Point2), then I need the math to figure out how to draw the next two lines on perpendicular angles so that they are centered on Point1 and Point2.
The image below shows what I need to draw based on the center line. However, this line can be at any angle. The Point1 and Point2 that connect the line can be anywhere in a 2D space.
You can try playing around with LineCaps:
protected void DrawIBeam(Graphics g, Point fromPoint, Point toPoint)
{
using (GraphicsPath hPath = new GraphicsPath())
{
hPath.AddLine(new Point(-5, 0), new Point(5, 0));
CustomLineCap myCap = new CustomLineCap(null, hPath);
myCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
using (Pen myPen = new Pen(Color.Black, 2))
{
myPen.CustomStartCap = myCap;
myPen.CustomEndCap = myCap;
g.DrawLine(myPen, fromPoint, toPoint);
}
}
}
and call it:
DrawIBeam(e.Graphics, new Point(10, 10), new Point(60, 60));
From CustomLineCap Class
Assuming a width that's half the width of the I part of the I beam, first you find the slope of the first line you drew.
Next, you take the negative inverse of the slope, and draw a line from Point1 of length width in both directions. That's why width is half of the width you want to draw.
Finally you draw a line from Point 2 of length width in both directions.
Here's the mathematical formula for drawing a perpendicular line.