MSChart折线图标签重叠

时间:2015-05-31 21:42:19

标签: scale point mschart

我有这个问题......点标签这样做:

enter image description here

图表必须看起来最漂亮,系列的X轴总是会长大,但我担心如果得到更多分数,它会看起来更加丑陋。

图表中有11个点,我试图将X轴缩放到5但没有。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试在系列中添加智能标签,这样您就可以设置一些规则,使点标签的显示不重叠

chart1.Series["Default"].SmartLabelStyle.Enabled = true;
chart1.Series["Default"].SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Partial;
chart1.Series["Default"].SmartLabelStyle.CalloutLineAnchorCap = LineAnchorCapStyle.Diamond;
chart1.Series["Default"].SmartLabelStyle.CalloutLineColor = Color.Red;
chart1.Series["Default"].SmartLabelStyle.CalloutLineWidth = 2;
chart1.Series["Default"].SmartLabelStyle.CalloutStyle = LabelCalloutStyle.Box;

如果这不能产生您想要的效果,那么您的下一个选项将是使用自定义标签

//IMPORTANT: For this event to work the event handler must be added to the InitializeComponent()
//  method. We recommend that you add the event using the Properties window in the IDE   
private void Chart1_Customize(System.Web.UI.DataVisualization.Charting.Chart sender)
{
    // Get X and Y axis labels collections
    CustomLabelsCollection xAxisLabels = Chart1.ChartAreas["ChartArea1"].AxisX.CustomLabels;
    CustomLabelsCollection yAxisLabels = Chart1.ChartAreas["ChartArea1"].AxisY.CustomLabels;

    // Change text of the first Y axis label
    yAxisLabels[0].Text = "Zero";

    // Change Y axis labels
    for(int labelIndex = 1; labelIndex < yAxisLabels.Count; labelIndex++)
    {
        yAxisLabels[labelIndex].Text = yAxisLabels[labelIndex].Text + "�. 00'";
    }

    // Remove each second X axis label
    for(int labelIndex = 0; labelIndex < xAxisLabels.Count; labelIndex++)
    {
        // Adjust position of the previous label
        if(labelIndex > 0)
        {
            xAxisLabels[labelIndex - 1].ToPosition += 
                (xAxisLabels[labelIndex].ToPosition - xAxisLabels[labelIndex].FromPosition) / 2.0;
            xAxisLabels[labelIndex - 1].FromPosition -= 
                (xAxisLabels[labelIndex].ToPosition - xAxisLabels[labelIndex].FromPosition) / 2.0;
        }

        // Remove label
        xAxisLabels.RemoveAt(labelIndex);
    }
}

有关MS Chart的更多帮助,我强烈建议您下载工作示例项目here,它包含有关如何使用MS Chart的不同部分的示例代码,此处给出的代码来自该示例项目