I'm implementing a scatter plot using the MS Chart Control .NET 3.5, WinForms, C#. My x-axis data is DateTime and noticed I couldn't zoom in smaller than a resolution of 1 day, despite setting the ScaleView as follows:
chart1.ChartAreas["MyChart"].AxisX.ScaleView.MinSize = 4;
chart1.ChartAreas["MyChart"].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Hours;
Has anyone else had this issue? Any ideas?
Figured this out... perhaps I didn't RTFM close enough, but it wasn't obvious from the interactive demo.
Set
chart1.ChartAreas["MyChart"].CursorX.Interval = 0;
and then it allowed me to zoom along the x-axis just fine.
Works Great !
Very handy and mandatory if you want to have smooth Zooming behavior.
Didn't stumble upon it, though I did RTFM :-)
However, if you handle doubles or floats instead of integer based types (such as hours or days), setting the interval to Zero may be a little bit extreme : While zooming, you will end up having overly precise labels such as 2,907343253253235
A good combination is to use these two properties :
chartArea1.AxisY.ScaleView.MinSize = 0;
chartArea1.CursorY.Interval = 0.001;
this way you can zoom as much as you want, while still controlling precision at a reasonable level
Related
I've got a problem with Windows Charts:
I'm using Fastpoint and when plotting a LOT of points (100000+) at X=0,Y=0, they seem to shift towards positive X values. (to the right)
for (int k = 0; k < amountofpoints; k+=1)
{
newGraph.Series[item.name].Points.AddXY(0,0)
}
Look like a minor bug in MSChart.
It happens when all visible points are located at the same position.
Unfortunately even a RecalculateAxesScale();on the ChartArea doesn't help.
It happens even for small numbers of points and gets worse when adding more..:
The only workaround I see is to have at least one point at a different position so that the layout calculations span a non-zero distance.
You can hide the point by making it transparent or you can place it in a different series in the same chartarea, it just needs to be considered for calculating the display.
I called it a 'minor' bug because the whole setup makes obviously no real sense.
So instead of working around you should consider avoiding the situation by not adding all those identical points in the first place..
Has anyone any idea how to use Excel.XlLegendPosition.xlLegendPositionCustom to specifically position the legend of a chart? Currently, I'm using
chart.Legend.Position = Excel.XlLegendPosition.xlLegendPositionBottom;
which does exactly what one would expect. Right up until the time when you try and print the chart. THEN it places the legend pretty much where ever it wants -- which isn't where I need it to be.
I've checked the MSDN and every other site I could think of. The best Microsoft can come up with is as follows:
xlLegendPositionCustom: A custom position.
There are no examples that I could find that show how to use this.
Thanks for any help.
Okay, it doesn't seem as if anyone has encountered this particular issue -- or needed to use the command. No worries, I appreciate folks taking the time to look at the question.
I did find a workaround that allows you to position the legend using points. It appears if you use the preset positioning options (like Excel.XlLegendPosition.xlLegendPositionBottom), Excel positions the legend based on whatever magic MS uses to figure these things out. Since I couldn't figure out how to use the Excel.XlLegendPosition.xlLegendPositionCustom, I played around with the other commands and found these:
chart.Legend.Left = 375;
chart.Legend.Top = 450;
Those commands will take an int (I used 374 and 450) and using them, you can force the position of the legend to anywhere on the chart.
I don't think you can set it to xlLegendPositionCustom, it's read-only, i.e. when you 'ask'(Debug.Print ActiveChart.Legend.Position), after setting ActiveChart.Legend.Left/ActiveChart.Legend.Top you get -4161 which is xlLegendPositionCustom.
I set x-axis as logarithmic scale, but it only displays the major labels, exp : 1E000, 1E001, 1E002, etc... Now i also want to display the minor values, for example : 2E000, 3E000, 4E000,..., 2E001, 3E001, .....
it should look like the graph below :
It seems a simple question but i can't find the way to solve this problem. Can anyone help me ?
Thanks in advance.
This question is similar to this one.
It took me a long time to solve this one, but:
If you if you set the MinorGrid Interval to 1, then you get the traditional Logarithmic grid marks, ten per decade:
aChart.ChartAreas[0].AxisX.IsLogarithmic = true;
aChart.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
aChart.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
As far as I know, the only way to do this is by setting the interval property of the axis.LabelStyle e.g.:
this.chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 0.1;
For example, setting 0.1 in a logaritmic scale, the labels show will be 10^0.1, 10^0.2, 10^0.3 ...
If you need something more particular, I suggest you to create the labels manually using the property axis.CustomLabels, even if is not so intuitive...
I have chart that can contain a lot of points (10000 +)
When I scale the chart in order to see all points in screen, it takes some time to draw them
Can You advice me some optimization, in order not to draw all points
I'm not an expert with listed technologies, but I would solve this by 'bucketing' your data points.
Your X axis is time, so determine the resolution point for the current chart size. IE, if you are seeing the entire chart you will only need a data point per day for example. If you are zoomed in a long way, you might want a point per hour.
Now you have determined resolution, go through your chart, and find all the data that exists between the resolution points, IE, all data that is > 20th April 2011 at 4pm and < 20th April 2011 at 5pm if you are on an hourly resolution.
Depending on the type of data you are using, will determine if you want to average all the data point you have collected, or find the median (or some other method, such as a candle stick chart to show the max/min values). Either way, pick the most relevant method, repeat for all points and render the result with your new data.
Hope that's what you meant.
Seems like you should use some sort of level of detail (LoD) algorithm.
For example:
Always use a maximum given set of points to represent all your actual points. By calculating local minima and maxima you can create a proper representation of the given point set for a certain 'detail', depending on how far you are zoomed in.
Calculating these extrema can still prove to be slow, so you might need to cache them. You can calculate and cache this on the fly as new data arrives.
In addition to the other good suggestions, I would
Do some random-pausing on it, to see if it's spending much time doing something else that could be avoided, such as maybe allocating new point structures all the time.
Rather than paint directly to the window, paint to a bitmap, and copy that to the window. It always looks faster, and sometimes it even is faster. (Be sure to stub out the method that clears the window background.)
I had experienced a severe performance problem with thousands of Series added to the chart rather than thousands of Points. The solution that worked for me was a flavor of the Flyweight pattern:
Instead of adding 1000-s of series, add just a single one.
At the end of a virtual series, i.e., when all points of the series have been added and it's time to move on to the next one, insert an empty point:
series.Points.Add(new DataPoint(0, 0) { IsEmpty = true });
Hope that helps somebody.
I'm currently using the charting within .NET using System.Windows.Forms.DataVisualization.Charting.Chart. Thus far it seems very powerful, and works great. However, there is a huge problem in terms of how it is auto-calculating intervals. I use a lot of double values, and in libraries like ZedGraph, it handles this perfectly. It selects min/max/interval just fine. However, in MS Chart, it may select 206.3334539832 as a minimum, and intervals of a similar decimal precision. Obviously this looks quite ugly.
So, I tried simply making the axis format {0.00} and it works great when it loads the chart. Except when you zoom in, you need greater precision, maybe at 4 decimal places instead of 2. It seems I'm either stuck with 9 decimal places all the time, or else a constant fixed number that may break when someone requires greater precision. I'd rather it pick up the precision based on the level of zoom currently applied. Libraries like ZedGraph and Dundas (which I believe MS is even using!) tend to pick good values that change as you zoom in and out.
Is there any way to have the intervals change precision as the zoom frame changes? It's probably some simple property I have set wrong, but it's hard to tell with the millions of properties this thing has (especially when there's about 14 places that represent the concept of Interval).
I had the exact same problem when zooming. I added code to format the axis labels and call it from the Paint handler. The Axis View objects have an IsZoomed property and have functions to get the current axis limits (GetViewMinimum/Maximum). I set the Axis LabelStyle.Format to "N" for all cases unless the Max-Min=range is less than 1. Then I set the format to "F#" where # is calculated based on the axis range.
# = Convert.ToInt32(Math.Abs(Math.Log10(range) - .5)) + 1;
Having played around with the chart control I haven't been able to find a simple solution to your problem. However the following may help:
Have you considered setting the maximum and minimum values for the axes yourself? If you round the actual maximum and minimum values to the nearest sensible "round" number (5, 10, 0.5, 0.01) this should make the calculated intervals a bit more friendly.
I understand this is not an ideal solution but by carefully choosing the maximum and/or minimum values you can ensure the intervals are "nicer" numbers. If the range of your axes is say divisible by 2, 5 & 10 it should result in fairly nice intervals.
Why not modify number format string.
Create format string
string formatString = "{0.00";
Identify zoom level, say zoomLevel = 2;
formatString = formatString.PadRight(5+zoomLevel, '0');
formatString += "}";
Now use this format on axis legend. Use string builder or some better way to modify the format string.
To provide the result with minimal cost you can use exponential scientific format
You can attach to customize event.
From there, you can modify the labels on x-axis:
var xAxisLabels = chart1.ChartAreas[0].AxisX.CustomLabels;
...
xAxisLabels[0].Text = ...
set min. and max. values:
chart1.ChartAreas[0].AxisX.Maximum = ...;
etc.
you can dynamically update the max and min based on your data set. each time user zooms in, you do a FOREACH on every point and get the stats and based on that set your max and min
It helps to set the IntervalOffset of the axis, here an example:
Private Sub chMap_AxisViewChanged(sender As System.Object, e As System.Windows.Forms.DataVisualization.Charting.ViewEventArgs) Handles chMap.AxisViewChanged
'the grid ticks are rounded values
e.Axis.IntervalOffset = -e.Axis.ScaleView.ViewMinimum
End Sub