Home > Software engineering >  I have very similar charts in my c# form app (10-20). How can I program this charts with one method
I have very similar charts in my c# form app (10-20). How can I program this charts with one method

Time:01-11

I have very similar charts in my c# form app (10-20) (Chart1,Chart2,Chart3.....). The only difference of the charts are the data series. Normally I have to repeat the same code 10–20 times in my project.

How can I program the charts with one method, that I can call multiple times, when I can pass the chart name as a variable. I have searched a lot but found really no solution for that.

My code for one chart is as following:

public void Draw_Chart()
{
    chart1.Series.Clear();
    chart1.Titles.Clear();
    chart1.Legends.Clear();
    var newSeries_1 = new Series();
    var newSeries_2 = new Series();
    newSeries_1.ChartType = SeriesChartType.Line;
    newSeries_2.ChartType = SeriesChartType.Line;
    chart1.Series.Add(newSeries_1);
    chart1.Series.Add(newSeries_2);
    List_X_Axis.Clear();

    for (int w = 0 ; w <= 940; w  )
    {
        parameter_value_chartX[w] = w;
        //parameter_value_chartY1[w] = 150;
        //parameter_value_chartY1[w] = 250;
    }
        
    newSeries_1.Points.DataBindXY(parameter_value_chartX, parameter_value_chartY1);
    newSeries_2.Points.DataBindXY(parameter_value_chartX, parameter_value_chartY2);

    chart1.BackColor = Color.Gray;
    chart1.ChartAreas[0].AxisX.Title = ".";
    chart1.ChartAreas[0].AxisY.Title = "mm";
    chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Cyan;
    chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Cyan;
    chart1.ChartAreas[0].AxisX.Minimum = 0d;
    chart1.Series[0].Color = Color.Cyan;
    chart1.ChartAreas[0].AxisY.Minimum = cur_scale_min;
    chart1.ChartAreas[0].AxisY.Maximum = cur_scale_max;
    chart1.ChartAreas[0].AxisY2.Enabled = AxisEnabled.True;
    chart1.ChartAreas[0].AxisY2.Minimum = pos_scale_min;
    chart1.ChartAreas[0].AxisY2.Maximum = pos_scale_max;
    chart1.ChartAreas[0].AxisY2.Title = "% of Max. Current";
    chart1.ChartAreas[0].AxisY2.TitleForeColor = Color.Yellow;
    chart1.ChartAreas[0].AxisY2.LabelStyle.ForeColor = Color.Yellow;
    chart1.ChartAreas[0].AxisY2.MajorGrid.LineColor = Color.Silver;
    chart1.ChartAreas[0].AxisY2.MajorTickMark.LineColor = Color.Silver;
    chart1.Series[1].YAxisType = AxisType.Secondary;
    chart1.Series[1].Color = Color.Yellow;
}

CodePudding user response:

public void Draw_Chart(ChartType chart, Series[] series)
{
    ...
    chart.Series.Clear();
    series.ForEach(s=>chart.Series.Add(s));
    ...
}

maybe there is something I don't catch.....it cannot be so easy.

CodePudding user response:

I found the solution acc. to Ralf's comment in the following way:

...    
Draw_Chart(chart1);
...    
Draw_Chart(chart2);
...    
Draw_Chart(chart3);
...


public void Draw_Chart(Chart chart)
{
chart.Series.Clear();
chart.Titles.Clear();
chart.Legends.Clear();
var newSeries_1 = new Series();
var newSeries_2 = new Series();
newSeries_1.ChartType = SeriesChartType.Line;
newSeries_2.ChartType = SeriesChartType.Line;
chart.Series.Add(newSeries_1);
chart.Series.Add(newSeries_2);

...
}
  • Related