C# Zedgraph Change Line Dash Length - c#

I'm trying to change the length of the Dashes on the Zed Graph line. I would like to have larger gaps between the solid lines.
example of my code
LineItem LineCurve = null
LineCurve = ZedGraphControl.GraphPane.AddCurve("line1",PairPointListData, Color, Symbol);
//now I want to change the dash settings
LineCurve.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
LineCurve.Line.StepType = StepType.ForwardStep;
LineCurve.Line.DashOn = 1.0f;//Not sure what this floating point does
LineCurve.Line.DashOff = 1.0f;//Also not sure
I have changed the values of the Dash On and Off, but I can't see anything noticeable.

Your dash style must be set to 'Custom'. See:
http://zedgraph.sourceforge.net/documentation/html/P_ZedGraph_LineBase_DashOff.htm
Here is some sample code:
double[] xvals = new double[100];
double[] yvals = new double[100];
for (double i = 0; i < xvals.Length; i++)
{
xvals[(int)i] = i / 10;
yvals[(int)i] = Math.Sin(i / 10);
}
var zgc = msGraphControl1.zedGraphControl1;
var lineItem = zgc.GraphPane.AddCurve("Custom", xvals, yvals, Color.Blue);
lineItem.Line.Style = DashStyle.Custom;
lineItem.Line.Width = 3;
lineItem.Line.DashOn = 5;
lineItem.Line.DashOff = 10;
//offset the next curve
for (int i = 0; i < xvals.Length; i++)
{
xvals[i] = xvals[i] + 0.5;
yvals[i] = yvals[i] + 0.05;
}
var lineItem2 = zgc.GraphPane.AddCurve("DashDotDot", xvals, yvals, Color.Red);
lineItem2.Line.Width = 3;
lineItem2.Line.Style = DashStyle.DashDotDot;
//offset the next curve
for (int i = 0; i < xvals.Length; i++)
{
xvals[i] = xvals[i] + 0.5;
yvals[i] = yvals[i] + 0.05;
}
var lineItem3 = zgc.GraphPane.AddCurve("Solid", xvals, yvals, Color.Black);
lineItem3.Line.Width = 3;
lineItem3.Line.Style = DashStyle.Solid;
zgc.AxisChange();
zgc.Refresh();

Related

Different output between Emulator and Samsung Galaxy S3 with Xamarin

