Chart Control Y axis auto-scale on scrolling - c#

I've been searching the net for some time now yet still haven't found any good solution to my problem. I want to make MS Chart to automatically rescale Y axis on scrolling to make sure that all data points are visible. The twist here is that I need to have the ability to exclude certain series from being used for auto scale. So far I only found solutions that offer to iterate through the entire point collection on AxisViewChanged event, which doesn't work well when you have large collections of points and a few series to iterate through. I was wondering if there was any way to narrow the search by obtaining data points that are between currently visible min and max X values. Any help would be appreciated.
Edit Heres the image. As you can see the candlesticks in the middle aren't entirely visible.

you can try this code
DateTime date = DateTime.Now;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 20;
Random r = new Random((int)date.Ticks);
chart1.Series[0].ChartType = SeriesChartType.Candlestick;
chart1.Series[0].Color = Color.Green;
chart1.Series[0].XValueType = ChartValueType.Time;
chart1.Series[0].IsXValueIndexed = true;
chart1.Series[0].YValuesPerPoint = 4;
chart1.Series[0].CustomProperties = "MaxPixelPointWidth=10";
for (int i = 0; i < 100; i++ )
{
DataPoint point = new DataPoint(date.AddHours(i).ToOADate(), new double[] { r.Next(10, 20), r.Next(30, 40), r.Next(20, 30), r.Next(20, 30) });
chart1.Series[0].Points.Add(point);
}
int min = (int)chart1.ChartAreas[0].AxisX.Minimum;
int max = (int)chart1.ChartAreas[0].AxisX.Maximum;
if (max > chart1.Series[0].Points.Count)
max = chart1.Series[0].Points.Count;
var points = chart1.Series[0].Points.Skip(min).Take(max - min);
var minValue = points.Min(x => x.YValues[0]);
var maxValue = points.Max(x => x.YValues[1]);
chart1.ChartAreas[0].AxisY.Minimum = minValue;
chart1.ChartAreas[0].AxisY.Maximum = maxValue;

Use a query to find out which series you want to use for finding ymin and ymax in the code.
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
if (e.Axis.AxisName == AxisName.X)
{
int start = (int)e.Axis.ScaleView.ViewMinimum;
int end = (int)e.Axis.ScaleView.ViewMaximum;
// Series ss = chart1.Series.FindByName("SeriesName");
// use ss instead of chart1.Series[0]
double[] temp = chart1.Series[0].Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[0]).ToArray();
double ymin = temp.Min();
double ymax = temp.Max();
chart1.ChartAreas[0].AxisY.ScaleView.Position = ymin;
chart1.ChartAreas[0].AxisY.ScaleView.Size = ymax - ymin;
}
}

This is a minor improvement on the excellent submission from Shivaram K R, to prevent open, close and low dropping off the bottom for the lowest points on financial charts with four Y values: high, low, open close.
// The following line goes in your form constructor
this.chart1.AxisViewChanged += new EventHandler<ViewEventArgs> (this.chart1_AxisViewChanged);
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
if (e.Axis.AxisName == AxisName.X)
{
int start = (int)e.Axis.ScaleView.ViewMinimum;
int end = (int)e.Axis.ScaleView.ViewMaximum;
// Use two separate arrays, one for highs (same as temp was in Shavram's original)
// and a new one for lows which is used to set the Y axis min.
double[] tempHighs = chart1.Series[0].Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[0]).ToArray();
double[] tempLows = chart1.Series[0].Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[1]).ToArray();
double ymin = tempLows.Min();
double ymax = tempHighs.Max();
chart1.ChartAreas[0].AxisY.ScaleView.Position = ymin;
chart1.ChartAreas[0].AxisY.ScaleView.Size = ymax - ymin;
}
}

