Does anyone know how to draw a connected line between the cells in the gridview? I want to connect the lowest price of the fruits in each month. I am using WPF and C# to build it.
Here is the link for the picture of my situation I want my gridview to show :
P/s: I drew the image using Paint. Sorry, I couldn't upload a picture due to low reputation points.
Add a canvas like this to your xaml so that it fulfill your gridview
<Canvas Width="300" Height="300" Background="Beige" Opacity="0.5" Name="canvas1"/>
then in your code, you can draw your path using either Line or Path class. My code is using Line class:
private void drawGridLines()
{
Line lastline = new Line();
lastLine.X2 = 0;
lastLine.Y2 = 0;
bool first = true;
for(int i=0; i<5; i++) // iterate over your gridview rows
{
Line newline = new Line();
newline.X1 = lastline.X2;
newline.Y1 = lastline.Y2;
newline.X2 = 50; // calculate X position of your minimum cell
newline.Y2 = 50; // calculate Y position of your minimum cell
if(!first) { // first minimum cell should't be drawn, it is just the start point for next line
drawLine(newline);
} else {
first = false;
}
lastline = newline;
}
}
public void drawLine(Line line)
{
line.Stroke = System.Windows.Media.Brushes.Red;
line.StrokeThickness = 1;
canvas1.Children.Add(line);
}
Related
When I try to draw a shape, like a line, is working only in the first StackPanel
Line myLine = new Line();
myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
myLine.X1 = 1;
myLine.Y1 = 1;
myLine.X2 = 500;
myLine.Y2 = 50;
// myLine.HorizontalAlignment = HorizontalAlignment.Left;
// myLine.VerticalAlignment = VerticalAlignment.Center;
myLine.StrokeThickness = 2;
gridAreaFight.Children.Add(myLine);
And here is the picture of the grid.
I try to create a Tower Defense game, and I want the towers to attack (with lines/shapes) on the grid, but it seems the draw method is working only in my first stack panel image (see the picture, in the left corner top). Is there something I'm not doing right? Or I should use something else instead drawing method for my towers?
You need to set row and column span for the line in order to draw it in whole available space. If you are drawing something on the grid without specific row and column, it defaults to column and row number 0.
Line myLine = new Line();
myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
Grid.SetRowSpan(myLine, n); // Here you are setting RowSpan. n-number of rows to span
Grid.SetColumnSpan(myLine, n); // Here you are setting ColumnSpan. n-number of columns to span
myLine.X1 = 1;
myLine.Y1 = 1;
myLine.X2 = 500;
myLine.Y2 = 50;
// myLine.HorizontalAlignment = HorizontalAlignment.Left;
// myLine.VerticalAlignment = VerticalAlignment.Center;
myLine.StrokeThickness = 2;
area.Children.Add(myLine);
I would like to display 13 pictureBox, however, it ends up with only the last one visible.
So I was wondering if I did it in a wrong way.
The following code get image from resources folder.
var testP = new PictureBox();
for (int i = 0; i < 13; i++)
{
testP.Width = 65;
testP.Height = 80;
testP.BorderStyle = BorderStyle.None;
testP.SizeMode = PictureBoxSizeMode.StretchImage;
test[i] = getImage(testP, testPTemp[i]);
}
The following code is trying to display 13 pictureBox with shifting location.
These two codes segments should be able to perform the action.
test = new PictureBox[13];
for (var i = 0; i < 13; i++)
{
test[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);
test[i].Left = 330;
test[i].Top = 500;
test[i].Location = new Point(test[i].Location.X + 0 * displayShift, test[i].Location.Y);
this.Controls.Add(test[i]);
}
Here is the getImage()
private PictureBox getImage(PictureBox pB, string i) // Get image based on the for loop number (i)
{
pB.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + i); // Get the embedded image
pB.SizeMode = PictureBoxSizeMode.StretchImage;
return pB;
}
I'm pretty sure there are all PictureBox Controls but they have all the same location so they are lying above each other. That's why only the last one is visible to you.
I think you should replace the 0 with the i variable.
test[i].Location = new Point(test[i].Location.X + i * displayShift, test[i].Location.Y); this.Controls.Add(test[i]);
It's hard to tell the exact problem based off the code you've provided. One possible issue could be that when you are creating the PictureBoxes you only create a single instance before the for loop and then fill the array with references to that instance. Another possibility is that when you're calculating the X position of the controls, you're multiplying by 0 which will always result in 0 (meaning all the controls are at location 330).
Below is code that will achieve basically what you're trying but without all your code I can't give you a more specific example.
In Your Class
const int PICTURE_WIDTH = 65;
const int PICTURE_HEIGHT = 85;
Inside You Function
//Loop through each image
for(int i = 0; i < testTemp[i].length; i++)
{
//Create a picture box
PictureBox pictureBox = new PictureBox();
pictureBox.BorderStyle = BorderStyle.None;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
//Load the image date
pictureBox.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);
//Set it's size
pictureBox.Size = new Size(PICTURE_WIDTH, PICTURE_HEIGHT);
//Position the picture at (330,500) with a left offset of how many images we've gone through so far
pictureBox.Location = new Point(330 + (i * PICTURE_WIDTH), 500);
//Add the picture box to the list of controls
this.Controls.Add(pictureBox);
}
If you need to keep a list of the picture boxes, just create a new list before the loop and add each pictureBox to the list inside the loop. If the control/window you're adding these PictureBoxes to needs to scroll left or right to see all the images set the AutoScroll property to true.
I am having some trouble with my C# chart.
I want to create a winnings chart.
This chart is what I want to create:
I currently have this:
WinChart.ChartAreas[0].AxisY.Title = "$ USD";
WinChart.ChartAreas[0].AxisY.Minimum = -1;
WinChart.ChartAreas[0].AxisY.Maximum = 1;
WinChart.ChartAreas[0].AxisX.Title = "Tourneys";
WinChart.ChartAreas[0].AxisX.Minimum = 0;
WinChart.ChartAreas[0].AxisX.IsStartedFromZero = true;
WinChart.Series[0].Points.Add(0);
WinChart.Series[0].Points.Add(0.10);
WinChart.Series[0].Points.Add(0.20);
WinChart.Series[0].Points.Add(0.30);
WinChart.Series[0].Points.Add(-0.50);
WinChart.Series[0].Points.Add(-0.60);
WinChart.Series[0].Points.Add(-0.70);
How can I make it start at the coordinates 0,0 and how do I make a middle line that is $0 ?
Documentation link: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.aspx
I'm going to assume some thing. WinChart is probably inheriting from Chart:
public class WinChart : Chart { }
and that ChartAreas is a ChartAreaCollection object and the same with Series
So I would do something like:
WinChart.ChartAreas[0].AxisY.Title = "$ USD";
WinChart.ChartAreas[0].AxisY.Minimum = -1;
WinChart.ChartAreas[0].AxisY.Maximum = 1;
WinChart.ChartAreas[0].AxisY.Interval = 0.2; // adjusts y axis scale
WinChart.ChartAreas[0].AxisX.Title = "Tourneys";
WinChart.ChartAreas[0].AxisX.Minimum = 0;
Series series = new Series();
series.Points.Add(0,0);
...
series.Points.Add(5, 1.05);
WinChart.Series.Add(series);
//repeat last five lines to add second line to graph
I am trying to draw a vertical line that is anchored to a point. I tried to use the height of my Y axis which is fixed to draw the line, but it wasn't centered correctly. So right now I have an infinite line, but that I want is the line to just fill the graph like so
VerticalLineAnnotation lineannot = new VerticalLineAnnotation();
lineannot.AnchorDataPoint = chart.Series[item].Points.Last();
lineannot.LineColor = Color.Red;
lineannot.Width = 3;
lineannot.Visible = true;
lineannot.IsInfinitive = true;
chart.Annotations.Add(lineannot);
IsInfinitive is complemented by ClipToChartArea; you can set the line to be clipped to a ChartArea like this:
lineannot.ClipToChartArea = chart.ChartAreas[item].Name;
assuming item is the right area name or index..
Note that ClipToChartArea takes the name of the chart area!
This is the simplest way to do it.
It is also possible to control an annotation's position and size directly:
// Note that directly after adding points this will return NaN:
double maxDataPoint = chart1.ChartAreas[0].AxisY.Maximum;
double minDataPoint = chart1.ChartAreas[0].AxisY.Minimum;
LineAnnotation annotation2 = new LineAnnotation();
annotation2.IsSizeAlwaysRelative = false;
annotation2.AxisX = chart1.ChartAreas[0].AxisX;
annotation2.AxisY = chart1.ChartAreas[0].AxisY;
annotation2.AnchorY = minDataPoint;
annotation2.Height = maxDataPoint - minDataPoint;;
annotation2.Width = 0;
annotation2.LineWidth = 2;
annotation2.StartCap = LineAnchorCapStyle.None;
annotation2.EndCap = LineAnchorCapStyle.None;
annotation2.AnchorX = 21; // <- your point
annotation2.LineColor = Color.Pink; // <- your color
chart1.Annotations.Add(annotation2);
community,
this is my first question here. to all the other problems i had there was often a answer on this side. for this i did not find one. here is my problem:
i have a chart and i want to add a vertical starpline at the point i click the mouse on the chart. this i working so far with that code:
chartSettlingCurve.ChartAreas[0].CursorX.SetCursorPixelPosition(new Point(e.X, e.Y), true);
double pX = chartSettlingCurve.ChartAreas[0].CursorX.Position; //X Axis coordinate Mouse
DataPoint dataPoint = chartSettlingCurve.Series[0].Points.FindByValue(nearestPreceedingValue(nearestPreceedingValue(pX)), "X");
labelTab3Test.Text = Convert.ToString(dataPoint);
DataPoint maxValuePoint = chartSettlingCurve.Series[0].Points.FindMaxByValue();
StripLine stripLineEnd = new StripLine();
stripLineEnd.BorderColor = Color.Blue;
stripLineEnd.IntervalOffset = dataPoint.XValue;
stripLineEnd.Text = "End";
this code generates a stripline at every point i click on the graph but i want just one line. so if ther still is a strip line it shopud be replaced by the new one at a other position.
woud be nice if you can help me. if you dont get my problem pleas ask.
thanks
a working solution is:
for (int j = 0; j < chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Count; j++)
{
if (chartSettlingCurve.ChartAreas[0].AxisX.StripLines[j].Tag.ToString() == "end")
{
chartSettlingCurve.ChartAreas[0].AxisX.StripLines.RemoveAt(j);
j--;
}
}
or as an alternative:
bool loop = true;
while (loop)
{
loop = false;
foreach (var element in chartSettlingCurve.ChartAreas[0].AxisX.StripLines)
{
if (element.Tag.ToString() == "end")
{
chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Remove(element);
loop = true;
break;
}
}
}
You can remove all striplines using code below;
while (chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Count > 0)
{
chartSettlingCurve.ChartAreas[0].AxisX.StripLines.Remove(hartSettlingCurve.ChartAreas[0].AxisX.StripLines.Last());
}
than you can add some code.