I am having issues with the output of the the result of a math calculation. I have a basic average of an array of double and I assign the result to a Label object, using the ToString() method. When I emulate the average, the label shows the correct value of 15.96 for example, but the same average of the same array, on my Galaxy S3 shows 159.6.
Is there anyone who know what's up and what can I do to make the S3 show the correct value?
Thank you all!
EDIT: passing the result to a label and adding the label to the grid:
double result = Math.Round(NP122.DoAverage(parameters), 2);
CustomLabel label = new CustomLabel();
label.ColNo = grid.ColumnDefinitions.IndexOf(c);
label.FontSize = 25;
label.TextColor = Color.Green;
if (result.ToString() == "NaN")
label.Text = "0";
else
label.Text = result.ToString();
label.IsVisible = true;
for (int i = 0; i < numberOfRows.Length + 2; i++) {
if(i == numberOfRows.Length +1)
Grid.SetRow(label, i);
}
Grid.SetColumn(label, grid.ColumnDefinitions.IndexOf(c));
listaRez.Add(label);
foreach (CustomLabel a in listaRez)
{
if (a.ColNo == grid.ColumnDefinitions.IndexOf(c))
{
grid.Children.Add(a);
}
}
EDIT 2: Custom function for NP122.DoAverage:
public static class NP122
{
public static double Vx, sx, Xm, kn, Xkinf, Xksup;
public static double sum;
public static double sumaProvizorie;
public static double[] valoriKn = new double[25];
public static double ValoareCaracteristicaSuperioara(double[] l)
{
Vx = 0;
sx = 0;
Xm = 0;
kn = 0;
Xkinf = 0;
Xksup = 0;
sum = 0;
sumaProvizorie = 0;
valoriKn[0] = 0;
//more here
valoriKn[24] = 0.35;
if (l.Length < 2 )
{
Xksup = 0;
Xkinf = 0;
}
else
{
Xm = (l.Sum()) / (l.Length);
for (int j = 0; j < l.Length; j++)
{
sumaProvizorie = Math.Round(Math.Pow((l[j] - Xm), 2), 2);
sum += sumaProvizorie;
}
kn = valoriKn[l.Length - 1];
double elements = (1.00 / (l.Length - 1));
double putere = sum;
sx = Math.Round(Math.Sqrt(elements * putere), 4);
Vx = sx / Xm;
Xksup = Xm * (1 + kn * Vx);
Xkinf = Xm * (1 - kn * Vx);
}
return Xksup;

How to fill every seat number corresponding to the text button with a loop in C#?

First of all, I'm a rookie in programming and I'm not English, I'm sorry if I had a mistake expressing myself :D
I'm doing a program that represents a booking system bus. I had created 64 buttons at Runtime with an array of buttons, but that isn't the problem.
The problem is to represent each text button corresponding to the number of seats of the bus. Here is my code:
var buttonArray = new Button[64];
for (var i = 1; i <= 4; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Size = new Size(75, 23);
buttonArray[i].Name = "button" + i;
buttonArray[i].Text = "Seat " + i;
buttonArray[i].Location = new Point(-50 + (i*100), 10);
panel1.Controls.Add(buttonArray[i]);
for (var j = 5; j <= 19; j++)
{
buttonArray[j] = new Button();
buttonArray[j].Size = new Size(75, 23);
buttonArray[j].Name = "button" + j;
buttonArray[j].Text = "Seat " + j;
buttonArray[j].Location = new Point(-50 + (i * 100), -105 + (j * 30));
panel1.Controls.Add(buttonArray[j]);
}
}
The result would be 64 buttons represented with 4 columns and 16 rows, for example:
Here's how I would do it: Just loop from zero to 63, and calculate the row and column on the fly. Then you set the button properties and add it to the panel. You can change the size of the seats by adjusting the seatWidth and seatHeight variables. Also note that I added a little extra space between the first two columns and the second two columns, because it looks like that's what you had in your diagram. You can adjust this with the middleRowWidth variable. You can also customize the distance between the seats by adjusting the seatSpacing value:
const int seatSpacing = 4;
const int middleRowWidth = 20;
const int seatWidth = 55;
const int seatHeight = 20;
panel1.Width = 4 * (seatWidth + seatSpacing) + seatSpacing + middleRowWidth;
var buttonSize = new Size(seatWidth, seatHeight);
for (var i = 0; i < 64; i++)
{
// Calculate the location for this seat
int thisRow = i / 4;
int thisColumn = i % 4;
int seatTop = thisRow * (seatHeight + seatSpacing);
int seatLeft = thisColumn * (seatWidth + seatSpacing);
// Add some extra distance down the middle
if (thisColumn >= 2) seatLeft += middleRowWidth;
// Create a new button
var thisButton = new Button
{
Size = buttonSize,
Name = "button" + (i + 1),
Text = "Seat " + (i + 1),
Location = new Point(seatLeft, seatTop),
Visible = true,
};
// Add it to the panel
panel1.Controls.Add(thisButton);
}
Result:
UPDATE
After thinking about how to make this more generic, I thought about how some airplanes have their seats laid out. Some have two seats on each side, some have three, some have two on each side with a column of three down the middle. In order to accommodate different types of layouts, I modified the code slightly. Below code has customizable variables for your layout. The one below is for a 2-3-2 layout, where the numbers are the seat count and - are aisles. It also includes two "exit row" aisles running horizontally:
// Seating Layout
const int numberOfRows = 20;
const int numberOfColumns = 7;
const int numberOfSeats = numberOfRows * numberOfColumns;
var columnsToInsertAisleAfter = new List<int> {1, 4};
var rowsToInsertAisleAfter = new List<int> {6, 12};
// Seat sizing
const int seatWidth = 35;
const int seatHeight = 20;
const int seatSpacing = 4;
const int aisleWidth = 20;
var seatSize = new Size(seatWidth, seatHeight);
// Panel and form layout
panel1.Size = new Size(
numberOfColumns * (seatWidth + seatSpacing) + seatSpacing +
aisleWidth * columnsToInsertAisleAfter.Count,
numberOfRows * (seatHeight + seatSpacing) + seatSpacing +
aisleWidth * rowsToInsertAisleAfter.Count);
this.Size =
new Size(panel1.Size.Width + (panel1.Left * 2),
panel1.Size.Height + (panel1.Top * 2)) +
new Size(this.Width - this.ClientSize.Width,
this.Height - this.ClientSize.Height);
// Add seats to panel
for (var i = 0; i < numberOfSeats; i++)
{
// Calculate the location for this seat
int thisRow = i / numberOfColumns;
int thisColumn = i % numberOfColumns;
int seatTop = thisRow * (seatHeight + seatSpacing);
int seatLeft = thisColumn * (seatWidth + seatSpacing);
// Add some extra distance for aisles
var aislesBeforeThisColumn =
columnsToInsertAisleAfter.Count(col => col < thisColumn);
seatLeft += aislesBeforeThisColumn * aisleWidth;
var aislesBeforeThisRow =
rowsToInsertAisleAfter.Count(row => row < thisRow);
seatTop += aislesBeforeThisRow * aisleWidth;
// Add a new button to the panel
panel1.Controls.Add(new Button
{
Size = seatSize,
Name = "button" + (i + 1),
Text = (i + 1).ToString(),
Location = new Point(seatLeft, seatTop),
Visible = true,
});
}
Large Airplane Layout:
This should do what you want,
Point pt;
int y = 20;
int x = 0;
for (int i = 0; i < 16; i++)
{
x = 20;
for (int j = 1; j <= 4; j++)
{
pt = new Point(x, y);
Button btn = new Button() { Location = pt, Size = new Size(75, 23) };
btn.Name = i.ToString() + j.ToString();
btn.Text = string.Format("{0} {1:D2}", "Seat", i * 4 + j);
panel1.Controls.Add(btn);
x += 90;
}
y += 30;
}

Multiple Plots with shared axes in Oxyplot

I want to add multiple plots with shared x-axis using OXYPLOT library. The example code is as follows and it sets 4 different y-axis sharing the same x-axis. However i can only plot data on the 1st x&y axis but not the others. Any kind of suggestion would be appreciated.
[Example("Untitled")]
public static PlotModel Untitled()
{
var plotModel1 = new PlotModel();
plotModel1.PlotMargins = new OxyThickness(40,20,40,30);
var linearAxis1 = new LinearAxis();
linearAxis1.EndPosition = 0.25;
linearAxis1.Maximum = 1;
linearAxis1.Minimum = -1;
linearAxis1.Title = "C1";
linearAxis1.Key= "C1";
plotModel1.Axes.Add(linearAxis1);
var linearAxis2 = new LinearAxis();
linearAxis2.EndPosition = 0.5;
linearAxis2.Maximum = 1;
linearAxis2.Minimum = -1;
linearAxis2.Position = AxisPosition.Right;
linearAxis2.StartPosition = 0.25;
linearAxis2.Title = "C2";
linearAxis2.Key= "C2";
plotModel1.Axes.Add(linearAxis2);
var linearAxis3 = new LinearAxis();
linearAxis3.EndPosition = 0.75;
linearAxis3.Maximum = 1;
linearAxis3.Minimum = -1;
linearAxis3.StartPosition = 0.5;
linearAxis3.Title = "C3";
linearAxis3.Key= "C3";
plotModel1.Axes.Add(linearAxis3);
var linearAxis4 = new LinearAxis();
linearAxis4.Maximum = 1;
linearAxis4.Minimum = -1;
linearAxis4.Position = AxisPosition.Right;
linearAxis4.StartPosition = 0.75;
linearAxis4.Title = "C4";
linearAxis1.Key= "C4";
plotModel1.Axes.Add(linearAxis4);
var linearAxis5 = new LinearAxis();
linearAxis5.Maximum = 100;
linearAxis5.Minimum = 0;
linearAxis5.Position = AxisPosition.Bottom;
linearAxis5.Title = "s";
linearAxis5.Key= "s";
plotModel1.Axes.Add(linearAxis5);
return plotModel1;
}
Assign the XAxisKey and YAxisKey propertiy to your serises.
PlotModel pm = new PlotModel();
OxyPlot.Series.FunctionSeries s1 = new FunctionSeries(Math.Sin, -10, 10, 0.1, "sin(x)");
s1.YAxisKey = "axesY2";
s1.XAxisKey = "axesX2";
pm.Series.Add(s1);
In your case, the key is "C1", "C2", and "C3", etc.
As said before, you have to use the YAxisKey and XAxisKey attributes combined with the StartPosition and EndPosition. The position is in percentage (from 0 to 1) so for example, if you want to divide equally the Y axes of graph, you can try code like this:
float percentage = 1f / NumberOfGraphs;
for (int i = 1; i <= NumberOfGraphs; i++) {
...
LinearAxis yAxes = new LinearAxis();
yAxes.Position = OxyPlot.Axes.AxisPosition.Left;
yAxes.StartPosition = (i - 1) * percentage;
yAxes.EndPosition = i * percentage;
yAxes.Key = "Y" + i;
...
LineSeries lineSerie = new LineSeries();
lineSerie.YAxisKey = "Y" + i;
...
yourPlotView.Model.Series.Add(lineSerie)
}

Display evenly distributed data points in Mschart

I have looked at "similar" questions but can't actually find anything that applies, surprisingly. I calculate how many data points I would like to create on my Mschart Line Chart with a sum using imported converted data. Using the result of this, I divide it by another number that varies and this is the space I want to have between each datapoint. 124 is constant.
300 (varies) / 124 (X-Axis) = 2.419354838709677 (this is the space I want between each point)
I thought about creating a for loop like so
for (double i = 1; i < samples_hd; i++)
{
// distribute data points evenly here
}
A file is going to be read through into the chart, so far I have manually added data points as I'm not sure at all how to add them like above ^.
var sn = new System.Windows.Forms.DataVisualization.Charting.Series(English.Sam_Num);
sn.ChartType = SeriesChartType.Line;
sn.Points.Add(new DataPoint(9, 30));
sn.Points.Add(new DataPoint(150, 28));
sn.XAxisType = AxisType.Primary;
Chart_Line.Series.Add(sn);
Chart_Line.ChartAreas[0].AxisX.Minimum = 0;
Chart_Line.ChartAreas[0].AxisX.Maximum = 124;
Chart_Line.ChartAreas[0].AxisX.Interval = 15;
Chart_Line.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
It took such a long time to get it working, but it works :) My graph actually has 3 series, but for the example I will just show the 2 series.
private void Main(byte[] rec_arr)
{
var series1 = new Series("series1");
series1 .ChartType = SeriesChartType.Line;
series1 .MarkerStyle = MarkerStyle.Circle;
series1 .MarkerSize = 3;
series1 .YAxisType = AxisType.Primary;
series1 .Color = Color.Navy;
var series2 = new Series("series2");
series2 .ChartType = SeriesChartType.Line;
series2 .MarkerStyle = MarkerStyle.Triangle;
series2 .MarkerSize = 3;
series2 .YAxisType = AxisType.Primary;
series2 .Color = Color.Crimson;
foreach (var series in Chart1.Series)
{
series.Points.Clear();
}
double interval1 = 0;
double interval2 = File_Details.time / (double)124;
int Offset2 = 502;
int Offset3 = 750;
float data1, data2;
if (File_Details.time == 0)
{
Label_Error_Graph.Visible = true;
Chart1.ChartAreas[0].AxisX.Maximum = 0;
Chart1.ChartAreas[0].AxisX.Minimum = 0;
Chart1.Series[0].Points.Add(0);
Chart1.Series[1].Points.Add(0);
}
else
{
Label_Error_Graph.Visible = false;
}
for (interval1 = 0; interval1 < File_Details.time; interval1 += interval2)
{
data1 = DecodeSingle(rec_arr, Offset3);
if (Chart1.Series.IndexOf("series1") == -1)
{
Chart1.Series.Add(series1);
}
Offset3 = Offset3 + 2;
if (data1 < 300)
{
Chart1.Series[0].Points.AddXY(interval1, supply);
}
else
{
Chart1.Series[0].Points.AddXY(interval1, 300);
}
}
for (interval1 = 0; interval1 < File_Details.time; interval1 += interval2)
{
data2 = DecodeSingle(rec_arr, Offset2) / (float)100;
if (Chart1.Series.IndexOf("series2") == -1)
{
Chart1.Series.Add(series2);
}
Offset2 = Offset2 + 2;
if (data2 < 150)
{
Chart1.Series[1].Points.AddXY(interval1, data2);
}
else
{
Chart1.Series[1].Points.AddXY(interval1, 150);
}
}
}

Can't add curve to the ZedGraph in C#

I try to add multiple curves and yaxises using ZedGraph. But I added points to the first curve succesfully after I tried to add the second curve. The first one values' disappear and
myCurve.Points.Count equals 0. For example, if I add 6 curves, only the sixth one has values others count =0. Also any of them show up on the graph. Here is the code:
colors = new Color[ff.documentColumnCount + 4];
zedGraphControl1.IsShowPointValues = true;
myPane = zedGraphControl1.GraphPane;
LineItem myCurve;
Color[] colors;
myPane.XAxis.Type = ZedGraph.AxisType.Date;
myPane.XAxis.Scale.Format = "HH:mm:ss";
myPane.XAxis.Scale.MajorUnit = DateUnit.Second;
zamanValue = new double[ff.tarihSaat.Length - 4]; // x axis time values. ff is another windows form name, no problem here.
for (int i = 0; i < ff.tarihSaat.Length - 4; i++)
{
zamanValue[i] = (double)new XDate(ff.tarihSaat[i].Year,
ff.tarihSaat[i].Month,
ff.tarihSaat[i].Day,
ff.tarihSaat[i].Hour,
ff.tarihSaat[i].Minute,
ff.tarihSaat[i].Second);
counter++;
}
yaxisArray = new YAxis[ff.documentColumnCount + 4]; // temp y axises
for (int k = 0; k < chckboxNumber; k++)
{
tempPointPairList.Clear();
tempPointPairList = createPairPointList(k); // Creates points, I see the correct values everytime, also no problem here.
minYvalues[k] = Findmin(tempPointPairList);
maxYvalues[k] = FindMax(tempPointPairList);
myCurve = myPane.AddCurve(ff.columnNames[k + 3], tempPointPairList, colors[k], SymbolType.None);
myCurve.Line.Width = 2.5f;
//myCurve.IsVisible = true;
myCurve.YAxisIndex = k;
myCurve.IsVisible = true;
if (k == 0)
{
myPane.YAxis.IsVisible = true;
myPane.YAxis.Scale.Max = 1;
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.MajorStep = (myPane.YAxis.Scale.Max - myPane.YAxis.Scale.Min) / 10;
myPane.YAxis.MajorGrid.IsVisible = true;
}
else
{
yaxisArray[k] = new YAxis(ff.columnNames[k + 3]);
//yaxisArray[k].Color = colors[k];
yaxisArray[k].IsVisible = false;
yaxisArray[k].Title.IsVisible = false;
myPane.YAxisList.Add(yaxisArray[k]);
if (minYvalues[k] == maxYvalues[k])
{
yaxisArray[k].Scale.Min = minYvalues[k] - 0.1;
yaxisArray[k].Scale.Max = maxYvalues[k] + 0.1;
}
else
{
yaxisArray[k].Scale.Min = minYvalues[k];
yaxisArray[k].Scale.Max = maxYvalues[k];
}
myPane.YAxisList.Add(yaxisArray[k]);
}
yAxisListIndexes[k] = myPane.YAxisList.Count-1;
minTextBoxes[k].Text = minYvalues[k].ToString();
maxTextBoxes[k].Text = maxYvalues[k].ToString();
durum[k].previousState = 1;
durum[k].currentState = 1;
chckBoxList[k].Checked = true;
myCurve.Clear();
}
myPane.XAxis.Scale.Min = zamanValue[0];
myPane.XAxis.Scale.Max = zamanValue[zamanValue.Length - 1];
//myPane.YAxisList[0].IsVisible = true;
zedGraphControl1.AxisChange();
zedGraphControl1.Invalidate();
zedGraphControl1.Refresh();
Where is the mistake?
You don't add curves to each other, you add them to myPane.CurveList so you have them in myPane.CurveList[0], myPane.CurveList[1] and so on, not in myCurve. myCurve serves as store for current curve you are working with. When you call
myCurve = myPane.AddCurve(ff.columnNames[k + 3], tempPointPairList, colors[k], SymbolType.None);
a brand new curve is created, added to myPane.CurveList and is written into myCurve variable. It has a fresh state as it's just created. You can access your previous curve(s) in myPane.CurveList.

Categories

Resources