mschart:我的x轴刻度是一团糟

时间:2014-04-01 13:27:33

标签: vb.net c#-4.0 charts mschart

我试图驯服mschart的x轴时遇到一些问题。

我使用图表同时绘制多个直方图,你可以看到我的x刻度是一团糟(见下图)。我的主要问题是:如何让它看起来像y轴一样干净。我所说的“干净”是指:

  • 使刻度中出现“零值”。
  • 将对人类可读性有意义的值放在我的意思中,而不是像{0.000000000000}这样的格式。

我能找到的所有解决方案都是某种DIY方法,在我考虑的所有各种用例中都不起作用(或更好:我无法使其工作)。我的直觉说:“mschart能够在Y轴的情况下完美地完成。它必须能够为X轴做到这一点!”。

修改:抱歉,我没有足够的声誉来发布图片。这是imgur link =>

中的图表

http://imgur.com/bpeYDFK.png

在旁注中,并且正如您在图中看到的几乎所有值都集中在x = 0附近,我想知道是否有可能为x创建对数刻度...虽然我很意识到负值是一个问题,0应该是--Infinite。

当然我可以选择更好的比例,但我想在这里完成的是自动创建大量图表而不是一个一个地担心它们。我的解决方案是从x轴的顶部和底部切下一定比例的能量(类似于“从min_x开始,向上,并且一旦你达到10%的能量作为总和,这将是新的min_x。同样的事情从max_x向下。“),但这个解决方案仍然在零附近留下一个狭窄的峰值。我可以使用更高的切割能量值,但我担心它在所有情况下都不起作用......

任何提示?非常感谢你。

Edit2 :这是此图表系列类型的示例。因为它是在x_min和x_max之间计算N个区间的直方图,所以X的值非常混乱。

-67.7591400146485,0
-66.0651615142823,0
-64.3711830139161,0
-62.6772045135498,0
-60.9832260131836,0
-59.2892475128174,0
-57.5952690124512,0
-55.901290512085,0
-54.2073120117188,0
-52.5133335113526,0
-50.8193550109863,0
-49.1253765106201,0
-47.4313980102539,0
-45.7374195098877,0
-44.0434410095215,0
-42.3494625091553,0
-40.6554840087891,0
-38.9615055084228,0
-37.2675270080566,0
-35.5735485076904,0
-33.8795700073242,0
-32.185591506958,0
-30.4916130065918,0
-28.7976345062256,0.000405350628293474
-27.1036560058594,0
-25.4096775054932,0
-23.7156990051269,0
-22.0217205047607,0.000405350628293474
-20.3277420043945,0.000810701256586948
-18.6337635040283,0.000810701256586948
-16.9397850036621,0.000810701256586948
-15.2458065032959,0.0016214025131739
-13.5518280029297,0.00121605188488042
-11.8578495025635,0.00283745439805432
-10.1638710021973,0.00364815565464126
-8.46989250183105,0.00405350628293474
-6.77591400146484,0.0105391163356303
-5.08193550109863,0.0121605188488042
-3.38795700073241,0.0186461289014998
-1.6939785003662,0.0283745439805432
7.54951656745106E-15,0.835022294284556
1.69397850036622,0.0413457640859343
3.38795700073243,0.0206728820429672
5.08193550109864,0.00608025942440211
6.77591400146485,0.00486420753952169
8.46989250183106,0.000810701256586948
10.1638710021973,0.000810701256586948
11.8578495025635,0.000405350628293474
13.5518280029297,0.00243210376976084
15.2458065032959,0.000810701256586948
16.9397850036621,0.000405350628293474
18.6337635040283,0
20.3277420043945,0
22.0217205047607,0
23.715699005127,0
25.4096775054932,0
27.1036560058594,0
28.7976345062256,0
30.4916130065918,0
32.185591506958,0

Edit3 :所以我找到了一个混合this postthis other post的解决方案。

Dim bestGuessInterval As Double = (maxGraphValue - minGraphValue) / numberOfPointsInGraph

newChartArea.AxisX.Interval = RoundToClosest(bestGuessInterval)

'It uses the custom function "RoundToClosest":

Private Function RoundToClosest(ByVal value As Double) As Double
    Dim digits As Integer = NumberOfZerosAfterDecimalPoint(value)        
    Return Math.Round(value, digits)
End Function

Private Function NumberOfZerosAfterDecimalPoint(ByVal value As Double) As Integer
    Dim numberAsString As String = value.ToString()        
    Dim charCounter As Integer = 0
    Dim digitsAfterPoint As Boolean = False
    For Each character As String In numberAsString
        If character = "." Then
            digitsAfterPoint = True
            charCounter += 1
        Else
            If CDbl(character) <> 0 Then
                Return charCounter
            Else
                If digitsAfterPoint Then
                    charCounter += 1
                End If
            End If
        End If
    Next
End Function

1 个答案:

答案 0 :(得分:0)

您想要设置图表区域的Interval。如果您能阅读C#,请查看以下代码。

Area.AxisY.Interval = p_axisYInterval;
Area.AxisX.Interval = p_axisXInterval;

这是一个演示应用

在c#中创建一个新的WinForms项目。添加名为chart1(默认名称)的图表控件和名为button1(也是默认名称)的按钮。双击按钮以连接点击事件。

然后复制粘贴下面的整个代码块并运行它。单击按钮将数据添加到图表并进行步骤/调试以查看事情发生的位置。

我刚从已经拥有的项目中删除了99%的代码,所以它有点混乱,但它得到了重点。

修改

更新了我的样本以执行XY系列而不是固定的X值并硬编码一些东西只是为了看它是如何表现的。

查看我在下面显示此代码块的方法,并将bool flag更改为true或false并运行它以查看行为的更改。基本上,如果MaximumMinimum 对称,或者如果0恰好是{{{0}命中的值之一,那么MSChart将很乐意显示 0 1}}(即,如果Min = -10.0,Max = 5.0且Interval = 1.0,那么它将显示-10,-9,-8 ... 0,1,2,... 5)。

我对这些值进行了硬编码,但您当然希望以编程方式获取它们。然而,这是我留给你的一个练习。享受!

Interval
// Make X-Interval Clean numbers
if (flag)
{
    // Play with these number, the hard part I suppose is calculating them programmatically.
    m_chart.ChartAreas[p_chartArea].AxisX.Minimum = -2;
    m_chart.ChartAreas[p_chartArea].AxisX.Interval = 0.5;
    m_chart.ChartAreas[p_chartArea].AxisX.Maximum = 1;
}