【问题标题】:Display integers only in AxisY label?仅在 AxisY 标签中显示整数?
【发布时间】:2013-10-08 23:27:43
【问题描述】:

我有一个描述人数的条形图。当只有少数人时,Y 轴显示值:0.5、1、1.5 等... 看起来有点傻

  • 我可以将间隔改写为 1 (AxisY.LabelStyle.Interval = 1), 但是如果有 100 人就不行了
  • 我可以设置 AxisY.Maximum = 10,但这不适用于 100 人
  • 我可以设置 AxisY.LabelStyle.Format = {#},但它显示 [1,1,2,2] 对每个标签进行四舍五入

我意识到我可以根据内容动态地使用前两个选项中的任何一个,但想知道是否有一种自动方法可以使标签“仅整数”?

【问题讨论】:

  • 我也遇到了这个问题,好问题。
  • 需要的是一个 MinimumInterval 属性,但似乎没有这样的东西。

标签: c# winforms mschart


【解决方案1】:

您可以使用刻度分隔符在同一轴上显示小数和大数:

// Enable scale breaks
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.Enabled = true;
// Set the scale break type
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.BreakLineStyle = BreakLineStyle.Wave;
// Set the spacing gap between the lines of the scale break (as a percentage of y-axis)
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.Spacing = 2;
// Set the line width of the scale break
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.LineWidth = 2;
// Set the color of the scale break
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.LineColor = Color.Red;
// Show scale break if more than 25% of the chart is empty space
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.CollapsibleSpaceThreshold = 25;
// If all data points are significantly far from zero, 
// the Chart will calculate the scale minimum value
chart1.ChartAreas["Default"].AxisY.ScaleBreakStyle.IsStartedFromZero = AutoBool.Auto;

此代码示例直接来自the mschart samples,如果您使用图表控件,则必须下载。

【讨论】:

  • 这不是我们所需要的。问题是没有 MinimumInterval 属性。理想情况下,需要能够将最小间隔设置为 1,以便自动生成标签/刻度线/网格永远不会生成小数值。
【解决方案2】:

跟随自定义事件就可以了。我基本上使用正则表达式来检测不是整数的标签,然后将它们删除。但是将间隔设置为 1 可能会给您带来麻烦,除非您稍后将其恢复为自动。

由于必须更改轴间隔属性,此代码无法解决我的问题。如果有人有其他建议,请提出建议。

    private void Chart_Customize(object sender, EventArgs e)
    {
        List<CustomLabel> list = new List<CustomLabel>();
        System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^\\d+$");

        foreach (CustomLabel l in chart.ChartAreas[0].AxisY.CustomLabels)
        {
            if(!r.IsMatch(l.Text))
            {
                list.Add(l);
            } 
        }

        if (list.Count > 0)
        {
            foreach (CustomLabel l in list)
                chart.ChartAreas[0].AxisY.CustomLabels.Remove(l);
            chart.ChartAreas[0].AxisY.Interval = 1;

        }
    }

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多