Based on previous answers
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
if(e.Axis.AxisName == AxisName.X)
{
int start = (int)e.Axis.ScaleView.ViewMinimum;
int end = (int)e.Axis.ScaleView.ViewMaximum;
List<double> allNumbers = new List<double>();
foreach(Series item in chart1.Series)
allNumbers.AddRange(item.Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[0]).ToList());
double ymin = allNumbers.Min();
double ymax = allNumbers.Max();
chart1.ChartAreas[0].AxisY.ScaleView.Position = ymin;
chart1.ChartAreas[0].AxisY.ScaleView.Size = ymax - ymin;
}
}
It could be you have more series in the chartarea. In this case you pick the high and low of all series in the area instead of just one.
regards,
Matthijs

Above answers were very helpful for me. However, I have a chart with multiple charting areas. I have adapted the code to scale up to all chart areas:
foreach (ChartArea area in chart1.ChartAreas)
{
List<double> allNumbers = new List<double>();
foreach (Series item in chart1.Series)
if (item.ChartArea == area.Name)
allNumbers.AddRange(item.Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[0]).ToList());
double ymin = allNumbers.Min();
double ymax = allNumbers.Max();
if (ymax > ymin)
{
double offset = 0.02 * (ymax - ymin);
area.AxisY.Maximum = ymax + offset;
area.AxisY.Minimum = ymin - offset;
}
}

Related

Highlight winforms MS Chart line on hover

I'm trying to use MS Chart with custom controls. My purpose is to:
Highlight only a segment of the line that connects two neighboring points on mouse hover over that piece
Find indexes of those two neighboring points (I need that for being able to drag that line by moving two points simultaneously)
Kind of illustration:
For now I can detect a hover over a line on the chart by using the approach described here. But I'm stuck in finding indexes or at least coordinates of those two points.
So the original idea from this question was to find nearest points by x (assuming that all the series has x-values are indeed steadily increasing) and then calculate y-value. But I have a little improved that and added support for completely vertical lines. So here is my code for capturing the needed line:
private static GrippedLine? LineHitTest(Series series, double xPos, double yPos, Axis xAxis, Axis yAxis)
{
double xPixelPos = xAxis.PixelPositionToValue(xPos);
double yPixelPos = yAxis.PixelPositionToValue(yPos);
DataPoint[] neighbors = new DataPoint[2];
neighbors[0] = series.Points.Last(x => x.XValue <= xPixelPos);
neighbors[1] = series.Points.First(x => x.XValue >= xPixelPos);
DataPoint[] verticalMates;
foreach (DataPoint neighbor in neighbors)
{
if (Math.Abs(neighbor.XValue - xPixelPos) < LINE_GRIP_REGION)
{
verticalMates = series.Points.FindAllByValue(neighbor.XValue, "X").ToArray();
if (verticalMates.Length > 1)
{
if (verticalMates.Length > 2)
{
if (verticalMates[0].YValues[0] < verticalMates[verticalMates.Length - 1].YValues[0])
{
neighbors[0] = verticalMates.LastOrDefault(y => y.YValues[0] < yPixelPos);
neighbors[1] = verticalMates.FirstOrDefault(y => y.YValues[0] >= yPixelPos);
}
else
{
neighbors[0] = verticalMates.LastOrDefault(y => y.YValues[0] > yPixelPos);
neighbors[1] = verticalMates.FirstOrDefault(y => y.YValues[0] <= yPixelPos);
}
}
else
{
neighbors[0] = verticalMates[0];
neighbors[1] = verticalMates[1];
}
break;
}
}
}
double x0 = xAxis.ValueToPixelPosition(neighbors[0].XValue);
double y0 = yAxis.ValueToPixelPosition(neighbors[0].YValues[0]);
double x1 = xAxis.ValueToPixelPosition(neighbors[1].XValue);
double y1 = yAxis.ValueToPixelPosition(neighbors[1].YValues[0]);
double Yinterpolated = y0 + (y1 - y0) * (xPos - x0) / (x1 - x0);
int[] linePoints = new int[2];
// if mouse Y position is near the calculated OR the line is vertical
if (Math.Abs(Yinterpolated - yPos) < LINE_GRIP_REGION || neighbors[0].XValue == neighbors[1].XValue)
{
linePoints[0] = series.Points.IndexOf(neighbors[0]);
linePoints[1] = series.Points.IndexOf(neighbors[1]);
}
else
{
return null;
}
return new GrippedLine()
{
startLinePointIndex = linePoints[0],
endLinePointIndex = linePoints[1],
x0Correction = neighbors[0].XValue - xPixelPos,
y0Correction = neighbors[0].YValues[0] - yPixelPos,
x1Correction = neighbors[1].XValue - xPixelPos,
y1Correction = neighbors[1].YValues[0] - yPixelPos
};
}

