Cannot get zedgraph to work - c#

I've tried to make a method, which insert some numbers in a zedgraph. But it won't work.
This is my code:
string title = "Contemporary Quality";
string xaxis = "Amount";
string yaxis = "Percent";
zedGraphControl3.GraphPane.CurveList.Clear();
zedGraphControl3.GraphPane.GraphObjList.Clear();
GraphPane myPane = zedGraphControl3.GraphPane;
myPane.GraphObjList.Clear();
myPane.CurveList.Clear();
myPane.XAxis.Title.Text = xaxis;
myPane.Title.Text = title;
myPane.YAxis.Title.Text = yaxis;
myPane.XAxis.Scale.MinAuto = false;
myPane.XAxis.Scale.MinGrace = -0.5;
myPane.XAxis.Scale.Max = 16;
myPane.BarSettings.Type = BarType.PercentStack;
double[] x = new double[] { zg1.GraphPane.XAxis.Scale.Min, zg1.GraphPane.XAxis.Scale.Max };
double[] y = new double[] { 20, 20 };
ZedGraph.LineItem lineItem = new ZedGraph.LineItem("cursorY1", x, y, Color.Black, ZedGraph.SymbolType.None);
lineItem.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
lineItem.IsSelectable = false;
lineItem.Label.IsVisible = false; // hides the cursor in the legend
zg1.GraphPane.CurveList.Add(lineItem);
y = new double[] { 40, 40 };
lineItem = new ZedGraph.LineItem("cursorY2", x, y, Color.Black, ZedGraph.SymbolType.None);
lineItem.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
lineItem.IsSelectable = false;
lineItem.Label.IsVisible = false; // hides the cursor in the legend
zg1.GraphPane.CurveList.Add(lineItem);
y = new double[] { 60, 60 };
lineItem = new ZedGraph.LineItem("cursorY3", x, y, Color.Black, ZedGraph.SymbolType.None);
lineItem.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
lineItem.IsSelectable = false;
lineItem.Label.IsVisible = false; // hides the cursor in the legend
zg1.GraphPane.CurveList.Add(lineItem);
y = new double[] { 80, 80 };
lineItem = new ZedGraph.LineItem("cursorY4", x, y, Color.Black, ZedGraph.SymbolType.None);
lineItem.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
lineItem.IsSelectable = false;
lineItem.Label.IsVisible = false; // hides the cursor in the legend
zg1.GraphPane.CurveList.Add(lineItem);
PointPairList PPLa = new PointPairList();
PointPairList PPLb = new PointPairList();
PointPairList PPLc = new PointPairList();
PointPairList PPLd = new PointPairList();
PointPairList PPLf = new PointPairList();
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
List<string> newlistString = new List<string>();
DateTime end = dateTimePicker2.Value.Date;
DateTime start = dateTimePicker1.Value.Date;
newlistString = ctrscan.AnalyzeOldScans(start, end);
for (int h = 0; h <= (newlistString.Count - 1); h++)
{
if (newlistString[h].Equals("A"))
{
a = a + 1;
}
if (newlistString[h].Equals("B"))
{
b = b + 1;
}
if (newlistString[h].Equals("C"))
{
c = c + 1;
}
if (newlistString[h].Equals("D"))
{
d = d + 1;
}
if (newlistString[h].Equals("F"))
{
f = f + 1;
}
double aa = Convert.ToDouble(a);
double bb = Convert.ToDouble(b);
double cc = Convert.ToDouble(c);
double dd = Convert.ToDouble(d);
double ff = Convert.ToDouble(f);
PPLa.Add(h,aa);
PPLa.Add(h, bb);
PPLa.Add(h, cc);
PPLa.Add(h, dd);
PPLa.Add(h, ff);
}
Console.WriteLine(a+" "+b+" "+c+" "+d+" "+f);
BarItem myBara = myPane.AddBar("Quality A", PPLa, Color.Red);
BarItem myBarb = myPane.AddBar("Quality B", PPLb, Color.Blue);
BarItem myBarc = myPane.AddBar("Quality C", PPLc, Color.Gray);
BarItem myBard = myPane.AddBar("Quality D", PPLd, Color.Black);
BarItem myBarf = myPane.AddBar("Quality F", PPLf, Color.Pink);
zedGraphControl3.AxisChange();
zg1.AxisChange();
The output from the "consolewriteline" is: 0 15 56 4 9. And therefor I expect at least 4 bars on the graph. But nothing happens. Hope anyone knows..
Thanks.

PPLa.Add(h,aa);
PPLa.Add(h, bb);
PPLa.Add(h, cc);
PPLa.Add(h, dd);
PPLa.Add(h, ff);
needs to be:
PPLa.Add(h,aa);
PPLb.Add(h, bb);
PPLc.Add(h, cc);
PPLd.Add(h, dd);
PPLe.Add(h, ff);
i hope this will work for you

