setting fixed values to X axis of a chart - c#

The problem is that I want to set some fixed values for 3 types of charts:
First is a week chart so it need to have Sunday to Saturday values on X axis.
Second is month so it have to set the days of the current month
The last one its a year chart that need to show months from 1 to 12 or jan to dec.
I did watch a lot of tutorials but any of those set points like I want and most of them teach how to set X and Y points, but I want a fixed X point and get the Y point from the DB.

To get these fixed ranges :
I have set up the chart like so:
private void rb_range_CheckedChanged(object sender, EventArgs e)
{
Chart chart = chart8;
Series s = chart.Series[0];
s.ChartType = SeriesChartType.Line;
s.XValueType = ChartValueType.DateTime;
s.YValueType = ChartValueType.Double;
Axis ax = chart.ChartAreas[0].AxisX;
Axis ay = chart.ChartAreas[0].AxisY;
//ax.IsMarginVisible = true; // max or may be necessary
ax.Interval = 1;
if (rb_week.Checked)
{
setValues('w', 123);
ax.IntervalType = DateTimeIntervalType.Days;
ax.LabelStyle.Format = "dddd";
}
else if (rb_month.Checked)
{
setValues('m', 123);
ax.IntervalType = DateTimeIntervalType.Days;
ax.LabelStyle.Format = "dd";
}
else if (rb_year.Checked)
{
setValues('y', 123);
ax.IntervalType = DateTimeIntervalType.Months;
ax.LabelStyle.Format = "MMMM";
}
s.Points.Clear();
foreach (var dp in points) s.Points.Add(dp);
// after the points are added or bound to you may want to..
// set the minimum&maximum, but if the data fit you don't have to!
ax.Minimum = points.Min(x => x.XValue);
ax.Maximum = points.Max(x => x.XValue);
}
A few Notes :
It is important to select or bind only those dates that should go into the chart! If you make mistakes here the limits will be off!
If your data are dense enough, that is, if they inclused the 1st and last day of the interval they refer to, you can omit setting the Minimum and Maximum on the x-axis; in that case also include ax.IsMarginVisible = false; to avoid points from the neighboring ranges showing up.
If you data are sparse you may need to determine the Minimum and Maximum values differently than simply picking the first and last x-values. Instead you should pick the correct DateTime values. Note that you need to pick real DateTimes and convert them to double with the ToOADate() function, as the axis properties expect value units.
You can study the code I used to create my data for hints on how to get the Date of the 1st and last day of a given week, month or year..
Note how I use DateTime.DaysInMonth to get the correct number of days in a given month
If you chose Column as ChartType the 1st and last columns may get cut. For this case you can expand the range by adding half a unit to the Maximum and subtracting the same from the Minimum. You may also need to add one such amount to the IntervalOffset.
Here is how I set up the points:
List<DataPoint> points = new List<DataPoint>();
void setValues(char time, int rand)
{
Random rnd = new Random(rand); // random data values
points = new List<DataPoint>();
DateTime dn = DateTime.Now;
DateTime dw = new DateTime(dn.Year, dn.Month, dn.Day % 7 + 1); //my weeks start on monday
DateTime dm = new DateTime(dn.Year, dn.Month, 1);
DateTime dy = new DateTime(dn.Year, 1, 1);
if (time == 'w') for (int i = 0; i < 7; i++)
points.Add(new DataPoint(dw.AddDays(i).ToOADate(), rnd.Next(100) + 50));
if (time == 'm') for (int i = 0; i < DateTime.DaysInMonth(dn.Year, dn.Month); i++)
points.Add(new DataPoint(dm.AddDays(i).ToOADate(), rnd.Next(100) + 50));
if (time == 'y') for (int i = 0; i < 12; i++)
points.Add(new DataPoint(dy.AddMonths(i).ToOADate(), rnd.Next(100) + 50));
}

Related

Find points with maximum visibility in a room-as-grid

