using logarithmic scale in Ms chart control

限于喜欢 提交于 2019-12-24 11:34:07

问题


Im trying to create a chart the has a base 10 logarithmic scale for the x axis with a range from 1 to 1000. I seem to be able create the axis during design time but whenever the form is loaded I get an error message saying "Chart Area Axes - A logarithmic scale cannot be used for this axis.

Is this a limitation on the MSChart control? why am I not able to create a log scale on the X Axis?


回答1:


It is because for a logarithm scale, the values must be greater than zero. Charting.CHart treats empty chart as being consisting of zeros (I know it is weird). This error can be very difficulty to debug. Therefore, it mean that the graph cannot be EMPTY if any of the axis is a logarithm scale. What I normally do is set the axis as linear and the change it immediately after plot on the graph (and checking no zeros or negative values on the logarithm scale). Also, remember to change the axis to linear before clearing the axis and plotting. Hope this helps someone.




回答2:


I propose to use SuppressExceptions property which allows to ignore some exceptions, also that one connected with zeros in axis with logarithmic scale. I think this is the best solution for that situation instead manipulating data.

chart.SuppressExceptions = true;



回答3:


It's not just about zeroes but also about having your values in the right datatype. Suppose you're populating the chart with values form a DataTable. If you haven't specified the type, .NET will automatically assume they are strings and thus not be able to plot a logarithmic scale.

incomplete specification of the datatable:

        PP = New DataTable
        PP.Columns.Add("X-value")
        PP.Columns.Add("Y-value")

complete specification:

        PP = New DataTable
        PP.Columns.Add("X-value", Type.GetType("System.Double"))
        PP.Columns.Add("Y-value", Type.GetType("System.Double"))

In the first example the exception will be thrown. In the second it won't.




回答4:


Not all chart types support logarithmic scales; try changing the chart type e.g. to Line Chart.

Chart types: http://msdn.microsoft.com/en-us/library/dd489233.aspx

ChartType property: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.series.charttype.aspx



来源:https://stackoverflow.com/questions/15218249/using-logarithmic-scale-in-ms-chart-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!