Randomise colour of gMap routes - c#

How would I randomise the colour of r.Stroke.Color for every route generated?
var myFile = new CsvFile(#".\netting.csv");
for (int row = 1; row < myFile.Rows.Count; row++)
{
for (int col = 0; col < myFile.Rows[row].Fields.Count; col++)
{
var markersOverlay = new GMapOverlay("markers");
var startMarker = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(myFile.Rows[row].Fields[3]), Convert.ToDouble(myFile.Rows[row].Fields[4])), GMarkerGoogleType.green_dot);
var goalMarker = new GMarkerGoogle(new PointLatLng(Convert.ToDouble(myFile.Rows[row].Fields[5]), Convert.ToDouble(myFile.Rows[row].Fields[6])), GMarkerGoogleType.red_dot);
markersOverlay.Markers.Add(startMarker);
markersOverlay.Markers.Add(goalMarker);
gMapControl1.Overlays.Add(markersOverlay);
var start = new PointLatLng(Convert.ToDouble(myFile.Rows[row].Fields[3]), Convert.ToDouble(myFile.Rows[row].Fields[4]));
var end = new PointLatLng(Convert.ToDouble(myFile.Rows[row].Fields[5]), Convert.ToDouble(myFile.Rows[row].Fields[6]));
var route = GoogleMapProvider.Instance.GetRoute(start, end, false, false, 15);
var r = new GMapRoute(route.Points, "My route");
r.Stroke.Width = 2;
r.Stroke.Color = Color.SeaGreen;
var routesOverlay = new GMapOverlay("routes");
routesOverlay.Routes.Add(r);
gMapControl1.Overlays.Add(routesOverlay);
}
}

You could use a random color method like
Color randomColor(){
System.Random rdm = new Random();
int red = rdm.Next(0,255);
int green = rdm.Next(0,255);
int blue = rdm.Next(0,255);
return Color.FromArgb(red,green,blue);
}
then you could assign that color to your route:
route.Stroke = new Pen(randomColor(),3);

Related

C# - All objects are getting the same values

I am coding a chart. This is my code:
for (int i = 0; i < 2; i++)
{
Val= new ChartValues<ObservablePoint>(); // added
Val.Clear(); // added
lineSeries = new LineSeries
{
Title = "Graph " + i,
Values = Val,
Stroke = Brushes.Blue,
Fill = Brushes.Transparent,
LineSmoothness = 0,
PointGeometry = null
};
for (int jj = 0; jj < 2; jj++)
{
Val.Add(new ObservablePoint
{
X = jj+i+1,
Y = jj+i+1
});
}
Col.Add(lineSeries);
}
The problem is that in my Col variable (collection) all series are getting the latest values of the loop, which results are lineSeries are the same.
When I use the lineSeries = new LineSeries{}, it should create a new independent object right? Or do I have to erase and create it?
Any help is appreciated.
The issue is that you're reusing the same variable Val to store the actual values. Where is it declared, is it a list? Create a new one each time inside the outer loop and then use it to construct the LineSeries.
for (int i = 0; i < 2; i++)
{
var Val = new ChartValues<ObservablePoint>();
for (int jj = 0; jj < 2; jj++)
{
Val.Add(new ObservablePoint
{
X = jj+i+1,
Y = jj+i+1
});
}
var lineSeries = new LineSeries
{
Title = "Graph " + i,
Values = Val,
Stroke = Brushes.Blue,
Fill = Brushes.Transparent,
LineSmoothness = 0,
PointGeometry = null
};
Col.Add(lineSeries);
}
the problem is in line series, you use the same variable, and you are adding the same variable. It it changing each time, and in the end have the last data
lineSeries = new LineSeries
{
......
};
should be
var lineSeries = new LineSeries
{
......
};

How can I save data from a number of arrays into one array?

