I have a Chart (RangeBarChart)
with a ChartArea
attached to it. The ChartArea has one Series
attached to it. I also have a List<string>
, this stringlist is filled with the values I want the Axislabels
on my AxisX
to have. In my example screenshot, this stringlist contains 6 strings.
All the 6 labels are named correct, but at the upper and lower limits of my AxisX, the labels are numbered as well, see screenshot (upper part, '-1' and the '6' labels are undesired).
Screenshot link: https://imgur.com/a/pwYF4yl
I add data to my chart with two lines of code in a foreach loop. I found that when I comment out these two lines, the extra numbers on my axis don't appear, but obviously this is a non-solution, as I'm also not showing any data. I also can't delete the labels manually in code because I have no pointIndices pointing to them. When looking at my series.Points[] collection in the debugger, all their X-values are between 0 and 5 (also in screenshot).
How can I get rid of these labels?
I recreated the unwanted behaviour in a quick test project, just add a Chart called 'chart' in the designer and copy this code in the code part of your main form and you can recreate the problem for yourself.
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);
}
}
}
}
You can hide both 'endlabels' by setting this axis property: Axis.LabelStyle.IsEndLabelVisible :
ca.Axis.LabelStyle.IsEndLabelVisible = false;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.