MSChart Custom Y Axis Value Range - c#

I have some data I need to display using MSChart, I am looking to plot data which is one of the following values taken at a particular datetime:
Low
Low-Medium
Medium
Medium-High
High
So I am looking for datetime along the X axis and the above values on Y
When I try and plot them something like this....
mySeries.Points.AddXY(dateA, "Low");
mySeries.Points.AddXY(dateB, "Low-Medium");
mySeries.Points.AddXY(dateC, "Medium");
The chart obviously doesn't have any idea that Medium show be a larger bar than Low.
How can I specify this range of values for the Y Axis?

You can set numeric values as suggested in Kyle answer and then change the Y labels e.g.:
chart1.ChartAreas[0].AxisY.CustomLabels.Add(0, 1, "LOW");
// it means: on Y range = [0, 1] show the label "LOW" ...
chart1.ChartAreas[0].AxisY.CustomLabels.Add(2, 3, "MEDIUM");
chart1.ChartAreas[0].AxisY.CustomLabels.Add(3, 4, "HIGH");

Give the different values a numeric value:
mySeries.Points.AddXY(dateA, 1); // Low
mySeries.Points.AddXY(dateB, 2); // Low-Medium
mySeries.Points.AddXY(dateC, 3); // Medium
I'm not sure how you would show the named values on the Y axis though.

Related

LiveCharts: symmetrical Y-graph around the zero axis

With LiveCharts I create an X/Y graph:
X-axis: Time
Y-axis: symmetrical values output from the zero axis to MinValue and MaxValue.
The min/max values are set as follows:
if (Math.Abs(newValueY) > maxValueY)
{
maxValueY = Math.Abs(newValueY);
MaxValue = maxValueY;
MinValue = -maxValueY;
}
My problem is that the grid is not displayed symmetrically. The zero axis is not displayed. Is there any way to configure LiveCharts to draw the grid up and down from zero axis instead of starting at MinValue?
Thanks for any hints!
Markus

How to make points more compact using fastpoint chart in

I am new to C# winform. As title, my problem is how to make the points more compact in fastpoint chart. To make it clear, how to proportionally reduce the distance of the blue points like the red line segments shown in the image; that is, make the X axis more compact:
I have searched and found a lot of information about control the interval of X or Y axis labels but which is not the situation here.
You can control the range of data values for each Axis by setting their Minimum and Maximum values.
The syntax is:
someChart.ChartAreas[CAIndexOrName].AxisX.Minimum = someDoubleValue;
Let's prepare a chart to show one day:
Axis ax = chart.ChartAreas[0].AxisX; // a short reference
ax.IntervalType = DateTimeIntervalType.Hours;
ax.Interval = 1;
To set the properties to some DateTime values you need to convert them to doubles; for this conversion there are two built-in functions: DateTime.ToOADate and, to reverse DateTime.FromOADate
This makes the x-axis display 24 hours:
DateTime dt = DateTime.Today;
ax.Minimum = dt.ToOADate();
ax.Maximum = (dt.AddHours(24)).ToOADate();
ax.LabelStyle.Format = "H:mm"; // optional
You also may want to control the data type of the series values:
Series s = chart.Series[0];
s.XValueType = ChartValueType.DateTime; // or some other type, maybe Time
Note you you can also control both the Interval of the Labels and TickMarks and Gridlines on each axis but also set an Offset to start them a little earlier or later..

How to get chart data points value with equal or less than to highlight those data

I'm looking to change my chart series data points if there is an error values. I want to set rule to highlight those data points like below. Please help to get below code working.
// Find first point with a Y2 value of equal or less than 10.
var dataPoint = Chart1.Series[1].Points.Where(x => x.YValues <= 10);
foreach (DataPoint dt in dataPoint)
{
dt.BorderDashStyle = ChartDashStyle.Dot;
dt.Color = Color.Red;
}
DataPoint.YValues is an array.
The YValues property is used to set the Y-values of data points.
Only one Y-value per point is required for all chart types except
bubble, candlestick and stock charts. These chart types require more
than one Y-value because one data point consists of multiple values.
For example, to plot one stock chart column, four values are required:
high, low, open and close values.
The YValues property returns an array of double values when used to
retrieve the Y-values.
Important The YValuesPerPoint property determines the maximum number
of Y-values that all data points in a Series can have. If you specify
more than the allowable number of Y-values, an exception will be
raised.
Unless you are using one of the above special ChartTypes you always will want to use the first element. So simply write:
var dataPoint = Chart1.Series[1].Points.Where(x => x.YValues[0] <= 10);
If you do use one of the three multiple Y-Values chart types you could, depending on the situation for example write this:
var dataPoint = Chart1.Series[1].Points.Where(x => x.YValues.Max() <= 10);
or this:
var dataPoint = Chart1.Series[1].Points.Where(x => x.YValues.Min() <= 10);