I want save data from all of these loops, so I would like to know how I could do it:
public Bitmap[] fill(Bitmap[] cr)
{
Card a = new Card();
Card b = new Card();
Card c= new Card();
Card d = new Card();
Bitmap n = new Bitmap(100, 142);
cr = new Bitmap[10];
for (int i = 0; i < 10; i++)
{
d.Num++;
d.Color1 = new SolidBrush(Color.Blue);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, d.Num);
d.DrawCard();
n = d.DrawCard();
cr[i] = n;
}
for (int i = 0; i < 10; i++)
{
a.Num++;
d.Color1 = new SolidBrush(Color.Yellow);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, a.Num);
d.DrawCard();
n = d.DrawCard();
cr[i] = n;
}
for (int i = 0; i < 10; i++)
{
b.Num++;
d.Color1 = new SolidBrush(Color.Red);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, b.Num);
d.DrawCard();
n = d.DrawCard();
cr[i] = n;
}
for (int i = 0; i < 10; i++)
{
c.Num++;
d.Color1 = new SolidBrush(Color.Green);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, c.Num);
d.DrawCard();
n = d.DrawCard();
cr[i] = n;
}
// _deck = new Card[n];
test.Add(cr);
return cr;
}
I tried using an ArrayList but it only gives me data for the last loop only.
It's not really clear what you are trying to do. However, if you mean to return from your method an array (Bitmap[]) that contains in it, each Bitmap instance that is assigned to the variable n in each loop, the general approach would be:
Create a List<Bitmap> to which the objects can be added:
List<Bitmap> cr = new List<Bitmap>();
Then at the end of each loop, instead of cr[i] = n;, add the Bitmap object to your list:
cr.Add(n);
Finally, at the end of the method copy the elements from the list to an array:
return cr.ToArray();
The whole thing winds up looking like this:
public Bitmap[] fill()
{
List<Bitmap> cr = new List<Bitmap>();
Card a = new Card();
Card b = new Card();
Card c= new Card();
Card d = new Card();
Bitmap n;
for (int i = 0; i < 10; i++)
{
d.Num++;
d.Color1 = new SolidBrush(Color.Blue);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, d.Num);
n = d.DrawCard();
cr.Add(n);
}
for (int i = 0; i < 10; i++)
{
a.Num++;
d.Color1 = new SolidBrush(Color.Yellow);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, a.Num);
n = d.DrawCard();
cr.Add(n);
}
for (int i = 0; i < 10; i++)
{
b.Num++;
d.Color1 = new SolidBrush(Color.Red);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, b.Num);
n = d.DrawCard();
cr.Add(n);
}
for (int i = 0; i < 10; i++)
{
c.Num++;
d.Color1 = new SolidBrush(Color.Green);
d.Color2 = new SolidBrush(Color.White);
d = new Card(d.Color1, d.Color2, c.Num);
n = d.DrawCard();
cr.Add(n);
}
return cr.ToArray();
}
Note that your original code had a number of other extraneous bits, mostly to do with allocating new objects that were promptly discarded. So there are some changes above that reflect cleaning up those errors as well. There are probably even more logic errors in the above code as well as the program as a whole, but I am ignoring those possibilities here, addressing only those things that seem clearly to be mistakes.

how to add the two list data defined in their respective loop in C#

public bool AddPosPromotionalMaster(FormCollection frm, POSPromotionalMaster posprom)
{
var promitemcode = frm["PItemCode"].Split(',');
var promuom = frm["PUom"].Split(',');
var salerate = frm["SaleRate"].Split(',');
var discqty = frm["Discount"].Split(',');
var discpercent = frm["DiscountPercentage"].Split(',');
var discprice = frm["DiscountPrice"].Split(',');
var poscode = frm["PosCode"].Split(',');
var days = frm["WDay"].Split(',');
var fromtime = frm["FromTime"].Split(',');
var totime = frm["ToTime"].Split(',');
var itemcodelength = promitemcode.Length;
var poscodelength = poscode.Length;
var dayslength = days.Length;
var posprommasters = new POSPromotionalMaster();
List<POSPromotionalMaster> finallist = new List<POSPromotionalMaster>();
for (int p = 0; p < poscodelength; p++)
{
for (int d = 0; d < dayslength; d++)
{
for (int i = 0; i < itemcodelength; i++ )
{
List<POSPromotionalMaster> pospromlist = new List<POSPromotionalMaster>();
posprommasters.PromCode = posprom.PromCode;
posprommasters.PromDate = posprom.PromDate;
posprommasters.PromDesc = posprom.PromDesc;
posprommasters.Type = posprom.Type;
posprommasters.BasedItemCode = posprom.BasedItemCode;
posprommasters.PItemDesc = posprom.PItemDesc;
posprommasters.BasedUom = posprom.BasedUom;
posprommasters.PItemCode = promitemcode[i];
posprommasters.PUom = promuom[i];
posprommasters.SaleRate = Convert.ToDecimal(salerate[i]);
posprommasters.Discount = Convert.ToDecimal(discqty[i]);
posprommasters.DiscountPrice = Convert.ToDecimal(discprice[i]);
posprommasters.FromDate = posprom.FromDate;
posprommasters.ToDate = posprom.ToDate;
posprommasters.PosCode = poscode[p];
posprommasters.WDay = days[d];
posprommasters.FromTime = Convert.ToDateTime(fromtime[d]);
posprommasters.ToTime = Convert.ToDateTime(totime[d]);
posprommasters.CreatedBy = posprom.CreatedBy;
posprommasters.CreatedOn = posprom.CreatedOn;
posprommasters.CheckedBy = posprom.CheckedBy;
posprommasters.CheckedOn = posprom.CheckedOn;
posprommasters.AuthorizedBy = posprom.AuthorizedBy;
posprommasters.AuthorizedOn = posprom.AuthorizedOn;
posprommasters.CheckerComment = posprom.CheckerComment;
posprommasters.AuthorizedComment = posprom.AuthorizedComment;
posprommasters.DiscountPercentage = Convert.ToDecimal(discpercent[i]);
pospromlist.Add(posprommasters);
}
}
}
bool flg = pospromdal.AddPosPromotional(pospromlist);
return flg;
}
This is my Code and I want to add the pospromlist data to the finallist.
Since the pospromlist is defined inside the loop that's why I can't able to add the data into its externally defined list.
You need to move the declaration of posprommasters to where you define pospromlist, and remove pospromlist altogether, replacing it with finallist.
posprommasters needs to be defined and created on each iteration; otherwise, you would end up with all items of your list being identical to each other.
List<POSPromotionalMaster> finallist = new List<POSPromotionalMaster>();
for (int p = 0; p < poscodelength; p++) {
for (int d = 0; d < dayslength; d++) {
for (int i = 0; i < itemcodelength; i++ ) {
var posprommasters = new POSPromotionalMaster();
...
finallist.Add(posprommasters);
}
}
}

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)
}

