Drawing rectangles on chart C# - c#

I'm new to C# and I want to draw some rectangles on a chart (System.Windows.Forms.DataVisualization.Charting.Chart). I've tried looking at some tutorials, althought most of them are about drawing on a form and not on a chart.
Here is some code I've made based on some tutorials. It does not work, the chart simply goes blank.
public partial class Form1 : Form
{
private RectangleF r1;
private RectangleF r2;
public Form1()
{
r1.X = 10;
r1.Y = 10;
r1.Width = 20;
r1.Height = 20.5F;
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
InitializeComponent();
this.chart1.PostPaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(this.PostPaint);
}
private void PostPaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
e.ChartGraphics.GetAbsoluteRectangle(r1);
}
}
In this example I've created as well, only the second rectangle is drawn and the chart shows no axis at all. I want to draw multiples triangles using floats.
public partial class Form1 : Form
{
private Rectangle r1;
private Rectangle r2;
public Form1()
{
r1.X = 10;
r1.Y = 10;
r1.Width = 20;
r1.Height = 20;
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
InitializeComponent();
this.chart1.PostPaint += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs>(this.PostPaint);
}
private void PostPaint(object sender, System.Windows.Forms.DataVisualization.Charting.ChartPaintEventArgs e)
{
e.ChartGraphics.Graphics.DrawRectangle(new Pen(Color.Red, 3), r1);
e.ChartGraphics.Graphics.DrawRectangle(new Pen(Color.Black, 5), r2);
}
}

In both examples, you're setting the values for r1 twice instead of r1 and r2.
In your first example, GetAbsoluteRectangle does not draw a rectangle. It is used for converting coordinates. You should use DrawRectangle like you are in the second example.

Simply change
r1.X = 100;
r1.Y = 100;
r1.Width = 200;
r1.Height = 300;
By that :
r2.X = 100;
r2.Y = 100;
r2.Width = 200;
r2.Height = 300;
It will - normaly - works fine.

Related

why I do not display signals in windows forms?

