Home > Blockchain >  Broken linechart Windows Form,
Broken linechart Windows Form,

Time:03-02

I want to add a broken line to my linechart. I am setting the YValue as double.NaN when there are no YValue to show.

It works fine except for the first point where it draws a vertical line as seen in the picture. How can I get rid of that one?

enter image description here

CodePudding user response:

There is no code in the question, but it would automatically work with double.NaN if you used the DataBindXY method on the Points property of the chart series, so I assume you are adding the DataPoints manually.

In this case, the IsEmpty property of the DataPoint must be set to true. So, if you create the DatePoints from the x/y values (for example, from two arrays with the actual values), you can set the IsEmpty property accordingly by checking the y value with:

 double.IsNaN(yValue)

A simple self-contained example might look something like this:

private readonly int[] _xArr = new int[] { 1, 2, 3, 4, 5 ,6, 7 };
private readonly double[] _yArr = new double[] { double.NaN, 0.9, 1.2, double.NaN, 1.3, 1.1, 1.4 };

private IEnumerable<DataPoint> DataPoints()
{
    return _xArr.Select((x, i) => new DataPoint(x, _yArr[i]) { IsEmpty = double.IsNaN(_yArr[i])});
}

For completeness, here a simple chart setup for a quick test:

private void Form1_Load(object sender, EventArgs e)
{
    var series = chart1.Series[0];

    series.LegendText = "Some Chart";
    series.ChartType = SeriesChartType.Line;
    series.IsValueShownAsLabel = true;
    foreach(var dp in DataPoints())
    {
        series.Points.Add(dp);
    }
}

The example would then look something like this with the data from above:

demo chart

As one can see, this displays missing start points and interruptions as desired.

  • Related