簡體   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