Let's write a C# program of type "Windows Forms Application" to create a
application simulating the modulation and amplitude demodulation of signal transmission
Analog.
The carrier signal, the modulator signal, the modulated signal will be graphically displayed/
demodulated and the function of spectral density of signals.
I tried to color the picture boxes and they are displayed, but not their content.
My goal is to draw the 4 lines which I want to display in the 4 Picture boxes.
How can I modify this code to get the spec waveformsifice modulation and demodulation in amplitude of an analog signal?
My code:
using System;
using System.Windows.Forms;
using System.Drawing;
using static System.Math;
static class Program
{
[STAThread]
public static void Main()
{
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new ModulationDemodulationExample());
Application.EnableVisualStyles();
}
}
public class ModulationDemodulationExample : Form
{
// Declare controls and variables
private const int SignalLength = 100;
private PictureBox carrierSignalBox = new PictureBox();
private PictureBox modulatorSignalBox = new PictureBox();
private PictureBox modulatedSignalBox = new PictureBox();
private PictureBox demodulatedSignalBox = new PictureBox();
private double[] carrierSignal = new double[SignalLength];
private double[] modulatorSignal = new double[SignalLength];
private double[] modulatedSignal = new double[SignalLength];
private double[] demodulatedSignal = new double[SignalLength];
private double carrierFrequency = 2.0;
private Bitmap carrierSignalBmp;
private Bitmap modulatorSignalBmp;
private Bitmap modulatedSignalBmp;
private Bitmap demodulatedSignalBmp;
public ModulationDemodulationExample()
{
InitializeComponent();
}
private void InitializeComponent()
{
// Initialize form and controls
Text = "Modulation and Demodulation Simulator";
Size = new Size(800, 600);
PictureBox carrierSignalBox = new PictureBox()
{
Location = new Point(10, 10),
Size = new Size(360, 180),
Visible = true,
Name = "Carrier Signal"
};
PictureBox modulatorSignalBox = new PictureBox()
{
Location = new Point(10, 200),
Size = new Size(360, 180),
Visible = true,
Name = "Modulator Signal"
};
PictureBox modulatedSignalBox = new PictureBox()
{
Location = new Point(400, 10),
Size = new Size(360, 180),
Visible = true,
Name = "Modulated Signal"
};
PictureBox demodulatedSignalBox = new PictureBox()
{
Location = new Point(400, 200),
Size = new Size(360, 180),
Visible = true,
Name = "Demodulated Signal"
};
Controls.Add(carrierSignalBox);
Controls.Add(modulatorSignalBox);
Controls.Add(modulatedSignalBox);
Controls.Add(demodulatedSignalBox);
GenerateSignal();
CreateBitmaps();
DrawSignals();
}
private void GenerateSignal()
{
for (int i = 0; i < SignalLength; i++)
{
carrierSignal[i] = Math.Sin(2 * Math.PI * carrierFrequency * i);
modulatorSignal[i] = Math.Sin(2 * Math.PI * i);
modulatedSignal[i] = carrierSignal[i] * modulatorSignal[i];
demodulatedSignal[i] = modulatedSignal[i] * carrierSignal[i];
}
}
private void CreateBitmaps()
{
carrierSignalBmp = new Bitmap(carrierSignalBox.Width, carrierSignalBox.Height);
modulatorSignalBmp = new Bitmap(modulatorSignalBox.Width, modulatorSignalBox.Height);
modulatedSignalBmp = new Bitmap(modulatedSignalBox.Width, modulatedSignalBox.Height);
demodulatedSignalBmp = new Bitmap(demodulatedSignalBox.Width, demodulatedSignalBox.Height);
//carrierSignalBox.Image = carrierSignalBmp;
carrierSignalBox.DrawToBitmap(carrierSignalBmp, carrierSignalBox.ClientRectangle);
modulatorSignalBox.Image = modulatorSignalBmp;
modulatedSignalBox.Image = modulatedSignalBmp;
demodulatedSignalBox.Image = demodulatedSignalBmp;
}
private void DrawSignals()
{
DrawSignal(carrierSignalBox, carrierSignal, Color.Blue);
DrawSignal(modulatorSignalBox, modulatorSignal, Color.Red);
DrawSignal(modulatedSignalBox, modulatedSignal, Color.Green);
DrawSignal(demodulatedSignalBox, demodulatedSignal, Color.Black);
}
void DrawSignal(PictureBox pictureBox, double[] signal, Color color)
{
// Get the graphics object for the PictureBox
var graphics = pictureBox.CreateGraphics();
// Clear the PictureBox
graphics.Clear(Color.White);
// Set the pen color
var pen = new Pen(color);
// Scale the signal's values to the size of the PictureBox
var yScale = (double)pictureBox.Height / 2.0;
var xScale = (double)pictureBox.Width / (double)signal.Length;
// Draw the signal
for (int i = 0; i < signal.Length - 1; i++)
{
var x1 = (int)(i * xScale);
var y1 = (int)(yScale + (signal[i] * yScale));
var x2 = (int)((i + 1) * xScale);
var y2 = (int)(yScale + (signal[i + 1] * yScale));
graphics.DrawLine(pen, x1, y1, x2, y2);
}
}
}
I created the 4 signals but I can not display them in form ,I would have liked each signal to be displayed in the PictureBox.
You can change your program with an override Paint method for each PictureBox like this
public partial class ModulationDemodulationExample : Form
{
// Declare controls and variables
private const int SignalLength = 100;
private PictureBox carrierSignalBox = new PictureBox();
private PictureBox modulatorSignalBox = new PictureBox();
private PictureBox modulatedSignalBox = new PictureBox();
private PictureBox demodulatedSignalBox = new PictureBox();
private double[] carrierSignal = new double[SignalLength];
private double[] modulatorSignal = new double[SignalLength];
private double[] modulatedSignal = new double[SignalLength];
private double[] demodulatedSignal = new double[SignalLength];
private double carrierFrequency = 2.0;
public ModulationDemodulationExample()
{
InitializeComponent();
// Initialize form and controls
Text = "Modulation and Demodulation Simulator";
Size = new Size(800, 600);
PictureBox carrierSignalBox = new PictureBox()
{
Location = new Point(10, 10),
Size = new Size(360, 180),
Visible = true,
Name = "Carrier Signal"
};
carrierSignalBox.Paint += new PaintEventHandler(carrierSignalBox_Paint);
PictureBox modulatorSignalBox = new PictureBox()
{
Location = new Point(10, 200),
Size = new Size(360, 180),
Visible = true,
Name = "Modulator Signal"
};
modulatorSignalBox.Paint += new PaintEventHandler(modulatorSignalBox_Paint);
PictureBox modulatedSignalBox = new PictureBox()
{
Location = new Point(400, 10),
Size = new Size(360, 180),
Visible = true,
Name = "Modulated Signal"
};
modulatedSignalBox.Paint += new PaintEventHandler(modulatedSignalBox_Paint);
PictureBox demodulatedSignalBox = new PictureBox()
{
Location = new Point(400, 200),
Size = new Size(360, 180),
Visible = true,
Name = "Demodulated Signal"
};
demodulatedSignalBox.Paint += new PaintEventHandler(demodulatedSignalBox_Paint);
Controls.Add(carrierSignalBox);
Controls.Add(modulatorSignalBox);
Controls.Add(modulatedSignalBox);
Controls.Add(demodulatedSignalBox);
GenerateSignal();
}
private void GenerateSignal()
{
for (int i = 0; i < SignalLength; i++)
{
carrierSignal[i] = Math.Sin(2 * Math.PI * carrierFrequency * i);
modulatorSignal[i] = Math.Sin(2 * Math.PI * i);
modulatedSignal[i] = carrierSignal[i] * modulatorSignal[i];
demodulatedSignal[i] = modulatedSignal[i] * carrierSignal[i];
}
}
private void carrierSignalBox_Paint(object? sender, PaintEventArgs e)
{
DrawSignal(e.Graphics, carrierSignalBox, carrierSignal, Color.Blue);
}
private void modulatorSignalBox_Paint(object? sender, PaintEventArgs e)
{
DrawSignal(e.Graphics, modulatorSignalBox, modulatorSignal, Color.Red);
}
private void modulatedSignalBox_Paint(object? sender, PaintEventArgs e)
{
DrawSignal(e.Graphics, modulatedSignalBox, modulatedSignal, Color.Green);
}
private void demodulatedSignalBox_Paint(object? sender, PaintEventArgs e)
{
DrawSignal(e.Graphics, demodulatedSignalBox, demodulatedSignal, Color.Black);
}
void DrawSignal(Graphics graphics, PictureBox pictureBox, double[] signal, Color color)
{
// Get the graphics object for the PictureBox
//var graphics = pictureBox.CreateGraphics();
// Clear the PictureBox
graphics.Clear(Color.White);
// Set the pen color
var pen = new Pen(color);
// Scale the signal's values to the size of the PictureBox
var yScale = (double)pictureBox.Height / 2.0;
var xScale = (double)pictureBox.Width / (double)signal.Length;
// Draw the signal
for (int i = 0; i < signal.Length - 1; i++)
{
var x1 = (int)(i * xScale);
var y1 = (int)(yScale + (signal[i] * yScale));
var x2 = (int)((i + 1) * xScale);
var y2 = (int)(yScale + (signal[i + 1] * yScale));
graphics.DrawLine(pen, x1, y1, x2, y2);
}
}
}
When you want to redraw the PictureBox, just call the Invalidate method and the Paint event is automatically called. Example:
carrierSignalBox.Invalidate()