How do i add a checkBox near each line?

int counter;
CheckBox[] _cbs;
ScrollLabel._lines contain 151l ines.
counter is int
I want that on line 0 and then each 3 lines to add a checkbox in the beginning of the line with one space to the right so there will be one space place between the text in the line and the checkBox.
Now im getting exception in the loop on the line:
private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "stop")
{
this.button1.Text = "start";
this.scrollLabel1.StopTimer();
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
counter += 3;
_cbs[i] = new CheckBox();
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
}
else
{
this.button1.Text = "stop";
this.scrollLabel1.MilliSecsSpeed = (int)this.numericUpDown1.Value;
this.scrollLabel1.StartTimer();
}
}
_cbs[i] = new CheckBox();
_cbs is null.
This is the constructor:
counter = 0;
this.scrollLabel1.MouseWheel += new MouseEventHandler(ScrollLabel1_MouseWheel);
this.scrollLabel1.MouseEnter += ScrollLabel1_MouseEnter;
readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
RssReader.CnnRss();
this.DoubleBuffered = true;
this.scrollLabel1.Text = readableRss;//File.ReadAllText(#"Demo.txt");
this.scrollLabel1.MilliSecsSpeed = (int)this.numericUpDown1.Value;
this.scrollLabel1.YStep = (float)this.numericUpDown2.Value;
this.scrollLabel1.Words = new List<WordColor>();
this.scrollLabel1.Words.Add(new WordColor() { WordOrText = "scrollLabel1", ForeColor = Color.Red, ColorOnlyThisWord = true, BackColor = Color.LightBlue });
this.scrollLabel1.Words.Add(new WordColor() { WordOrText = "using", ForeColor = Color.Blue, DrawRect = true, RectColor = Color.Black, BackColor = Color.Wheat });
this.scrollLabel1.PopLinesOnNonBmpMode = this.checkBox6.Checked;
this.scrollLabel1.BitmapModus = this.checkBox4.Checked;
this.scrollLabel1.TextLayoutCentered = this.checkBox5.Checked;
this.scrollLabel1.AdditionalLinesAtEnd = (int)this.numericUpDown3.Value;
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
_cbs = new CheckBox[i];
}
This is how the loop in the button click look like now:
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
counter += 3;
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
But all the _cbs are null inside.
EDIT**
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
_cbs[i] = new CheckBox();
counter += 3;
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
Not getting null but i dont see checkBox near/in the beginning of each 3 lines why ?
_cbs is an array which you have declared and you are reallocating in your constructor:
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
_cbs = new CheckBox[i]; // This line reallocates the array as a larger array each time through the loop!
}
You need to define the array only once:
CheckBox[] _cbs = new CheckBox[ScrollLabel._lines.Length];
Then you allocate individual checkboxes to the elements of the array:
for (int i = 0; i < ScrollLabel._lines.Length; i++) {
_cbs[i] = new CheckBox(); // This allocates a new checkbox for the i-th element
}
To place a checkbox on every third line, try something like this:
int lineHeight = 20; // pixels, set this to whatever your line height is
int checkboxInterval = lineHeight * 3; // every third line
int numCheckboxes = ScrollLabel._lines.Length / 3;
for (int i = 0; i < numCheckboxes; i++) {
_cbs[i] = new CheckBox();
_cbs[i].Location = new Point(0, i * checkboxInterval);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
Note that you only need one-third as many checkboxes as the number of lines.

Categories

Resources