Related

C# UWP Render InkStrokes in InkCanvas Seperately

enter image description here
I have captured points data from bamboo slate,and converted them to Windows.UI.Input.Inking.InkStroke data.Then I put them in a InkPresenter.StrokeContainer rendered like this image above.Strokes sticked to each other,how can I seperate them?
This is my code below.
private void DataDisplay()
{
List<InkPoint> list = new List<InkPoint>();
List<InkStroke> strokes = new List<InkStroke>();
InkDrawingAttributes drawingAttributes1 = new InkDrawingAttributes();
drawingAttributes1.Color = Colors.Black;
drawingAttributes1.Size = new Size(1, 1);
InkStrokeBuilder builder = new InkStrokeBuilder();
builder.SetDefaultDrawingAttributes(drawingAttributes1);
inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes1);
inkCanvas.InkPresenter.IsInputEnabled = true;
foreach (var item in data.Stroke)
{
string[] strArray = item.Split(',');
for (int i = 9; i <= strArray.Length - 5; i += 5)
{
float x = float.Parse(strArray[i]) / 30;
float y = float.Parse(strArray[i + 1]) / 30;
float pressure = float.Parse(strArray[i + 2]) / 1000;
Point point = new Point(x, y);
InkPoint ip = new InkPoint(point, pressure);
list.Add(ip);
}
Matrix3x2 matrix3X2 = new Matrix3x2(1, 0, 0, 1, 0, 0);
InkStroke newStroke = builder.CreateStrokeFromInkPoints(list, matrix3X2);
strokes.Add(newStroke);
}
inkCanvas.InkPresenter.StrokeContainer.AddStroke(strokes);
}

Can we create 3D graph in 4 quadrants in C#.NET winform?

I want to create a 3D Graph in 4 quadrants form in C#.NET. For instant, I could do as shown below. If you see the corner of the chart begins with (-150, -200). I wish to start with (0,0) and the extend into 4 quadrant form.
Kindly enlighten me how can I transform this 3D graph in 4 quadrant form ?
Below is the corresponding code:
void prepare3dChart(Chart chart, ChartArea ca)
{
ca.Area3DStyle.Enable3D = true;
Series s = new Series();
chart.Series.Add(s);
s.ChartType = SeriesChartType.Bubble;
s.MarkerStyle = MarkerStyle.Diamond;
s["PixelPointWidth"] = "100";
s["PixelPointGapDepth"] = "1";
chart.ApplyPaletteColors();
addTestData(chart);
}
void addTestData(Chart chart)
{
Random rnd = new Random(9);
double x = 0, y = 0, z = 0;
for (int i = 0; i < 100; i++)
{
AddXY3d(chart.Series[0], x, y, z);
x = Math.Sin(i / 11f) * 88 + rnd.Next(3);
y = Math.Cos(i / 10f) * 88 + rnd.Next(5);
z = (Math.Sqrt(i * 2f) * 88 + rnd.Next(6));
}
}
int AddXY3d(Series s, double xVal, double yVal, double zVal)
{
int p = s.Points.AddXY(xVal, yVal, zVal);
s.Points[p].Color = Color.Transparent;
return p;
}
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];
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);
renderPoints(data, e.ChartGraphics.Graphics, chart, 6);
}
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();
}
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);
}
}
I want my 3D graph to have 4 quadrants like this:
Thanks #TaW. I got the code right.
void prepare3dChart(Chart chart, ChartArea ca)
{
ca.Area3DStyle.Enable3D = true;
ca.BackColor = Color.Transparent;
ca.AxisX.Minimum = -300;
ca.AxisX.Maximum = 300;
ca.AxisY.Minimum = -300;
ca.AxisY.Maximum = 300;
ca.AxisX.Crossing = 0; // move both axes..
ca.AxisY.Crossing = 0; // to the middle
ca.AxisX.Interval = 50;
ca.AxisY.Interval = 50;
ca.AxisX.MajorGrid.LineColor = Color.LightGray;
ca.AxisY.MajorGrid.LineColor = Color.LightGray;
chart.Series.Clear();
Series s = new Series();
chart.Series.Add(s);
s.ChartType = SeriesChartType.Bubble;
s.MarkerStyle = MarkerStyle.Diamond;
s["PixelPointWidth"] = "100";
s["PixelPointGapDepth"] = "1";
chart.ApplyPaletteColors();
addTestData(chart);
}
You can style the chart easily to look like this by moving both axes to the middle; this is done by setting their Crossings:
The code used is this:
ChartArea ca = chart1.ChartAreas[0];
ca.BackColor = Color.Transparent;
ca.AxisX.Minimum = -300;
ca.AxisX.Maximum = 300;
ca.AxisY.Minimum = -300;
ca.AxisY.Maximum = 300;
ca.AxisX.Crossing = 0; // move both axes..
ca.AxisY.Crossing = 0; // to the middle
ca.AxisX.Interval = 100;
ca.AxisY.Interval = 100;
ca.AxisX.MajorGrid.LineColor = Color.LightGray;
ca.AxisY.MajorGrid.LineColor = Color.LightGray;
Note that the real problem with MSChart in 3d-mode always remains the z-axis because there really isn't one. (If anybody is interested in how one can simulate it by having a lot of series see here. My example uses 32 series..)
You could draw it yourself, using the built-in function for the 3d conversions (TransformPoints) but this is a lot of tedious work, especially when it comes to labelling..
The updated code :
void prepare3dChart(Chart chart, ChartArea ca)
{
ca.Area3DStyle.Enable3D = true;
ca.BackColor = Color.Transparent;
ca.AxisX.Minimum = -300;
ca.AxisX.Maximum = 300;
ca.AxisY.Minimum = -300;
ca.AxisY.Maximum = 300;
ca.AxisX.Crossing = 0; // move both axes..
ca.AxisY.Crossing = 0; // to the middle
ca.AxisX.Interval = 50;
ca.AxisY.Interval = 50;
ca.AxisX.MajorGrid.LineColor = Color.LightGray;
ca.AxisY.MajorGrid.LineColor = Color.LightGray;
chart.Series.Clear();
Series s = new Series();
chart.Series.Add(s);
s.ChartType = SeriesChartType.Bubble;
s.MarkerStyle = MarkerStyle.Diamond;
s["PixelPointWidth"] = "100";
s["PixelPointGapDepth"] = "1";
chart.ApplyPaletteColors();
addTestData(chart);
}