How to Change MS chart X axis to datetime

I have a chart that displays the latest 1000 candles(Binance API limit). I get the chart data using:
BinanceClient client = new BinanceClient();
var candles = client.Spot.Market.GetKlines(symbol: "BTCUSDT", interval: (Binance.Net.Enums.KlineInterval)inter, startTime: start, endTime: end, limit: 1000).Data.ToList();
This is the data source.
I then build the chart using:
for (int i = 0; i < candles.Count; i++)
{
// adding date and high
chart1.Series["Price15"].Points.AddXY(candles[i].OpenTime, candles[i].High);
// adding low
chart1.Series["Price15"].Points[i].YValues[1] = (double)candles[i].Low;
//adding open
chart1.Series["Price15"].Points[i].YValues[2] = (double)candles[i].Open;
// adding close
chart1.Series["Price15"].Points[i].YValues[3] = (double)candles[i].Close;
}
This all works, but I have created 2 Functions that I believe have the same problem source.
The first function is to allow zooming into the chart area, But this does not work properly(?)
as the X axis does not display the dates correctly so it only really zooms from left to right(i.e does not zoom if the cursor is in the middle of the chart. This is done like this:
private void chart1_MouseWheel(object sender, MouseEventArgs e)
{
try
{
Axis xAxis = chart1.ChartAreas[0].AxisX;
double xMin = xAxis.ScaleView.ViewMinimum;
double xMax = xAxis.ScaleView.ViewMaximum;
double xPixelPos = xAxis.PixelPositionToValue(e.Location.X);
if (e.Delta < 0)
{
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 0.75;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 1;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
}
else if (e.Delta > 0)
{
var posXStart = xAxis.PixelPositionToValue(e.Location.X) - (xMax - xMin) * 0.75;
var posXFinish = xAxis.PixelPositionToValue(e.Location.X) + (xMax - xMin) * 1;
xAxis.ScaleView.Zoom(posXStart, posXFinish);
FZoomLevel++;
}
}
catch { }
}
Second function is a Hittest to try and find my coordinate values. This worked for the Y axis, but still no values for the X axis. This is done like this:
private void chart1_GetToolTipText(object sender, ToolTipEventArgs e)
{
System.Windows.Forms.DataVisualization.Charting.HitTestResult htr = chart1.HitTest(e.X, e.Y);
if (htr.ChartElementType != ChartElementType.PlottingArea && htr.ChartElementType != ChartElementType.DataPoint && htr.ChartElementType != ChartElementType.Gridlines&& htr.ChartElementType!= ChartElementType.Axis)
return;
ChartArea ca = chart1.ChartAreas[0];
double yCoord = ca.AxisY2.PixelPositionToValue(e.Y);
string s = e.X.ToString("s");
double xCord = ca.AxisX.PixelPositionToValue(e.X);
e.Text = "\nY = " + Math.Round(yCoord, 2).ToString();
e.Text = "\nX = " + xCord.ToString();
}
My question is how can I format my X axis Like TradingView(e.g)? right now it automatically can calculate the range of dates. i.e. if I want 15min candle data from this date it will start from 2021-05-18 to 2021-05-28, but it wont display hours and x values.
background: I am trying to create an algotrading environment
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.IntervalType = (DateTimeIntervalType)DateRangeType.DayOfMonth;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm-hh-dd";
You can edit the format of the x axis by first setting the XValueType to either Time or DateTime
Not perfect but allows for more detail when zooming in.

Algorithm for moving pixels in bulk?

Currently i'm trying to produce a simple 2D map generation program, and it is pretty much finished apart from one key thing; The movement of the generated islands. The way the program functions it keeps all the islands in the middle of the map separated by colour like in some like disco ball of puke thing, but my main problem is trying to move the islands into new locations.
The program should randomly place the islands in new places based on colour, but i am having a considerable amount of difficulty doing this, as all solutions i have attempted have either fell on their face in a tsunami of 'index out of bounds of the array' errors or have worked, but taken literal hours to move a single island.
TLDR; Do any algorithms exist that would potentially allow me to move shapes made of pixels to random locations while keeping their existing shapes? mine suck.
Edit: I will try and rewrite this to be easier to read later since i'm in a rush, but in essence it reads all the pixels from the circle using .getpixel and stores them in an array based on their colour, it then generates a random location and runs the same code again, only this time it will accept a colour as an argument and will place the colour at the pixel relative to the centre of the circle if it finds a colour that is the same as the colour it is currently accepting.
In theory this should go through every colour and generate a new position for each one that maintains the shape of the island upon generation, but in practice it just takes forever.
//Thoughts - use the circle generator as a radar to find all the seperate colors, then for each color randomly generate an x and a y. then use the circle generator but only apply the colors that are selected
if (tempi >= 716 || tempib > 0)
{
if(tempib <= 0)
{
tempi = 0;
tempib = 1;
randxb = Rander.Next(10, xlen - 10);
randyb = Rander.Next(10, ylen - 10);
}
tempi += 1;
day += 1;
if(day >= 31)
{
month += 1;
day = 1;
}
if(month >= 13)
{
year += 1;
month = 1;
}
AD = "PF";
era = "Prehistoric era";
age = "Islandic Age";
Point temppb = new Point(randxb, randyb);
if (colours[tempib] == Color.DarkBlue || colours[tempib] == Color.FromArgb(0, 0, 0))
{
tempib += 1;
}
else
{
Radar(0, temppb, "write", colours[tempib]);
}
tempi = 0;
tempib += 1;
randxb = Rander.Next(10, xlen - 10);
randyb = Rander.Next(10, ylen - 10);
if (tempib >= islandnuma)
{
age = "Neanderthalic Age";
}
}
else
{
year += Rander.Next(1, 3);
day = 1;
AD = "PF";
era = "Prehistoric era";
Point tempp = new Point(xlen / 2 - 150, ylen / 2 - 150);
tempi += 1;
Radar(tempi, tempp, "scan", Color.White);
if(tempi >= 716)
{
clearmap();
}
}
}
This is the terrible algorithm it calls
Color[,] scanresults = new Color[717, 4499]; //shell, place in shell
private void Radar(int circle, Point pos, string mode, Color col) //Fuck this doesnt work i need to change it
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
if (mode == "scan")
{
int mj = 0;
if (circle <= 716)
{
for (double i = 0.0; i < 360.0; i += 0.1)
{
mj += 1;
int radius = circle / 2; //max size = 716
double angle = i * System.Math.PI / 180;
int x = pos.X - (int)(radius * System.Math.Cos(angle));
int y = pos.Y - (int)(radius * System.Math.Sin(angle));
Color m = Map.GetPixel(x, y);
scanresults[circle, mj] = Map.GetPixel(x, y);
}
}
else
{
return;
}
}
else
{
if(mode == "write")
{
for(int c2 = 0; c2 <= 716; c2++)
{
int bmj = 0;
for (double i = 0.0; i < 360.0; i += 0.1)
{
try
{
if (mode == "write")
{
bmj += 1;
int radius = (716 - c2) / 2; //max size = 716
double angle = i * System.Math.PI / 180;
int x = pos.X - (int)(radius * System.Math.Cos(angle));
int y = pos.Y - (int)(radius * System.Math.Sin(angle));
if (scanresults[c2, bmj] == col)
{
Map.SetPixel(x, y, col);
}
}
}
catch (Exception em)
{
Console.Write("error: " + em);
}
//Color m = Map.GetPixel(x, y);
//scanresults[circle, mj] = Map.GetPixel(x, y);
}
}
}
}
//Dont hate me im defensive about my terrible coding style
}
}

