Search code examples
c#mschart

Points on chart graphic don't fit y-lines


Points on my second chart don't fit y-axis as you can see here:

enter image description here

Points values are exactly 50.0000, 49.9999, 49.9998 and 50.0001. But they are not on lines. And when I add point and with it increase number of values on y-axis, then points would fit y-axis, like in this picture.

Here is my code (sorry for Serbian text values)

TacnostVage tacnost = bazaPodataka.UcitajTacnostVage(Convert.ToString(dataGridView2.SelectedRows[0].Cells[2].Value), Convert.ToInt32(comboBox18.Text));
            List<TestTacnostVage> testoviTacnost = bazaPodataka.UcitajTestoveTacnostVage(Convert.ToString(dataGridView2.SelectedRows[0].Cells[2].Value), Convert.ToInt32(comboBox18.Text));

            chart2.ChartAreas.Clear();
            chart2.Series.Clear();
            prikažiToolStripMenuItem.DropDownItems.Clear();
            tabeluToolStripMenuItem.DropDownItems.Clear();

            string format = Convert.ToString(vaga.Podeljak);
            format = format.Remove(format.Length - 1, 1) + "0";

            if (testoviTacnost.Count != 0)
            {
                for (int i = 0; i < tacnost.NominalneMase.Count(); i++)
                {
                    ChartArea area = new ChartArea();
                    Series series = new Series();

                    area.AxisY.MajorGrid.LineColor = Color.LightGray;
                    area.AxisX.MajorGrid.LineColor = Color.LightGray;
                    area.AxisY.LabelStyle.Format = format;
                    area.BorderColor = Color.LightGray;
                    area.BorderDashStyle = ChartDashStyle.Solid;
                    area.AxisY.Interval = vaga.Podeljak;
                    area.Name = "ChartArea" + (i + 1);
                    series.ChartType = SeriesChartType.Point;
                    series.ChartArea = "ChartArea" + (i + 1);
                    series.Name = "Tačka" + (i + 1);

                    string text = "";
                    TegoviTacnostVaga tegoviTacnost = bazaPodataka.UcitajTegoveTacnostVage(Convert.ToString(dataGridView2.SelectedRows[0].Cells[2].Value), Convert.ToInt32(comboBox18.Text), i);
                    if (tegoviTacnost != null)
                    {
                        for (int j = 0; j < tegoviTacnost.Proizvodjac.Count(); j++)
                        {
                            text += tegoviTacnost.Proizvodjac[j] + " ";
                            text += tegoviTacnost.SerijskiBrojevi[j] + " ";
                            text += tegoviTacnost.NominalneMase[j] + "g";
                            text += (j == tegoviTacnost.Proizvodjac.Count() - 1 ? "" : "\n");
                        }
                    }

                    series.LegendText = (text == "" ? "Nema podataka" : text);

                    for (int j = 0; j < testoviTacnost.Count(); j++)
                        series.Points.AddXY(testoviTacnost[j].RedniBrojTesta, testoviTacnost[j].RezultatiMerenja[i]);

                    area.AxisY.StripLines.Add(new StripLine() { BorderColor = Color.Red, IntervalOffset = (tacnost.RezultatiMerenja[i].Average() + koeficijentTacnost * ponovljivost.ReferentnaVrednost), Text = "Gornja granica: " + Convert.ToDouble(tacnost.RezultatiMerenja[i].Average() + koeficijentTacnost * ponovljivost.ReferentnaVrednost).ToString(format) });
                    area.AxisY.StripLines.Add(new StripLine() { BorderColor = Color.Red, IntervalOffset = (tacnost.RezultatiMerenja[i].Average() - koeficijentTacnost * ponovljivost.ReferentnaVrednost), Text = "Donja granica: " + Convert.ToDouble(tacnost.RezultatiMerenja[i].Average() - koeficijentTacnost * ponovljivost.ReferentnaVrednost).ToString(format) });

                    area.AxisY.Maximum = area.AxisY.StripLines[0].IntervalOffset + area.AxisY.Interval;

                    if (series.Points.FindMaxByValue().YValues[0] >= area.AxisY.Maximum)
                        area.AxisY.Maximum = series.Points.FindMaxByValue().YValues[0] + area.AxisY.Interval;

                    area.AxisY.Minimum = area.AxisY.StripLines[1].IntervalOffset - area.AxisY.Interval;

                    if (series.Points.FindMinByValue().YValues[0] <= area.AxisY.Minimum)
                        area.AxisY.Minimum = series.Points.FindMinByValue().YValues[0] - area.AxisY.Interval;

                    chart2.ChartAreas.Add(area);
                    chart2.Series.Add(series);
                }
            }  

Solution

  • I found solution, but I'm not sure if this explanation is true. The problem was Axis-Y maximum. Charts Axis-Y interval was 0.0001 (4 decimals), but in my code, I put maximum to be StripLines IntervalOffset (which was more than 4 decimals) plus Charts Interval (in result that is more than 4 decimals). So probably this happens when your Chars Axis-Y Maximum and your Interval (if you set Interval) have different number of decimals. So I just simply rounded Strip Lines InvervalOffset to 4 decimals (in this case), and put Axis-Y Maximum to have 4 decimals like Interval has.