C# Moving the vertical line annotation moving on chart control with 16000 points too slow

I have list XY points are including 16000 points, I deployed it to 2 chart type waveform chart and arc graph. I used the class name is VerticalLineAnnotation to design the vertical line on waveform chart and add series chart type line on arc graph to reflect any the last X point of the veritcal line on arc graph. When move the vertical line to left-right, it work very well but when I leave mouse out the line, the charts (waveforma and arc graph) are redraw and take to long time. How can I prevent it (redraw event) or speed up?.
This is source code
ChartArea CA;
Series S1;
VerticalLineAnnotation VA;
double degree = 0;
int test = 0;
Dictionary<int, PointXY> xyMap = new Dictionary<int, PointXY>();
private void DrawGraph(DataTable dtExcel)
{
Series series1 = new Series("Series2");
series1.ChartArea = "ChartArea1";
series1.ChartType = SeriesChartType.Spline;
series1.Color = Color.White;
chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 1000;
chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 4500;
chart1.ChartAreas["ChartArea1"].AxisY.Interval = 500;
chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 16000;
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1000;
Series series2 = new Series("Series2");
series2.ChartArea = "ChartArea1";
series2.ChartType = SeriesChartType.Spline;
series2.Color = Color.White;
chart2.ChartAreas["ChartArea1"].AxisY.Minimum = -100;
chart2.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
chart2.ChartAreas["ChartArea1"].AxisY.Interval = 10;
chart2.ChartAreas["ChartArea1"].AxisX.Minimum = -100;
chart2.ChartAreas["ChartArea1"].AxisX.Maximum = 100;
chart2.ChartAreas["ChartArea1"].AxisX.Interval = 10;
foreach (DataRow item in dtExcel.Rows)
{
series1.Points.AddXY(int.Parse(item[0].ToString()), item[1].ToString());
CaculatePointXY(item, series2);
}
chart1.Series.Add(series1);
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Green;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Green;
chart1.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;
chart2.Series.Add(series2);
chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Green;
chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Green;
chart2.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;
//Draw vertical line
CA = chart1.ChartAreas["ChartArea1"];
VA = new VerticalLineAnnotation();
VA.AxisX = CA.AxisX;
VA.AllowMoving = true;
VA.IsInfinitive = true;
VA.ClipToChartArea = CA.Name;
VA.Name = "myLine";
VA.LineColor = Color.Red;
VA.LineWidth = 2; // use your numbers!
VA.X = 8000;
chart1.Annotations.Add(VA);
chart2.Series.Add("Line");
chart2.Series["Line"].Points.Add(new DataPoint(0, 0));
chart2.Series["Line"].Color = Color.Red;
chart2.Series["Line"].BorderWidth = 2;
chart2.Series["Line"].Points.Add(new DataPoint(0, 100));
chart2.Series["Line"].ChartType = SeriesChartType.Line;
}
private void CaculatePointXY(DataRow item, Series series2)
{
double distance = (int.Parse(item[1].ToString()) - 819) * 0.0030525;
distance += 95;
double x = distance * Math.Cos((degree * Math.PI / 180));
double y = distance * Math.Sin((degree * Math.PI / 180));
series2.Points.AddXY(x, y);
xyMap.Add(int.Parse(item[0].ToString()), new PointXY()
{
X = x,
Y = y
});
degree += 0.0225;
}
private void chart1_Customize(object sender, EventArgs e)
{
foreach (var label in chart1.ChartAreas[0].AxisY.CustomLabels)
{
label.Text = ((int)double.Parse(label.Text) / 100).ToString();
}
foreach (var label in chart1.ChartAreas[0].AxisX.CustomLabels)
{
label.Text = ((int)double.Parse(label.Text) / 100).ToString();
}
}
private void chart1_MouseDoubleClick(object sender, MouseEventArgs e)
{
var xVal = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
var yVal = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
ZoomWaveFormGraph(xVal, yVal);
}
private void ZoomWaveFormGraph(double xVal, double yVal)
{
ZoomArcGraph((int)xVal);
var YMin = Math.Round(yVal - 1000, 0);
var YMax = Math.Round(yVal + 1000, 0);
chart1.ChartAreas["ChartArea1"].AxisY.Minimum = YMin;
chart1.ChartAreas["ChartArea1"].AxisY.Maximum = YMax;
chart1.ChartAreas["ChartArea1"].AxisY.Interval = 100;
var XMin = Math.Round(xVal - 1000, 0);
var XMax = Math.Round(xVal + 1000, 0);
chart1.ChartAreas["ChartArea1"].AxisX.Minimum = XMin;
chart1.ChartAreas["ChartArea1"].AxisX.Maximum = XMax;
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 200;
}
private void ZoomArcGraph(double xVal)
{
int a = (int)xVal;
var b = xyMap[a];
chart2.ChartAreas["ChartArea1"].AxisY.Minimum = Math.Round(b.Y - 20, 0);
chart2.ChartAreas["ChartArea1"].AxisY.Maximum = Math.Round(b.Y + 20, 0);
chart2.ChartAreas["ChartArea1"].AxisY.Interval = 2;
chart2.ChartAreas["ChartArea1"].AxisX.Minimum = Math.Round(b.X - 20, 0);
chart2.ChartAreas["ChartArea1"].AxisX.Maximum = Math.Round(b.X + 20, 0);
chart2.ChartAreas["ChartArea1"].AxisX.Interval = 2;
}
private double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
private void chart1_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
{
test = (int)Math.Round(e.NewLocationX, 0);
Console.WriteLine("X : " + e.NewLocationX.ToString());
}
private void chart1_AnnotationPositionChanged(object sender, EventArgs e)
{
var a = xyMap[test];
chart2.Series["Line"].Points.ElementAt(1).SetValueXY(a.X, a.Y);
chart2.Refresh();
}