C# How to use OADate to convert dates from database?

I have a database table of dates and stock prices. I bind it to a WinForm Chart control according to the article. https://msdn.microsoft.com/en-us/library/dd489231(v=vs.110).aspx
I want to mark the highest data point as this article. https://msdn.microsoft.com/en-us/library/dd456612(v=vs.110).aspx
// Get relative coordinates of the data point
System.Drawing.PointF pos = System.Drawing.PointF.Empty;
pos.X = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.X, i);
pos.Y = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max);
// Convert relative coordinates to absolute coordinates.
os = cg.GetAbsolutePoint(pos);
Since the X-Axis is datetime value, I got pos.X = -99845. I think I should use OADate as the article. https://msdn.microsoft.com/en-us/library/dd456614(v=vs.110).aspx But I haven't figured out the way for the binding source.
I find I should use the point's X value instead of its index i like in that tutorial example.
private void chart_PostPaint(object sender, ChartPaintEventArgs e)
{
if (e.ChartElement is Series && ((Series)e.ChartElement).Name == "Series3")
{
Series s = e.Chart.Series[0];
ChartGraphics cg = e.ChartGraphics;
double max = s.Points.FindMaxByValue().YValues[0];
for (int i = 0; i < s.Points.Count; i++)
{
if (s.Points[i].YValues[0] == max)
{
PointF pos = PointF.Empty;
pos.X = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.X, s.Points[i].XValue);
pos.Y = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max);
pos = cg.GetAbsolutePoint(pos);
for (int r = 10; r < 40; r+=10)
{
cg.Graphics.DrawEllipse(
Pens.Red,
pos.X - r / 2,
pos.Y - r / 2,
r, r);
}
}
}
}
}