Multiple Plots In Accord.NET Framework

I have a data set , and hypothesis function, but cant combine them into one, any advices?
i used
ScatterplotBox.Show("Training data",x,y);
ScatterplotBox.Show("Training data",area.ToArray(),h);
You can use ScatterplotView instead, but you're also going to need a Form to hold it. Take a look:
static void Main(string[] args)
{
Random r = new Random();
int max = 15;
double[] x = new double[max];
double[] y1 = new double[max];
double[] y2 = new double[max];
for (int i = 0; i < max; i++)
{
x[i] = i;
y1[i] = r.Next(0, 50);
y2[i] = r.Next(50, 100);
}
ScatterplotView spv = new ScatterplotView();
spv.Dock = DockStyle.Fill;
spv.LinesVisible = true;
spv.Graph.GraphPane.AddCurve("Curve 1", x, y1, Color.Red, SymbolType.Circle);
spv.Graph.GraphPane.AddCurve("Curve 2", x, y2, Color.Blue, SymbolType.Diamond);
spv.Graph.GraphPane.AxisChange();
Form f1 = new Form();
f1.Width = 600;
f1.Height = 400;
f1.Controls.Add(spv);
f1.ShowDialog();
Console.ReadLine();
}

How to find canvas created by for loop in wpf

I've got function:
private string[] Letters = new string[11] {"", "A", "B", "C", "D", "E","F", "G","H", "I", "J"};
private void GenerateGameMap()
{
int LocA = 5;
int LocB = 5;
for(int i = 1; i < 12; i++)
{
for(int i2 = 1; i2 < 12; i2++)
{
Canvas canvas = new Canvas();
canvas.Width = 26;
canvas.Height = 26;
canvas.Margin = new Thickness(LocA + 1, LocB + 1, 0, 0);
Border border = new Border();
border.BorderThickness = new Thickness(2);
if (i == 1 || i2 == 1)
{
Label label = new Label();
label.FontFamily = new FontFamily("Arial");
label.FontSize = 20;
label.Foreground = Brushes.White;
label.Margin = new Thickness(-1, -2, 0, 0);
label.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
label.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
if(i == 1)
{
if(i2 > 1)
{
label.Content = Litery[i2 - 1];
}
}else
{
if(i2==1)
{
label.Content = (i - 1).ToString();
}
}
border.BorderBrush = Brushes.Gold;
canvas.Background = Brushes.Black;
canvas.Children.Add(label);
}else
{
border.BorderBrush = Brushes.CadetBlue;
canvas.Background = Brushes.BurlyWood;
}
if(i > 1 && i2 > 1 )
{
canvas.Name = Letters[i2 - 1] + (i - 1).ToString();
canvas.MouseLeftButtonUp +=canvas_MouseLeftButtonUp;
}
border.Width = 28;
border.Height = 28;
border.Margin = new Thickness(LocA, LocB, 0, 0);
LocA+=30;
MainGameCanvas.Children.Add(canvas);
MainGameCanvas.Children.Add(border);
if(i2 == 11)
{
LocA = 5;
}
}
LocB += 30;
}
}
Everything is fine with that function. But when I'am trying to find canvas:
private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
string canvasName = Text1.Text + Text2.Text;
var GameObject = MainGameCanvas.FindName(canvasName);
Canvas SelectedGameobject = GameObject as Canvas;
SelectedGameobject.Background = Brushes.YellowGreen;
}
I'am receiving error 'Object reference not set on instance of an object' in line "SelectedGameobject.Background = Brushes.YellowGreen;". Is there other way to find this control ?. As i said, I'am sure that function GenerateGameMap() is working corectly.

