Chart in winform displaying wrong Point

2019-07-28 05:11发布

问题:

I have the following code. I have hardcoded the x and y values to test. And for some reason for the point (0,-0.5) it plots (1,-0.5) For the life of me I do not know what is going on, because if you try other values then the graph displays correctly.

foreach (var grp in q)
            {
                point = new DataPoint();
                 Sum1 = grp.Sum1 > 2 ? 2 : grp.Sum1;
                Sum1 = Sum1 < -2 ? -2 : Sum1;

                Sum2 = grp.Sum2 > 2 ? 2 : grp.Sum2;
                Sum2 = Sum2 < -2 ? -2 : Sum2;

                point.XValue = 0;
                point.YValues = new double[] { -0.5 };

                chart1.Series.Add(grp.Id.ToString());
                chart1.Series[grp.Id.ToString()].ChartType = SeriesChartType.Point;
                chart1.Series[grp.Id.ToString()].Label = grp.Id.ToString();
                chart1.Series[grp.Id.ToString()].Points.Add(point);
                chart1.Series[grp.Id.ToString()].ToolTip = "THEMES = " + Sum1 + "\n PRICES = " + Sum2;
                chart1.Series[grp.Id.ToString()].LabelToolTip = "THEMES = " + Sum1 + "\n PRICES = " + Sum2;
                chart1.Series[grp.Id.ToString()].MarkerSize = 11;

                chart1.Update();

                if (grp.Id.ToString() == "WW" || grp.Id.ToString() == "PB"
                    || grp.Id.ToString() == "AJ" || grp.Id.ToString() == "AK")
                {
                    avgTheme += (float)Sum1;
                    avgPrice += (float)Sum2;
                    count++;
                }
            }

UPDATE:

this line needed to be added, works only with .NET 4.5

          chart1.Series["ABC"].CustomProperties = "IsXAxisQuantitative=True";

回答1:

This is really weird! Looks like a very hard to believe bug. I played around but can only confirm that there seems to be no way to set a single Point to position 0 in a Series.

Here is a silly workaround:

S1.ChartType = SeriesChartType.Point;

for (int i=0; i < 2; i++)
{
    DataPoint point = new DataPoint();
    point.SetValueXY(i, -0.5);
    if (i > 0) point.Color = Color.Transparent;
    S1.Points.Add(point);
}

I wish I knew what this is about - Chart is so ill-documented there might still be some system to the madness..

Update: When you add a Timer and let its Tick remove the transparent 2nd Point, you can see how the 1st Point jumps from 0 to 1. So weird..



回答2:

Ah ha....

This is not a bug. This is the correct behavior for a SERIESCHARTTYPE.POINT of chart.

The purpose of the chart is to show a [b]series [/b]of values {from left to right}, not a set of X,Y points.

Series value 1 is 4 Series value 2 is 1 Series value 3 is 6 and so on.

So the bug is not in the chart, but in understanding what the chart type is designed for and meant to be used for.

You can see here how each element in the array only uses the y value for the point.

Could it be that you want to graph some points? Maybe you are confusing a chart with a graph? If you are trying to graph points this might help: https://www.daniweb.com/software-development/csharp/code/217204/function-plotting-in-c