ZedGraph, numbers gather in a region of number line when graph is scaled

I scale graph but I have problem like in the image (http://postimage.org/image/74ra418yv/).
This is a simulation window, so each pointpairlist is calculated for each unit time. I don't show all of them at the same time. So I create CalculateScale Function for scaling. I need minimum X,Y and maximum X,Y in all lists. I finds minimum and maximum value of x-axis and y-axis with CalculateScale. It works good, only problem is numbers are gathered in a region.
public void EditScale(List<PointPairList> lists)
{
GraphScale scale = CalculateScale(lists);
GraphPane.XAxis.Scale.Min = scale.MinX - 20;
GraphPane.XAxis.Scale.Max = scale.MaxX + 20;
GraphPane.YAxis.Scale.Min = scale.MinY - 20;
GraphPane.YAxis.Scale.Max = scale.MaxY + 20;
}
protected GraphScale CalculateScale(List<PointPairList> list)
{
double maximaY = -10000000;
double minimaY = 10000000;
double maximaX = -10000000;
double minimaX = 10000000;
double listMinY, listMaxY, listMinX, listMaxX;
for (int i = 0; i < list.Count; i++)
{
listMaxY = list[i].Max(point => point.Y);
listMinY = list[i].Min(point => point.Y);
if (listMaxY > maximaY)
maximaY = listMaxY;
if (listMinY < minimaY)
minimaY = listMinY;
listMaxX = list[i].Max(point => point.X);
listMinX = list[i].Min(point => point.X);
if (listMaxX > maximaX)
maximaX = listMaxX;
if (listMinX < minimaX)
minimaX = listMinX;
}
return new GraphScale(minimaX, maximaX, minimaY, maximaY);
}

Categories

Resources