Align Bar Graph, c# using Novacode

Asked

Viewed 75 times

0

How can I align my bar chart in the center of my docx document?

My code:

    using Novacode; //adquirido na extensão DocX        
    namespace WF_TermograFacil
    {
    private void button1_Click(object sender, EventArgs e)
    {
        using (var docX = DocX.Load(@"C:\TermograFacil\Layout.docx"))
        {
                // Rotina para inserir um gráfico de barras dentro do docx
                BarChart c = new BarChart();
                c.BarDirection = BarDirection.Bar;
                c.BarGrouping = BarGrouping.Standard;
                c.GapWidth = 160;
                c.AddLegend(ChartLegendPosition.Right, false);

                // Create data.
                List<ChartData> company1 = ChartData.CreateCompanyList1();
                List<ChartData> company2 = ChartData.CreateCompanyList2();
                List<ChartData> company3 = ChartData.CreateCompanyList3();

                // Create and add series
                Series s1 = new Series("Temp. Mín.");
                s1.Color = Color.FromArgb(0 , 176, 80);
                s1.Bind(company1, "Mounth", "Money");
                c.AddSeries(s1);
                Series s2 = new Series("Temp. Med.");
                s2.Color = Color.FromArgb(247, 150, 70);
                s2.Bind(company2, "Mounth", "Money");
                c.AddSeries(s2);
                Series s3 = new Series("Temp. Máx.");
                s3.Color = Color.FromArgb(255, 30, 30);
                s3.Bind(company3, "Mounth", "Money");
                c.AddSeries(s3);

                docX.InsertChart(c);
                docX.SaveAs(@"C:\TermograFacil\Final.docx");
                //Process.Start("WINWORD.EXE", @"C:\TermograFacil\Final.docx");

            }
            MessageBox.Show("O relatório foi gerado com sucesso!");

    }
    //Essa parte é para alimentar o Gráfico
    private class ChartData
    {
        public String Mounth { get; set; }
        public Double Money { get; set; }

        public static List<ChartData> CreateCompanyList1()
        {
            List<ChartData> company1 = new List<ChartData>();
            company1.Add(new ChartData() { Mounth = "Ar1", Money = 100 });
            return company1;
        }

        public static List<ChartData> CreateCompanyList2()
        {
            List<ChartData> company2 = new List<ChartData>();
            company2.Add(new ChartData() { Mounth = "Ar1", Money = 80 });
            return company2;
        }

        public static List<ChartData> CreateCompanyList3()
        {
            List<ChartData> company3 = new List<ChartData>();
            company3.Add(new ChartData() { Mounth = "Ar1", Money = 80 });
            return company3;
        }
    }

    }

Make it easier to understand. I do it as follows to align the contents of a text within a table:

    tabela.Rows[0].Cells[2].Paragraphs[0].Append("Texto Centralizado").Alignment = Novacode.Alignment.center;

If I could insert the Chart inside the table it would be a way to align too, but the extension does not give me this possibility.

  • Friend, could you elaborate on your question? Maybe putting a little more of the code to try to understand which component you are using, how you are using it, etc...

  • All right Milton! I put more information!

  • Have you tried anything like that? Creates a paragraph Novacode.Paragraph P1 = docx.Insertparagraph(); Then arrow its alignment to center P1.Alignment = Alignment.center; and within the paragraph put the graph?

  • It was just an idea, I don’t really know if it works!

No answers

Browser other questions tagged

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