I am using MSCharts, and have set the x-axis IsLogarithmic to true. The first problem I had was that the chart crashed when it was first displayed.
I found answers at:
and
http://social.msdn.microsoft.com/Forums/en/MSWinWebChart/thread/056aa11f-658c-48fa-9768-cde912cd2975
So I don’t set isLogarithmic at design time, I do it at runtime when I have a series with:
- X-values specified with no x values <= 0
- Series.IsXValueIndexed = false
- AxisX.IsStartedFromZero = false
However, I have a problem when trying to drag some points on the graph. The MS example code for dragging points uses the PixelPositionToValue function in the MouseMove event handler. However, when I try this with the log axis I have to convert the value myself back to linear with code like:
int GetXValue(MouseEventArgs e, Axis axis)
{
return axis.IsLogarithmic ?
Math.Pow(axis.LogarithmBase, axis.PixelPositionToValue(e.X)) :
axis.PixelPositionToValue(e.X)
}
I don’t think I should really have to do this, and worse, sometimes PixelPositionToValue does seem to convert the value to linear for me, so the function above gives inconsistent results.
I noticed that the documentation for PixelPositionToValue says “Converts an absolute pixel position along an axis to an axis value. This method only works in paint events.”, even though the MS example code for dragging points uses it in the Chart1_MouseMove event handler.
So, for probably very illogical reasons I tried calling Chart1.Refresh() after plotting my points (rather than Chart1.Invalidate()) and the inconsistent behaviour seems to have gone away.
My question is, has anyone tried a similar thing and found a proper way to get PixelPositionToValue to work consistently for a logarithmic x-axis, or found another workaround that doesn’t require manually converting the point to linear and calling Chart1.Refresh()?
I found the following articles:
http://support2.dundas.com/newsletter/04-07/newsletter.htm
http://support2.dundas.com/forum/tm.aspx?m=2786
http://support2.dundas.com/forum/tm.aspx?m=4616&mpage=1&key=ሴ
http://support2.dundas.com/forum/printable.aspx?m=3636
These indicate that for PixelPositionToValue to work outside of a paint event you need to call ReCalc() on the Chart area. ReCalc() doesn’t exist on my version of MSChart, but there is a function RecalculateAxesScale() (see http://social.msdn.microsoft.com/Forums/en-US/MSWinWebChart/thread/da45b7df-8d2f-4598-b4ad-47964eb1505c/), and calling this seems to fix the inconsistent behaviour.
Unfortunately I still need to convert the value to linear if the axis is logarithmic, but at least the behaviour is predictable now.