Question

I have looked at "similar" questions but can't actually find anything that applies, surprisingly. I calculate how many data points I would like to create on my Mschart Line Chart with a sum using imported converted data. Using the result of this, I divide it by another number that varies and this is the space I want to have between each datapoint. 124 is constant.

300 (varies) / 124 (X-Axis) = 2.419354838709677 (this is the space I want between each point)

I thought about creating a for loop like so

for (double i = 1; i < samples_hd; i++)
{
    // distribute data points evenly here
}

A file is going to be read through into the chart, so far I have manually added data points as I'm not sure at all how to add them like above ^.

var sn = new System.Windows.Forms.DataVisualization.Charting.Series(English.Sam_Num);
sn.ChartType = SeriesChartType.Line;
sn.Points.Add(new DataPoint(9, 30));
sn.Points.Add(new DataPoint(150, 28));
sn.XAxisType = AxisType.Primary;
Chart_Line.Series.Add(sn);

Chart_Line.ChartAreas[0].AxisX.Minimum = 0;
Chart_Line.ChartAreas[0].AxisX.Maximum = 124;
Chart_Line.ChartAreas[0].AxisX.Interval = 15;
Chart_Line.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
Was it helpful?

Solution

It took such a long time to get it working, but it works :) My graph actually has 3 series, but for the example I will just show the 2 series.

private void Main(byte[] rec_arr)
{
    var series1 = new Series("series1");
    series1 .ChartType = SeriesChartType.Line;
    series1 .MarkerStyle = MarkerStyle.Circle;
    series1 .MarkerSize = 3;
    series1 .YAxisType = AxisType.Primary;
    series1 .Color = Color.Navy;

    var series2 = new Series("series2");
    series2 .ChartType = SeriesChartType.Line;
    series2 .MarkerStyle = MarkerStyle.Triangle;
    series2 .MarkerSize = 3;
    series2 .YAxisType = AxisType.Primary;
    series2 .Color = Color.Crimson;

    foreach (var series in Chart1.Series)
    {
        series.Points.Clear();
    }

    double interval1 = 0;
    double interval2 = File_Details.time / (double)124;
    int Offset2 = 502;
    int Offset3 = 750;
    float data1, data2;

    if (File_Details.time == 0)
    {
        Label_Error_Graph.Visible = true;
        Chart1.ChartAreas[0].AxisX.Maximum = 0;
        Chart1.ChartAreas[0].AxisX.Minimum = 0;
        Chart1.Series[0].Points.Add(0);
        Chart1.Series[1].Points.Add(0);
    }
    else
    {
        Label_Error_Graph.Visible = false;
    }

    for (interval1 = 0; interval1 < File_Details.time; interval1 += interval2)
    {
        data1 = DecodeSingle(rec_arr, Offset3);
        if (Chart1.Series.IndexOf("series1") == -1)
        {
            Chart1.Series.Add(series1);
        }
        Offset3 = Offset3 + 2;

        if (data1 < 300)
        {
            Chart1.Series[0].Points.AddXY(interval1, supply);
        }
        else
        {
            Chart1.Series[0].Points.AddXY(interval1, 300);
        }
    }

    for (interval1 = 0; interval1 < File_Details.time; interval1 += interval2)
    {
        data2 = DecodeSingle(rec_arr, Offset2) / (float)100;
        if (Chart1.Series.IndexOf("series2") == -1)
        {
            Chart1.Series.Add(series2);
        }
        Offset2 = Offset2 + 2;

        if (data2 < 150)
        {
            Chart1.Series[1].Points.AddXY(interval1, data2);
        }
        else
        {
            Chart1.Series[1].Points.AddXY(interval1, 150);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top