I have a 2D grid of cells, like so:
I want to find the "centroids," or the places in each room that can see the most other cells. For example, "centroid" 1 can see 500 other cells, "centroid" 2 can see 400 other cells (NOT included in the first 500), and so on (if there's a better name for this let me know).
I'm currently doing this with the code below.
public void SetCentroids(CellWalkableState[,] grid)
{
centroids = new List<((int, int), int)>();
List<(int, int)> cellsCopy = new List<(int, int)>();
for (int i = 0; i < cells.Count; i++)
{
cellsCopy.Add(cells[i]);
}
Debug.Log(DateTime.Now.ToString("o") + " - Setting centroids for room with " + cells.Count + " cells");
var perCellInView = cellsCopy.AsParallel().Select(x => (x, StaticClass.FindInView(x, grid))).ToList();
var force_start = perCellInView.First();
Debug.Log(DateTime.Now.ToString("o") + " - got in view");
var perCellInViewOrdered = perCellInView.AsParallel().OrderByDescending(xxx => xxx.Item2.Count);
var force_start_1 = perCellInViewOrdered.First();
Debug.Log(DateTime.Now.ToString("o") + " - sorted");
List<(int, int)> roomCellsAdded = new List<(int, int)>();
while(roomCellsAdded.Count < (cells.Count*0.9))
{
if(cellsCopy.Count == 0)
{
Debug.LogError("something is wrong here.");
}
var centroid = perCellInViewOrdered.First().x;
var centroidCells = perCellInViewOrdered.First().Item2;
if(centroidCells.Count == 0)
{
Debug.Log("this shouldnt be happening");
break;
}
roomCellsAdded.AddRange(centroidCells);
centroids.Add((centroid, centroidCells.Count));
Debug.Log(DateTime.Now.ToString("o") + " - added centroids, " + roomCellsAdded.Count + " cells in view");
var loopPerCellInView = perCellInView.AsParallel().Where(x => centroids.Select(y => y.Item1).Contains(x.x) == false).Select(x => (x.x, x.Item2.Except(roomCellsAdded).ToList())).ToList();
Debug.Log(DateTime.Now.ToString("o") + " - excluded");
perCellInViewOrdered = loopPerCellInView.AsParallel().OrderByDescending(xxx => xxx.Item2.Count);
Debug.Log(DateTime.Now.ToString("o") + " - resorted");
}
}
public static List<(int, int)> FindInView((int,int) start, CellWalkableState[,] grid)
{
List<(int, int)> visible = new List<(int, int)>() { start };
bool alive = true;
int r = 1;
var length_x = grid.GetLength(0);
var length_y = grid.GetLength(1);
List<(int, int)> searched = new List<(int, int)>();
List<double> angles = new List<double>();
while(alive)
{
//alive = false;
int newR = r;
int count = CountFromR(newR);
var angleInc = 360.0 / count;
var rNexts = Enumerable.Repeat(1, count).ToArray();
for (int i = 0; i < count; i++)
{
var angle = angleInc * i;
if(angles.Contains(angle) == false)
{
angles.Add(angle);
float cos = Mathf.Cos((float)(Mathf.Deg2Rad * angle));
float sin = Mathf.Sin((float)(Mathf.Deg2Rad * angle));
var b = r;
var p = i % (r * 2);
var d = math.sqrt(math.pow(b, 2) + math.pow(p, 2));
var dScaled = d / r;
bool keepGoing = true;
while(keepGoing)
{
var rCur = dScaled * (rNexts[i]);
var loc = (start.Item1 + Mathf.RoundToInt(rCur * cos), start.Item2 + Mathf.RoundToInt(rCur * sin));
if (searched.Contains(loc) == false)
{
searched.Add(loc);
if (loc.Item1 >= 0 && loc.Item1 < length_x && loc.Item2 >= 0 && loc.Item2 < length_y)
{
if (grid[loc.Item1, loc.Item2] == CellWalkableState.Interactive || grid[loc.Item1, loc.Item2] == CellWalkableState.Walkable)
{
visible.Add(loc);
}
else
{
keepGoing = false;
}
}
else
{
keepGoing = false; // invalid, stop
}
}
else
{
if (visible.Contains(loc) == false)
{
keepGoing = false; // can stop, because we can't see past this place
}
}
if(keepGoing)
{
rNexts[i]++;
}
}
}
}
angles = angles.Distinct().ToList();
searched = searched.Distinct().ToList();
visible = visible.Distinct().ToList();
if(rNexts.All(x => x <= r))
{
alive = false;
}
else
{
r = rNexts.Max();
}
}
return visible;
}
static int CountFromR(int r)
{
return 8 * r;
}
The "short" summary of the code above is that each location first determines what cells around itself it can see. That becomes a list of tuples, List<((int,int), List<(int,int)>)>, where the first item is the location and the second is all cells it views. That main list is sorted by the count of the sublist, such that the item with the most cells-it-can-vew is first. That's added as a centroid, and all cells it can view are added to a second ("already handled") list. A modified "main list" is formed, with each sublist now excluding anything in the second list. It loops doing this until 90% of the cells have been added.
Some output:
2021-04-27T15:24:39.8678545-04:00 - Setting centroids for room with 7129 cells
2021-04-27T15:45:26.4418515-04:00 - got in view
2021-04-27T15:45:26.4578551-04:00 - sorted
2021-04-27T15:45:27.3168517-04:00 - added centroids, 4756 cells in view
2021-04-27T15:45:27.9868523-04:00 - excluded
2021-04-27T15:45:27.9868523-04:00 - resorted
2021-04-27T15:45:28.1058514-04:00 - added centroids, 6838 cells in view
2021-04-27T15:45:28.2513513-04:00 - excluded
2021-04-27T15:45:28.2513513-04:00 - resorted
2021-04-27T15:45:28.2523509-04:00 - Setting centroids for room with 20671 cells
This is just too slow for my purposes. Can anyone suggest alternate methods of doing this? For all of the cells essentially the only information one has is whether they're "open" or one can see through them or not (vs something like a wall).
An approximate solution with fixed integer slopes
For a big speedup (from quadratic in the number of rooms to linear), you could decide to check just a few integer slopes at each point. These are equivalence classes of visibility, i.e., if cell x can see cell y along such a line, and cell y can see cell z along the same line, then all 3 cells can see each other. Then you only need to compute each "visibility interval" along a particular line once, rather than per-cell.
You would probably want to check at least horizontal, vertical and both 45-degree diagonal slopes. For horizontal, start at cell (1, 1), and move right until you hit a wall, let's say at (5, 1). Then the cells (1, 1), (2, 1), (3, 1) and (4, 1) can all see each other along this slope, so although you started at (1, 1), there's no need to repeat the computation for the other 3 cells -- just add a copy of this list (or even a pointer to it, which is faster) to the visibility lists for all 4 cells. Keep heading right, and repeat the process as soon as you hit a non-wall. Then begin again on the next row.
Visibility checking for 45-degree diagonals is slightly harder, but not much, and checking for other slopes in which we advance 1 horizontally and some k vertically (or vice versa) is about the same. (Checking for for general slopes, like for every 2 steps right go up 3, is perhaps a bit trickier.)
Provided you use pointers rather than list copies, for a given slope, this approach spends amortised constant time per cell: Although finding the k horizontally-visible neighbours of some cell takes O(k) time, it means no further horizontal processing needs to be done for any of them. So if you check a constant number of slopes (e.g., the four I suggested), this is O(n) time overall to process n cells. In contrast, I think your current approach takes at least O(nq) time, where q is the average number of cells visible to a cell.

Why when called from another class the graph doesn't get populated?

I have been trying to populate a graph from another class. I have already made the graph public so its accessible to the subclass. When I try to run the entire program on Visual Studio 2019. It shows no error but still the graph does not get populated at all.
I tried implementing the same code from the base class and it works but somehow it doesn't work when i run it from the subclass.
This is the method from the other class called "ClassStatistics" and the base class is called "home". "linechart" is the graph variable name that i am using and is declared in the base class - home.
Am I doing anything wrong here?
DataTable dt = new DataTable();
dt.Columns.Add("X_Value", typeof(DateTime)); //X axis of the type date.
dt.Columns.Add("Y_Value", typeof(double)); //Y axis of type double.
Console.WriteLine("Hello M");
StreamReader sr = new StreamReader(Path.GetFullPath("UserData/Logs") + "\\" + System.DateTime.Today.ToString("MM-yyyy") + "_Logs.txt"); //Path to the log file.
string line;
DayOfWeek day = DateTime.Now.DayOfWeek; //Current day of the week
int days = day - DayOfWeek.Monday; //Keeps track of the number of days left before the next week.
DateTime Start = DateTime.ParseExact(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("dd-MM-yyyy"), "dd-MM-yyyy", CultureInfo.InvariantCulture); //First day of the month
DateTime End = DateTime.ParseExact(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("dd-MM-yyyy"), "dd-MM-yyyy", CultureInfo.InvariantCulture); //Last day of the month
DateTime temp; //stores the date extracted from the log file.
int max = 0;
while ((line = sr.ReadLine()) != null)
{
string[] strarr = line.Split(':'); //Splits the data using ':' for '0' to store the date and '1' to store the number of errors
temp = DateTime.ParseExact(strarr[0], "dd-MM-yyyy", CultureInfo.InvariantCulture); //Extracts the date present in the array
if (temp >= Start && temp <= End) //checks if the given date lies in the same month
{
if (Convert.ToInt32(strarr[1]) > max)
max = Convert.ToInt32(strarr[1]); //Storing the maximum value of Y in max
Console.WriteLine(strarr[1]);
dt.Rows.Add(temp, strarr[1]); //Add data to the data table to show in the graph.
}
}
linechart.DataSource = dt;
linechart.Series["Error-Date"].XValueMember = "X_Value";
linechart.Series["Error-Date"].YValueMembers = "Y_Value";
linechart.Series["Error-Date"].ChartType = SeriesChartType.Line; //Defining the type of chart
linechart.ChartAreas[0].AxisY.Maximum = max; //setting the maximum value of the Y axis
//Change the line colors and grid colors of the chart of both the axes.
linechart.ChartAreas[0].AxisX.LineColor = Color.Black;
linechart.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
linechart.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.White;
linechart.ChartAreas[0].AxisX.Interval = 1;
linechart.ChartAreas[0].AxisY.LineColor = Color.Black;
linechart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Black;
linechart.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.White;
linechart.Series["Error-Date"].BorderWidth = 5;
if (sr != null) sr.Close();

Charting hours wont start at zero

I have a chart control on a winform that should log some counting p hour.
On 12H clock repeat in local PC time.
So the chart starts from 0 to 11
The problem is that when its 12:20 or 12:50 or 12:10
I cannot get the numbering to start at 0 on the X axis
My main inits the chart like :
int[] numbers = new int[11] {12,11,10,91,82,7,66,5,44,3,2,1};
chart1.ChartAreas[0].AxisX.Maximum = 11;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Interval = 1;
Then a loop updates and redraws the chart like below
DateTime currentTime = DateTime.UtcNow.ToLocalTime();
int hour12 = (currentTime.Hour % 12);
numbers[hour12]++;
chart1.Series["total"].Points.DataBindY(numbers);
I also tried but it didnt help here.
chart1.ChartAreas[0].AxisX.IsMarginVisible = false;
Replace
chart1.Series["total"].Points.DataBindY(numbers);
with
chart1.Series["total"].Points.DataBindXY(Enumerable.Range(0,12).ToArray(), numbers);
Update:
Set AxisX so that all chart series shows up correctly:
chart1.ChartAreas[0].AxisX.Maximum = 12;
chart1.ChartAreas[0].AxisX.Minimum =-1;
chart1.ChartAreas[0].AxisX.Interval = 1;
In addition to Sakis to remove -1 and 12 to get the 0..11 scale.
chart1.ChartAreas[0].AxisX.Maximum = 12;
chart1.ChartAreas[0].AxisX.Minimum =-1;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.CustomLabels.Add(-1.5, -0.5, "Hour");
chart1.ChartAreas[0].AxisX.CustomLabels.Add(11.5, 12.5, " ");
for(int i=0;i<12;i++) chart1.ChartAreas[0].AxisX.CustomLabels.Add(i-0.9, i+0.9, i.ToString());

Selecting data intervals on Datachart with Mouse

I use stacked area chart and I want to select a datapoint interval with the mouse like below.
I know that some applications offer this feature however, I couldn't find how to do it.
Could you please show me the right way?
The term you needed is DataVisualization.Charting.Cursor
You can use this combination of properties:
// a few short references:
ChartArea ca = chart1.ChartAreas[0];
Axis ax = ca.AxisX;
var cx = ca.CursorX;
cx.IsUserEnabled = true; // allow a cursor to be placed
cx.IsUserSelectionEnabled = true; // allow it to be used for selecting
ax.ScaleView.Zoomable = false; // prevent from automatically zooming in
Here are the first and last values selected:
var x1 = cx.SelectionStart;
var x2 = cx.SelectionEnd;
Here are the first and last DataPoints selected:
var p1 = s.Points.Select(x => x).Where(x => x.XValue >= x1).First();
var p2 = s.Points.Select(x => x).Where(x => x.XValue <= x2).Last();
And the indices of the first and last DataPointsselected:
var i1 = s.Points.IndexOf(p1);
var i2 = s.Points.IndexOf(p2);
Now you can tell which points were selected:
textBox1.Text += (i2 - i1) + " points selected.\r\n\r\n";
for (int i = i1; i < i2; i++)
{
textBox1.Text += i + ". " + chart1.Series[0].Points[i].ToString() + "\r\n";
chart1.Series[0].Points[i].Color = Color.Red;
}
Note: The code to identify the starting and end points assumes that all DataPoints are added in increasing x-value order. Since you can add DataPoints in any order it will fail for instance when you insert out of order points! In that case you would instead collect the points in the selection (testing for both sides) in a List<DataPoint> and then enumerate this list.

Microsoft Chart Controls and X-Axis time scale format

I have a Microsoft Chart Controls within my winforms app.
I currently play the X and y values within a loop. I also had the X-axis format set as
ChartAreas[0].AxisX.LabelStyle.Format={"00:00:00"}
This worked fine as a time format, however I noticed once my time values went above 60 seconds, (i.e. 00:00:60), rather than the scale moving up to 1 minute (i.e. 00:01:00) it goes to 61 (i.e. 00:00:61) right up to 99 before it goes to one minute (00:00:99) then (00:01:00)
Is there a way to fix this please?
I suspect that LabelStyle.Format property is used in a similar way as in string.Format(mySringFormat,objToFormat).
Hence, given that your underlying X objects type is double, it will just print a colon-separated double (e.g. 4321 will be 00:43:21).
AFAIK, there isn't an easy way to print a double value like a time value using just a string format.
If you can change the code filling the chart, I suggest you to pass DateTime's for the X values, and then you will be able to use custom DateTime formatting, e.g.
"HH:mm:ss", or others
EDIT:
As per your comment:
// create a base date at the beginning of the method that fills the chart.
// Today is just an example, you can use whatever you want
// as the date part is hidden using the format = "HH:mm:ss"
DateTime baseDate = DateTime.Today;
var x = baseDate.AddSeconds((double)value1);
var y = (double)value2;
series.Points.addXY(x, y);
EDIT 2:
Here's a complete example, it should be easy to apply this logic to your code:
private void PopulateChart()
{
int elements = 100;
// creates 100 random X points
Random r = new Random();
List<double> xValues = new List<double>();
double currentX = 0;
for (int i = 0; i < elements; i++)
{
xValues.Add(currentX);
currentX = currentX + r.Next(1, 100);
}
// creates 100 random Y values
List<double> yValues = new List<double>();
for (int i = 0; i < elements; i++)
{
yValues.Add(r.Next(0, 20));
}
// remove all previous series
chart1.Series.Clear();
var series = chart1.Series.Add("MySeries");
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.Auto;
DateTime baseDate = DateTime.Today;
for (int i = 0; i < xValues.Count; i++)
{
var xDate = baseDate.AddSeconds(xValues[i]);
var yValue = yValues[i];
series.Points.AddXY(xDate, yValue);
}
// show an X label every 3 Minute
chart1.ChartAreas[0].AxisX.Interval = 3.0;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
}

Categories