C# Charting zoom problem with DataTime for X axis - c#

using the MS Charting for .NET, I am trying to zoom into the chart which I have created.
This works fine on the Y axis (type = float) and on the X axis if type = int, but when I have DateTime values on the X axis, scrolling does not behave as it should on this axis.
Vertically, everything still behaves properly, but while I can zoom into the X axis, I cannot drag the sliding bar to move where I am zoomed into. However, I can click either side and it will jump.
Does anyone know how to fix this and make it behave like it does with float values?
Thanks!

Depending on your data, try setting the chart area's CursorX.IntervalType property to something other than Auto.
You may run into a similar issue when trying to use the small scroll arrows of the scroll bar once you are zoomed in. To fix that you can try to set the chart area's AxisX.ScaleView.SmallScrollSizeType property to the same thing as the CursorX.IntervalType.
For example, if you have a chart with data that is reported every 30 seconds you can use the following settings:
chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].CursorX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
chart1.ChartAreas[0].CursorX.Interval = 0.5D;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Minutes;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 0.5D;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss";

I had the same problem and these settings solve it for me:
_chart.ChartAreas[0].CursorX.IsUserEnabled = true;
_chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
_chart.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].CursorX.Interval = 1D;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 1D;
_chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
_chart.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].AxisX.ScaleView.MinSize = 1D;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Minutes;
_chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1D;
Especially the last two lines did the job.

add
chart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;

My solution was:
chart1.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Milliseconds;

Related

Chart series are not shown until clicking on scrollbar

Here is my init method for the chart in my C# program.
private void initGraph()
{
chartTrend.Cursor = Cursors.Hand;
chartTrend.ChartAreas[0].CursorX.LineColor = Color.Red;
chartTrend.ChartAreas[0].CursorX.LineWidth = 2;
chartTrend.ChartAreas[0].CursorX.LineDashStyle = ChartDashStyle.Dot;
chartTrend.ChartAreas[0].CursorX.IsUserEnabled = true;
// let us select a portion of chart so then zoom that portion
chartTrend.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chartTrend.ChartAreas[0].CursorX.Interval =1 ;
chartTrend.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Seconds;
chartTrend.ChartAreas[0].CursorX.AutoScroll = false;
chartTrend.ChartAreas[0].AxisY.IsStartedFromZero = true;
chartTrend.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chartTrend.ChartAreas[0].AxisY.ScaleView.Zoomable = false;
// disable zoom-reset button (only scrollbar's arrows are available)
chartTrend.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chartTrend.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chartTrend.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
chartTrend.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds;
chartTrend.ChartAreas[0].AxisX.ScaleView.Size = 528;
}
The problem is that, when I add data to the chart, It will not be shown until I click on the scroll. I even tried to move the scroll by software but it didn't work. what can I do?
By the way, it is all for the last line of initGraph() method. when I comment it out, data will be shown, but in the way that I'm not interested.
The problem was the position of scroll. I understood that when I added data to the chart, It was depicting chart from a time which was far away from the start of my time range and after clicking on the scroll, it took it to the proper time. So I decided to move the scroll after adding my first points and finally, the problem solved by this line:
chartTrend.ChartAreas[0].AxisX.ScaleView.Position = chartTrend.ChartAreas[0].AxisX.Minimum;

How to change data label position in EPPlus?

So here is my successfully generated chart. However for some reason on this chart only it decides the date times should show up at the top of the chart instead of the bottom. Here is my code and what I've tried. Notice the text is neither bold nor is at showing where it should. I've also tried giving the chart a ton of area to work with and it still does this. Any help in the right direction would be greatly appreciated.
ExcelChart paintDewChart = overview.Drawings.AddChart(w.Name + "Dew", OfficeOpenXml.Drawing.Chart.eChartType.LineMarkersStacked);
paintDewChart.Title.Text = "Paint Dew";
paintDewChart.SetPosition(24, 0, 0, 0);
paintDewChart.SetSize(1550, 400);
paintDewChart.Legend.Position = eLegendPosition.Bottom;
var dser1 = (ExcelLineChartSerie)(paintDewChart.Series.Add(w2.Cells["C4:C" + water.Count.ToString()], w1.Cells["A4:A" + water.Count.ToString()]));
var dser2 = (ExcelLineChartSerie)(paintDewChart.Series.Add(w2.Cells["D4:D" + water.Count.ToString()], w1.Cells["A4:A" + water.Count.ToString()]));
dser1.Header = "PC2 Dew";
dser2.Header = "B Dew";
dser1.DataLabel.Position = eLabelPosition.Bottom;
dser2.DataLabel.Position = eLabelPosition.Bottom;
dser1.DataLabel.Font.Bold = true;
dser2.DataLabel.Font.Bold = true;
EDIT:
I figured out I can flip the orientation with this, however labels still overlay on the graph instead of below it.
paintDewChart.YAxis.Orientation = eAxisOrientation.MaxMin;
Here is the correct code I need for my situation.
paintDewChart.YAxis.Orientation = eAxisOrientation.MaxMin;
paintDewChart.XAxis.LabelPosition = eTickLabelPosition.High;
paintDewChart.XAxis.TickLabelPosition = eTickLabelPosition.High;
EDIT: Also depending on minimum and maximum value ranges going from positive to negative, you may need this.
paintDewChart.XAxis.Crosses = eCrosses.Max;

