Pie Chart by code line c#

Asked

Viewed 419 times

0

I need to create charts at runtime, as the user asks. I found a code on the site Ti4fun in it the guy can alter the properties of 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’t edit it. Someone knows how I can do this?

1 answer

0

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

In this 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 pick it up but tried to do some experiments and it is very boring so my suggestion is the following.

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

  1. Declare variable in form scope

    public partial class Form1 : Form
    {
        System.Windows.Forms.DataVisualization.Charting.Chart cht_MacroOndas;
    
  2. Check when updating the chart if the variable has not already been instantiated and only create a new case need

    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 clean the collections when using the previous Chart object, I put this Configurarchat method just to make it clear that at this point you would set the graph, in it you would have to reset the Series collections, Legends, Chartareas or any other you add when filling in the data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.