Pie Chart per line of code C #

0

I need to create graphics at runtime, as the user asks. I found a code on the TI4Fun website TI4Fun in it the guy can change the properties of the chart the way I want it. The problem is that I need to create a new one when I use:

var cht_MacroOndas = new System.Windows.Forms.DataVisualization.Charting.Chart();
this.Controls.Add(cht_MacroOndas);

I can create the chart , but I can not edit it. Does anyone know how I can do this?

    
asked by anonymous 22.04.2016 / 23:14

1 answer

0

Hello, I know it's been a while since you posted and warned me via face but only now I could actually look at this.

In your example you are creating a new graphic and adding in the collection, you could check if there is already an instance of the class in the Controls collection and get it but I tried to do some experiments and it is very annoying so my suggestion is as follows.

An object you add to the collection keeps your reference as long as the scope in which it was created is still active, what you can do is the following.

  • Declare the variable in the form's scope

    public partial class Form1 : Form
    {
        System.Windows.Forms.DataVisualization.Charting.Chart cht_MacroOndas;
    
  • Check when the graphic is updated if the variable has not already been instantiated and only create a new case if necessary

    private void button1_Click(object sender, EventArgs e)
    {
            if (cht_MacroOndas == null)
            {
                    cht_MacroOndas = new Chart();
                    this.Controls.Add(cht_MacroOndas);
            }
    
            ConfigurarChat(cht_MacroOndas);
    }
    
  • Remember to clear collections when using the previous chart object, I put this SetupChat method just to make it clear that at this point you would set the chart, in it you would have to zero the Series, Legends, ChartAreas, or any other collections that you add when filling in the data.

        
    12.05.2016 / 22:05