Spline chart smooth corners

I am using Chart control from .NET framework in my project. I have added chart control to the form and configured as shown below.
// Add a new series.
chart1.Series.Add("1");
var series = chart1.Series[0];
series.ChartType = SeriesChartType.Spline;
// Hide the legend.
series.IsVisibleInLegend = false;
// configure x axis.
var cArea = chart1.ChartAreas[0];
cArea.AxisX.IntervalType = DateTimeIntervalType.Number;
cArea.AxisX.LabelStyle.Format = "00";
cArea.AxisY.LabelStyle.Format = "0.000";
cArea.AxisY.LabelStyle.IsEndLabelVisible = true;
cArea.AxisX.Minimum = 0;
cArea.AxisX.Maximum = 100;
cArea.AxisX.Interval = 20;
cArea.AxisY.Minimum = 0;
cArea.AxisY.Maximum = 100;
cArea.AxisX.Interval = 20;
Data point values are as below:
chart1.Series[0].Points.AddXY(0, 5);
chart1.Series[0].Points.AddXY(5, 10);
chart1.Series[0].Points.AddXY(10, 30);
chart1.Series[0].Points.AddXY(20, 100);
chart1.Series[0].Points.AddXY(30, 100);
chart1.Series[0].Points.AddXY(40, 90);
chart1.Series[0].Points.AddXY(50, 80);
For the above data points, series is not smooth. Upper edge is getting cut. Refer attached image.
How to make it smooth so that whole line is visible ?
It's not visible because of the smoothing, adapt the scale (using cArea.AxisX.Maximum = 150; for example) or remove the smoothing to make the whole curve visible.
As with the DrawCurves GDI+ method you can control the tension of the splines, i.e. how close they are to the points and their connecting lines and how much smoothing they create. Too much 'smoothing' creates the fantasy tops you see and also crazy whirls from even small bumps in the data..
Setting the tension it is done via the LineTension Custom attribute.
Lower it from the default of 0.8 to something smaller. Test to see what you prefer.
Here is an example for a Series S :
S.SetCustomProperty("LineTension", "0.4");
Note that you still should make the y-axis Maximum a little larger or else you may need to bring the tension down to 0, which will look like a line type..
Here are a few variations:

How to draw a stripline diagonally using MSChart?

Tweaking MS Charts. I have successfully draw a dynamic chart but need to draw a line (Yellow) across the chart. How will i draw (yellow) line. I have X and Y values.
Here is an example you can play with:
// we create a general LineAnnotation, ie not Vertical or Horizontal:
LineAnnotation lan = new LineAnnotation();
// we use Axis scaling, not chart scaling
lan.IsSizeAlwaysRelative = false;
lan.LineColor = Color.Yellow;
lan.LineWidth = 5;
// the coordinates of the starting point in axis measurement
lan.X = 3.5d;
lan.Y = 0d;
// the size:
lan.Width = -3.5d;
lan.Height = 5.5d;
// looks like we need an anchor point, no matter which..
lan.AnchorDataPoint = yourSeries.Points[0];
// now we can add the LineAnnotation;
chart1.Annotations.Add(lan);

How to manually position ASP.Net Chart Legend?

Using the Chart controls built into ASP.Net, I'm trying to manually position the Title and the Legend so that they are directly next to each other horizontally just above the ChartArea. I've been able to manually position the Title using the following code:
chart.Titles["Title1"].Position.Auto = false;
chart.Titles["Title1"].Position.X = 10;
chart.Titles["Title1"].Position.Y = 5;
There's nothing to it, really. However, I'm attempting to position the Legend to the right of it with the following code, and the Legend doesn't even appear:
chart.Legends["Legend1"].Position.Auto = false;
chart.Legends["Legend1"].Position.X = 30;
chart.Legends["Legend1"].Position.Y = 5;
Any ideas what I'm doing wrong? This seems like it should be relatively simple. I've even tried various other coordinates, and I can't get the Legend to appear anywhere. It does appear if I use the built-in positioning such as below, but this positioning does not suit my purposes:
chart.Legends["Legend1"].Docking = Docking.Top;
chart.Legends["Legend1"].DockedToChartArea = "ChartArea1";
chart.Legends["Legend1"].IsDockedInsideChartArea = false;
chart.Legends["Legend1"].Alignment = StringAlignment.Far;
Try newing up an ElementPosition object, like this:
chart.Legends["Legend1"].Position.Auto = false;
chart.Legends["Legend1"].Position = new ElementPosition(30, 5, 100, 20);
Note: The constructor for ElementPosition takes 0 or 4 parameters (x, y, width, height).
I stumbled on this question for looking how to move legend at the bottom of a chart.
Answer for that is to use Docking property
Chart1.Legends["Legend1"].Docking = Docking.Bottom;
It may be helpful for someone in future, as this is the first link in google search.

Categories

Resources