Plotting two y-values for x=0 in a MS Chart control

I found a weird behavior in MS Chart for Windows Forms.
Let's say I want to have a scatter plot with two points (1,10) and (1,20). I can do that in this way:
....
Series series = new Series();
series.ChartType = SeriesChartType.Point;
double[] x = { 1, 1 };
double[] y = { 10, 20 };
series.Points.DataBindXY(x, y);
That works fine. But now I want the same result, but both x-values should be 0.
double[] x = { 0, 0 };
double[] y = { 10, 20 };
series.Points.DataBindXY(x, y);
In that case the chart control creates two data points at 'autogenerated' x positions 1 and 2. It just ignores the given x-values. It is the same behavior if I use
series.Points.AddXY(0, 10);
series.Points.AddXY(0, 20);
I get the same effect for more than two data points. So it turns out that scatter plot does not work if not at least one x-value is nonzero.
I think a possible workaround would be to use multiple series, but that is inacceptable.
Does anyone have a explanation for this behavior or a solution for this problem?
I found a solution by myself:
You have to add
series.CustomProperties = "IsXAxisQuantitative=True";
to your code. So the x-values really are treated as values. I don't know why this is not self-evident if I use the BindXY function.
(I would just comment on Fratyx's answer, but I guess don't have the reputation to do so.)
To elaborate on Fratyx's answer, the IsXAxisQuantitative property only applies to certain SeriesChartType's, and will be ignored for other types.
These are those types:
https://referencesource.microsoft.com/#System.Windows.Forms.DataVisualization/Common/Utilities/CustomAttributesRegistry.cs,e51a969ce4c7db16

How can I get the X value if I know the Y value of a point on a chart?

if I know the x & y values of 2 points on a chart, and I know the Y value of a position in between those 2 points, how do I get that Y value's corresponding X value?
Basically I would like to get the X position at which that value first occurs, in between the 2 original points.
The Y axis is in doubles, and the X axis uses DateTimes.
It's probable that a data point at exactly the Y value may not exist as an exact point on the chart (it's a line chart) however, but I would need to find the exact X value, not the nearest actual point to it i'm afraid.
For a LineChart the calculation is really just simply interpolation math.
But it still does take some knowledge about how Chart works.
You have normal numbers for the Y-Values but have DateTimes for the X-Values.
This wouldn't work well with math so we'd expect the need to transform the dates to numbers.
But Chart does just that internally, using calls to FromOADate and ToOADate().
The nice thing is that this means that what you add as a DateTime value internally is stored as a number, in fact as a double.
So you can indeed do the math straight forward. Here is a code example that shows how to find a point on the same line as two given points with a given y-value.
To make it look nice I add the calculated point as a new DataPoint to a second series of type Point..
First I prepare my chart:
chart1.ChartAreas.Clear();
chart1.Series.Clear();
ChartArea CA = chart1.ChartAreas.Add("CA");
Series S1 = chart1.Series.Add("S1");
Series S2 = chart1.Series.Add("S2");
S1.ChartType = SeriesChartType.Line;
S2.ChartType = SeriesChartType.Point;
S1.Points.AddXY(new DateTime(2015, 12, 10), 10);
S1.Points.AddXY(new DateTime(2015, 12, 31), 31);
DataPoint dp1 = S1.Points[0];
DataPoint dp2 = S1.Points[1];
Now I set the Y-Value for which I search the X-Value:
double y3 = 24; // X-Mas ;-)
Now I calculate the deltas and the slope. This can, of course be done all in one but I spell it out for clarity..:
double deltaY = dp2.YValues[0] - dp1.YValues[0];
double deltaX = dp2.XValue - dp1.XValue;
double slope = deltaY / deltaX;
Finally I calculate the X-Value you are looking for:
double x3 = dp1.XValue + (y3 - dp1.YValues[0]) * slope;
Now I can show that the new point indeed sits on the line between the first two points:
S2.Points.AddXY(x3, y3);
S2.Points[0].Color = Color.Red;

Categories