Witam,projektuję małą bibliotekę do robienia wykresów funkcji liniowej i mam mały kłopot przy rysowaniu poziomych linii siatki.Moim zamiarem było aby zaznaczając kontrolkę można było zadecydować ile takich linii powinno być,niestety wszystko działa dobrze w przypadku jednej osi,gdy dodaję więcej osi to przeskakują one nie tam gdzie chce,uciekają w prawo.
Zamieszczam poniżej kod całej kontrolki i jeszcze mam dodatkowo pytanie bo aby osie sie narysowały muszę użyć opcji Build Solution a chciałbym aby robiło się to od razu po zmienieniu wartości w Designerze.
Kod Kontrolki :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace EBinder.CustomControls.Charts
{
public class LinearChart : Control
{
public Pen MyPen;
public int Maxvalue;
public int Minvalue;
public int zero = 0;
public int yAxisLines;
public int xAxisLines;
public int YLineX = 0;
public int YLineY = 30;
public Point PY1;
public Point PY2;
public Point y1;
public Point y2;
public LinearChart()
{
this.SuspendLayout();
Points = new List<Point>();
PY1 = new Point(20, 50);
PY2 = new Point(20, Height - 20);
}
public int MinValue
{
get { return Minvalue; }
set { Minvalue = value; }
}
public int MaxValue
{
get { return Maxvalue; }
set { Maxvalue = value; }
}
public int YAxisLines
{
get { return yAxisLines; }
set { yAxisLines = value; }
}
public int XAxisLines
{
get { return xAxisLines; }
set { xAxisLines = value; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<Point> Points { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
System.Diagnostics.Debug.WriteLine("Liczba elementów: " + Points.Count);
MyPen = new Pen(Brushes.Black, 2);
//PY1 = new Point(20, 50);
//PY2 = new Point(20, Height - 20);
g.DrawLine(MyPen, new Point(20, 50), new Point(20, Height - 20));// Oś Y
g.DrawLine(MyPen, new Point(35, 65), new Point(20, 50));//Strzałka Y Prawa
g.DrawLine(MyPen, new Point(5, 65), new Point(20, 50)); //Strzałka Y
g.DrawLine(MyPen, new Point(20, Height - 20), new Point(Width - 50, Height - 20)); //Oś X
g.DrawLine(MyPen, new Point(Width - 50, Height - 20), new Point(Width - 65, Height - 5));//Strzałka X
g.DrawLine(MyPen, new Point(Width - 50, Height - 20), new Point(Width - 65, Height - 35));//Strzałka X
g.DrawString(this.Text, new Font("Segoe UI", 12), Brushes.Black, new PointF(Width / 2 - 5 - 45, 5)); // Tytuł Wykresu
for (int y = 0; y < YAxisLines; y++)
{
//PY1 = new Point(20, 50);
//PY2 = new Point(20, Height - 20);
//public int YLineX = 30;
//public int YLineY = 30;
//g.DrawLine(MyPen, new Point(50, 80), new Point(50, Height - 20));
YLineX += 30;
g.DrawLine(MyPen,new Point(PY1.X + YLineX,80),new Point(PY2.X + YLineX,Height - 20));
}
g.Dispose();
}
}
}
Zapomniałem jeszcze dodać,przy zmienianiu rozmiaru kontrolki osie w ogóle gdzieś znikają w prawo i nie chcą wrócić na miejsce :P .