繁体   English   中英

添加数据时,不需要的标签出现在RangeBar图表AxisX上

[英]Unwanted labels appear on RangeBar chart AxisX when adding data

我有一个附有ChartAreaChart (RangeBarChart) ChartArea具有一个附加的Series 我也有一个List<string> ,这个StringList的充满了我想要的值AxislabelsAxisX有。 在我的示例屏幕快照中,此字符串列表包含6个字符串。

所有6个标签的名称都正确,但是在我的AxisX的上限和下限处,标签也都编号了,请参见屏幕截图(上部为-1和6标签是不希望的)。

屏幕截图 截图链接: https//imgur.com/a/pwYF4yl

我在foreach循环中使用两行代码将数据添加到图表中。 我发现当我注释掉这两行时,轴上的多余数字没有出现,但是显然这不是解决方案,因为我也没有显示任何数据。 我也无法在代码中手动删除标签,因为我没有指向它们的pointIndices。 在调试器中查看我的series.Points []集合时,它们的所有X值都在0到5之间(也在屏幕截图中)。

我如何摆脱这些标签?

我在一个快速测试项目中重新创建了不需要的行为,只需在设计器中添加一个名为“图表”的图表,然后将此代码复制到您的主窗体的代码部分中,就可以自己重新创建问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace XAxisLabelsNumbersTest
{
    public partial class Form1 : Form
    {
        ChartArea ca;
        Series serie;
        List<string> xLabels = new List<string> {"Label1", "Label2", "Label3", "Label4", "Label5", "Label6"};
        List<myObj> myObjList = new List<myObj>();

        public class myObj
        {
            public Int32 begin { get; set; }
            public Int32 end { get; set; }
            public int xcord { get; set; }
            public int pointIndex { get; set; }
            public string label { get; set; }

            public myObj(Int32 begin, Int32 end, string label)
            {
                this.begin = begin;
                this.end = end;
                this.label = label;
            }
        }

        public Form1()
        {
            InitializeComponent();
            // Setting some properties regarding chart behaviour
            ca = chart.ChartAreas.Add("ca");
            serie = chart.Series.Add("serie");
            serie.ChartArea = ca.Name;
            serie.ChartType = SeriesChartType.RangeBar;
            serie.XValueType = ChartValueType.String;
            serie.YValueType = ChartValueType.Int32;
            serie["PixelPointWidth"] = "10";
            //ca.AxisY.LabelStyle.Format = "HH:mm tt";
            ca.AxisX.MajorGrid.Interval = 1;

            ca.AxisY.Minimum = 0;
            ca.AxisY.Maximum = 6;

            Title title = new Title("Title");
            chart.Titles.Add(title);
            title.DockedToChartArea = ca.Name;
            title.IsDockedInsideChartArea = false;
            title.Font = new Font("Serif", 18);
            ca.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.None;
            ca.AxisX.LabelStyle.Font = new Font("Trebuchet MS", ca.AxisX.LabelAutoFitMaxFontSize, FontStyle.Bold);
            ca.AxisX.LabelStyle.Interval = 1;

            // Create Labels from xLabels
            for (int i = 0; i < xLabels.Count; i++)
            {
                int pi = serie.Points.AddXY(i, null, null);
                serie.Points[pi].AxisLabel = xLabels[i];
            }

            // Fill myObjList with testdata
            myObjList.Add(new myObj(0, 1, "Label1"));
            myObjList.Add(new myObj(1, 2, "Label2"));
            myObjList.Add(new myObj(2, 3, "Label3"));
            myObjList.Add(new myObj(3, 4, "Label4"));
            myObjList.Add(new myObj(4, 5, "Label5"));
            myObjList.Add(new myObj(5, 6, "Label6"));

            // Fill serie with data from myObjList
            // Comment out this foreach block and the weird label numbering is gone...
            foreach (myObj myObj in myObjList) 
            {
                myObj.xcord = xLabels.FindIndex(Label => Label.Equals(myObj.label));
                myObj.pointIndex = serie.Points.AddXY(myObj.xcord, myObj.begin, myObj.end);
            }
        }
    }
}

您可以通过设置以下axis属性来隐藏两个“ endlabels”: Axis.LabelStyle.IsEndLabelVisible

ca.Axis.LabelStyle.IsEndLabelVisible = false;

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM