How to add a chart programmatically into Doc?

Home Forums Open-Xml-Sdk How to add a chart programmatically into Doc?

Tagged: 

This topic contains 1 reply, has 1 voice, and was last updated by  rogersb 6 months, 1 week ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3816

    rogersb
    Participant

    after reviewing c061750_ISO_IEC_29500-1_2012.pdf, c061798_ISO_IEC_29500-4_2012.pdf, c061796_ISO_IEC_29500-2_2012.pdf, and c065533_ISO_IEC_29500-3_2015.pdf

    searches for chart and how to manipulate the xml blob return almost nothing.

    HELP!!

    Just trying to modify the 0-100% bar chart, I can modify the value, thats it. When the value exceeds 100% then the Axis needs to extend to show the full value, the other 2 items need to display with the green = value and grey = other part up to 100%. So if the values are 45, 60 and 215, then you have a green bar with 45, then the other 55% is grey color; 60 in green and 40 in grey and the grey stops at 100. Then the 215 bar extends out, only green so that the axis on the bottom represents about the limit, the grid marker number on the bottom may stop at 200 but have a bit more grid lines showing so you can imagine it lines up with 215.

    Well in the code? It finds the barchart on a word docx that is opened and read into memory

      byte[] byteArray = System.IO.File.ReadAllBytes(Server.MapPath("~/TemplateDoc.docx"));
      // byte[] byteArray = System.IO.File.ReadAllBytes(fileName);  //used for local testing, disable above
            using (MemoryStream mem = new MemoryStream())
            {
               mem.Write(byteArray, 0, (int)byteArray.Length);
               using (WordprocessingDocument wordDoc =
                        WordprocessingDocument.Open(mem, true))
               {
              //start of the chart processing
                MainDocumentPart mainDoc = wordDoc.MainDocumentPart;
                Body bodyDoc = mainDoc.Document.Body;
    

    And then the logic I used adapted from ericwhite.com to get to that chart,

    
             var mdXDoc = mainDoc.GetXDocument();
             var cc2 = mdXDoc.Descendants(W.sdt)
            .FirstOrDefault(sdt => (string)sdt.Elements(W.sdtPr).Elements(W.tag).Attributes(W.val).First() == "Chart1");
             if (cc2 != null)
             {
                 var chartRid = "rId5";//        (string)cc.Descendants(C.chart).Attributes(R.id).FirstOrDefault();
                 if (chartRid != null)
                 {
                   ChartPart chartPart = (ChartPart)mainDoc.GetPartById(chartRid);
                   GeneratePartContent(chartPart);  // THIS IS ADDED AND BREAKS IT
                                
                   UpdateChart(chartPart, chartData);
                   var newContent = cc2.Elements(W.sdtContent).Elements().Select(e => new XElement(e));
                           // cc.AddAfterSelf(newContent)
                   cc2.ReplaceWith(newContent);
                   mainDoc.PutXDocument();
                 }
             }
    

    It would work fine until I used the sdk productivity tool to get the chart, the chartPart is instantiated here, and then in the call to GeneratePartContent(chartPart) its a void method that modifies the chartPart and returns.

    This was taken right out of the reflected code. So while the code using XElements works to update the value? I could not make it work to extend the charts axis. Using the added in void method? I can directly control the charts axis, but the values are wrong.

    I tried running the UpdateChart(…) method first then GeneratePartContent(..) but its the same either way.

    I will try with 2 word files, a regular bar chart, and then one where i use Word to extend out the axis and see how they look different, but finding detail on the barchart is not easy

    #3823

    rogersb
    Participant

    The problem happens when trying to modify the bar chart value? when you make a barchart it uses an Excel clip to store the values. If this gets closed? apparently it cannot be re-opened. Its gone.

    So I create a new barchart and modify the values from its excel clip. the problem is this create a binary blob representing that data and serializes it into a binary string.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.