How to plot a 3D Graph to represent an object in space

I have a robot that outputs x,y,z position in space. My problem is that I can only find 2D plot in windows forms using chart.
I want to plot my robot in 3D space. Any tools I can use??
Something similar to this:
I need a free software solution for this
EDIT:
My 2D graph atm:
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 12;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 7;
chart1.ChartAreas[0].AxisY.Interval = 1;
//example
posicao_atual_master.X = 10;
posicao_atual_master.Y = 5;
chart1.Series[0].Points.Clear();
chart1.Series[0].Points.AddXY(posicao_atual_master.X, posicao_atual_master.Y);
DESIGNER:
// chart1
//
chartArea1.AxisX.MajorGrid.Enabled = false;
chartArea1.AxisX.MajorTickMark.Enabled = false;
chartArea1.AxisY.MajorGrid.Enabled = false;
chartArea1.AxisY.MajorTickMark.Enabled = false;
chartArea1.Name = "ChartArea1";
chartArea1.Position.Auto = false;
chartArea1.Position.Height = 100F;
chartArea1.Position.Width = 90F;
this.chart1.ChartAreas.Add(chartArea1);
legend1.BackColor = System.Drawing.Color.Transparent;
legend1.BorderColor = System.Drawing.Color.Transparent;
legend1.Font = new System.Drawing.Font("Microsoft Sans Serif", 4F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Millimeter, ((byte)(1)), true);
legend1.IsTextAutoFit = false;
legend1.Name = "legen";
legend1.TableStyle = System.Windows.Forms.DataVisualization.Charting.LegendTableStyle.Tall;
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(543, 49);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
series1.Color = System.Drawing.Color.Transparent;
series1.Legend = "legen";
series1.MarkerBorderColor = System.Drawing.Color.Black;
series1.MarkerImage = "C:\\Users\\Tiago\\Desktop\\CODIGO_TESE_FINAL_BACKUP1408_BOM\\C# - AR.Drone SDK\\AR.Dron" +
"e\\icone_drone_verde.png";
series1.MarkerImageTransparentColor = System.Drawing.Color.Red;
series1.Name = "Master";
series2.ChartArea = "ChartArea1";
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
series2.Legend = "legen";
series2.MarkerImage = "C:\\Users\\Tiago\\Desktop\\CODIGO_TESE_FINAL_BACKUP1408_BOM\\Fotos dos Relatórios\\icon" +
"e_drone_vermelho.png";
series2.Name = "Slave";
this.chart1.Series.Add(series1);
this.chart1.Series.Add(series2);
this.chart1.Size = new System.Drawing.Size(1159, 359);
this.chart1.TabIndex = 7;
this.chart1.Text = "chart1";
this.chart1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseDown);
this.chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseMove);
this.chart1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseUp);
EDIT: IMAGE
You are correct, there is no proper way to use a real z-axis in the Chart control.
It does have a 3D style though, which can be used for a reasonably nice ChartArea.
You will have to do the painting of the graph in code though, as the built-in z-axis only support as many, or rather as few discret values as you have Series in the chart.
This is ok for some things, like a color cube, but when you need arbitryry data values it just won't do.
Instead you can do this:
Store the z-value of each DataPoint along with the Y-value in the YValues array.
For this you need a ChartType that supports several YValues
Code one of the xxxPaint events to draw the graphics
For this you need a conversion from values to pixels
First we prepare the chart. Many details are up to your needs;
void prepare3dChart(Chart chart, ChartArea ca)
{
ca.Area3DStyle.Enable3D = true; // set the chartarea to 3D!
ca.AxisX.Minimum = -250;
ca.AxisY.Minimum = -250;
ca.AxisX.Maximum = 250;
ca.AxisY.Maximum = 250;
ca.AxisX.Interval = 50;
ca.AxisY.Interval = 50;
ca.AxisX.Title = "X-Achse";
ca.AxisY.Title = "Y-Achse";
ca.AxisX.MajorGrid.Interval = 250;
ca.AxisY.MajorGrid.Interval = 250;
ca.AxisX.MinorGrid.Enabled = true;
ca.AxisY.MinorGrid.Enabled = true;
ca.AxisX.MinorGrid.Interval = 50;
ca.AxisY.MinorGrid.Interval = 50;
ca.AxisX.MinorGrid.LineColor = Color.LightSlateGray;
ca.AxisY.MinorGrid.LineColor = Color.LightSlateGray;
// we add two series:
chart.Series.Clear();
for (int i = 0; i < 2; i++)
{
Series s = chart.Series.Add("S" + i.ToString("00"));
s.ChartType = SeriesChartType.Bubble; // this ChartType has a YValue array
s.MarkerStyle = MarkerStyle.Circle;
s["PixelPointWidth"] = "100";
s["PixelPointGapDepth"] = "1";
}
chart.ApplyPaletteColors();
addTestData(chart);
}
Here we add some test data:
void addTestData(Chart chart)
{
Random rnd = new Random(9);
for (int i = 0; i < 100; i++)
{
double x = Math.Cos(i/10f )*88 + rnd.Next(5);
double y = Math.Sin(i/11f)*88 + rnd.Next(5);
double z = Math.Sqrt(i*2f)*88 + rnd.Next(5);
AddXY3d( chart.Series[0], x, y, z);
AddXY3d( chart.Series[1], x-111, y-222, z);
}
}
The DataPoints are added with this routine:
int AddXY3d(Series s, double xVal, double yVal, double zVal)
{
int p = s.Points.AddXY(xVal, yVal, zVal);
// the DataPoint are transparent to the regular chart drawing:
s.Points[p].Color = Color.Transparent;
return p;
}
If this Paint event we draw the data as we like it. Here are either Lines or Points:
private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
Chart chart = sender as Chart;
if (chart .Series.Count < 1) return;
if (chart .Series[0].Points.Count < 1) return;
ChartArea ca = chart .ChartAreas[0];
e.ChartGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
List<List<PointF>> data = new List<List<PointF>>();
foreach (Series s in chart .Series)
data.Add(GetPointsFrom3D(ca, s, s.Points.ToList(), e.ChartGraphics));
renderLines(data, e.ChartGraphics.Graphics, chart , true); // pick one!
renderPoints(data, e.ChartGraphics.Graphics, chart , 6); // pick one!
}
The coodinates are calculated using axis methods:
List<PointF> GetPointsFrom3D(ChartArea ca, Series s,
List<DataPoint> dPoints, ChartGraphics cg)
{
var p3t = dPoints.Select(x => new Point3D((float)ca.AxisX.ValueToPosition(x.XValue),
(float)ca.AxisY.ValueToPosition(x.YValues[0]),
(float)ca.AxisY.ValueToPosition(x.YValues[1]))).ToArray();
ca.TransformPoints(p3t.ToArray());
return p3t.Select(x => cg.GetAbsolutePoint(new PointF(x.X, x.Y))).ToList();
}
The actual drawing happens in these routines; one draws lines the other dots:
void renderLines(List<List<PointF>> data, Graphics graphics, Chart chart, bool curves)
{
for (int i = 0; i < chart.Series.Count; i++)
{
if (data[i].Count > 1)
using (Pen pen = new Pen(Color.FromArgb(64, chart.Series[i].Color), 2.5f))
if (curves) graphics.DrawCurve(pen, data[i].ToArray());
else graphics.DrawLines(pen, data[i].ToArray());
}
}
void renderPoints(List<List<PointF>> data, Graphics graphics, Chart chart, float width)
{
for (int s = 0; s < chart.Series.Count; s++)
{
Series S = chart.Series[s];
for (int p = 0; p < S.Points.Count; p++)
using (SolidBrush brush = new SolidBrush(Color.FromArgb(64, S.Color)))
graphics.FillEllipse(brush, data[s][p].X-width/2,
data[s][p].Y-width/2,width, width);
}
}
Other drawing routines like meshes or areas can be coded just as well.. Simply add new routines using user GDI+ methods like DrawCurve or FillPolygon or maybe even DrawImage..
You can set the ChartArea.Area3DStyle.Rotation and the ChartArea.Area3DStyle.Inclination for different views, as can be seen in the animation.
Edit I have update the PostPaint method to minimze dependencies.