C# Draw Random Rectangles

i wrote a little code like one from Flappy Bird (game) and in one of my timer i wrote the following code but when i'm starting this timer it's just showing me one up and one down pipe 3 times and then the painting just going black and it's showing me no more pipes. If you guys telling me where's the problem, that would be thankful..
private void timer2_Tick(object sender, EventArgs e)
{
if (Pipe1[0] + PipeWidth <= 0 | start == true)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int[] p1 = { px, py, p2x, p2y };
Pipe1 = p1;
}
else
{
Pipe1[0] = Pipe1[0] - 2;
Pipe1[2] = Pipe1[2] - 2;
}
if (Pipe2[0] + PipeWidth <= 0)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int[] p1 = { px, py, p2x, p2y };
Pipe1 = p1;
}
else
{
Pipe2[0] = Pipe2[0] - 2;
Pipe2[2] = Pipe2[2] - 2;
}
if (start == true)
{
start = false;
}
}
And here is the declares:
int[] Pipe1 = { 0, 0, 0, 0 };
int[] Pipe2 = { 0, 0, 0, 0 };
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;
Here is the Load Form part:
Random rnd = new Random();
int py = rnd.Next(40, (this.Height - PipeDifferentY));
int py2 = py + PipeDifferentY;
int[] p1 = { this.Width, py, this.Width, py2 };
Pipe1 = p1;
py = rnd.Next(40, (this.Height - PipeDifferentY));
py2 = py + PipeDifferentY;
int[] p2 = { this.Width + PipeDifferentX, py, this.Width + PipeDifferentX, py2 };
Pipe2 = p2;
Here is the painting part:
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[0], 0, PipeWidth, Pipe1[1]));
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe1[2], Pipe1[3], PipeWidth, this.Height - Pipe1[3]));
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[0], 0, PipeWidth, Pipe2[1]));
e.Graphics.FillRectangle(Brushes.LightCyan, new Rectangle(Pipe2[2], Pipe2[3], PipeWidth, this.Height - Pipe2[3]));
And the first timer has just:
this.Invalidate();
You have a stray Pipe1 at the bottom of your Pipe2 if statement.
Update:
Try change declares to:
List<int> Pipe1 = new List<int>();
List<int> Pipe2 = new List<int>();
int PipeWidth = 55;
int PipeDifferentY = 140;
int PipeDifferentX = 180;
bool start = true;
and timer function to:
private void timer2_Tick(object sender, EventArgs e)
{
if (Pipe1[0] + PipeWidth <= 0 | start == true)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(px);
Pipe1.Add(py);
Pipe1.Add(p2x);
Pipe1.Add(p2y);
}
else
{
Pipe1[0] = Pipe1[0] - 2;
Pipe1[2] = Pipe1[2] - 2;
}
if (Pipe2[0] + PipeWidth <= 0)
{
Random rnd = new Random();
int px = this.Width;
int py = rnd.Next(40, (this.Height - PipeDifferentY));
var p2x = px;
var p2y = py + PipeDifferentY;
int[] p1 = { px, py, p2x, p2y };
Pipe2.Clear();
Pipe2.Add(px);
Pipe2.Add(py);
Pipe2.Add(p2x);
Pipe2.Add(p2y);
}
else
{
Pipe2[0] = Pipe2[0] - 2;
Pipe2[2] = Pipe2[2] - 2;
}
if (start == true)
{
start = false;
}
}
and your load form part:
Random rnd = new Random();
int py = rnd.Next(40, (this.Height - PipeDifferentY));
int py2 = py + PipeDifferentY;
Pipe1.Clear();
Pipe1.Add(this.Width);
Pipe1.Add(py);
Pipe1.Add(this.Width);
Pipe1.Add(p2y);
py = rnd.Next(40, (this.Height - PipeDifferentY));
py2 = py + PipeDifferentY;
Pipe2.Clear();
Pipe2.Add(this.Width + PipeDifferentX);
Pipe2.Add(py);
Pipe2.Add(this.Width + PipeDifferentX);
Pipe2.Add(p2y);
painting part should be ok

Categories

Resources