Align labels in DataVisualization.Charting effectively

I need to develop a chart using datavisualization. Now the chart looks like
below
In the above chart i am using rangebar to show min and max values, in rangebar the label (65---210) now comes in the middle part. But i need to show the min and max like one in red. ie min value in the start position of the rangebar and max value in the end position of the rangebar.
Please let me know if there is any way to do it
The way to do it is by handling the chart's Paint event.
You would need to do some more sophisticated calculations using the Graphics object to make it work for the generic case. But it should get you started.
public partial class WebForm1 : System.Web.UI.Page
{
float deltax, deltay, deltay2;
Font font;
Brush brush;
Random r;
protected void Page_Load(object sender, EventArgs e)
{
deltax = 7.5F;
deltay = 60;
deltay2 = 5;
font = new Font("Courier", 14);
brush = new SolidBrush(Color.Red);
r = new Random();
for (int i = 1; i <= 5; i++)
Chart1.Series[0].Points.Add(new DataPoint(i, new double[2] { r.Next(10, 40), r.Next(60, 90) }));
}
protected void Chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
if (e.ChartElement.ToString().Contains("Series"))
{
int count = Chart1.Series[0].Points.Count - 1;
for (int i = count; i >= 0; i--)
{
DataPoint dp = Chart1.Series[0].Points[i];
float size = (float)(deltax*(dp.YValues[1] - dp.YValues[0] + deltay2));
float x = (float)(deltax * dp.YValues[0]);
float y = (float)(deltay * (count - dp.XValue + 2));
e.ChartGraphics.Graphics.DrawString(string.Format("{0}", dp.YValues[0]), font, brush, new PointF(x, y));
e.ChartGraphics.Graphics.DrawString(string.Format("{0}", dp.YValues[1]), font, brush, new PointF(x + size, y));
}
}
}
}

how to make diagram fit with in panel?

hey i am trying to build a windows application in .net,i have to draw factorial image inside the panel
private void Canvas_Paint(object sender, PaintEventArgs e)
{
start_x = Canvas.Width / 2;
start_Y = Canvas.Height / 2;
for (int i = 0; i < 400; i++)
draw_T();
}
public void draw_T()
{
mypen = new Pen(Color.Green, 2F);
my_angle = my_angle + (45);
my_length = 100 + (1);
end_x = (int)(start_x + Math.Cos(my_angle * .0174539676) * my_length);
end_Y = (int)(start_Y + Math.Sin(my_angle * .0174539676) * my_length);
Point[] points =
{
new Point (start_x,start_Y),
new Point (end_x,end_Y)
};
Point[] points1 =
{
new Point ((end_x+start_x)/2,(end_Y+start_Y)/2),
new Point (end_x+50,end_Y-100)
};
start_x = end_x;
start_Y = end_Y;
Graphics g = Canvas.CreateGraphics();
g.DrawLines(mypen, points);
g.DrawLines(mypen, points1);
}
drawing T shape line,then i start to draw another T shape from last end points .But problem is diagram is going outside of the panel.How do i fix drawing inside the panel

Categories