Copy ChartPart in Word document?

Home Forums WordprocessingML Copy ChartPart in Word document?

This topic contains 0 replies, has 1 voice, and was last updated by  Michael 7 years ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #4303

    Michael
    Participant

    Hi,

    I do have a Chart (pie chart) in a Word document which I am trying to copy. I have followed other guides from you and managed to update a Chart with new data but now I do need to copy it first and then update the copy with new data. Any advice of how to do this?

    The main function looks like this, please not that it is used in a recursive function to build the whole Word document. The code outside of the pie_cart content is left out for simplicity, let me know if you need that code.

    I do greatly appreciate all help you can provide.

    Thanks! /Michael

    XElement content = GetContentControlByTag(element, “Content”);
    XElement selectRowsContentControl = GetContentControlByTag(element, “SelectRows”);
    string selector = GetContentControlContents(selectRowsContentControl);

    //Get chart title
    XElement selectTitleContentControl = GetContentControlByTag(element, “Title”);
    string chartTitle = GetContentControlContents(selectTitleContentControl);

    var tableData = databaseHandler.GetSqlQueryResults(selector, hostVulnerabilityItems);
    List<DataRow> dataList = tableData.AsEnumerable().ToList();
    List<string> columNamesList = new List<string>();
    foreach (DataColumn column in tableData.Columns)
    {
    string temp = column.ColumnName;
    columNamesList.Add(column.ColumnName);
    }

    string[] columnNames = columNamesList.ToArray();

    //Fetch values
    List<double[]> valuesList = new List<double[]>();
    foreach (DataRow row in dataList)
    {
    List<double> valuesListTemp = new List<double>();

    foreach (DataColumn column in tableData.Columns)
    {
    valuesListTemp.Add(Convert.ToDouble(row[column].ToString()));
    }

    valuesList.Add(valuesListTemp.ToArray());
    }

    var chart1Data = new OpenXmlPowerTools.ChartData
    {
    SeriesNames = new[] { chartTitle, },
    CategoryNames = columnNames,
    Values = valuesList.ToArray(),
    };

    var chartRid = (string)content.Descendants(C.chart).Attributes(R.id).FirstOrDefault();
    if (chartRid != null)
    {
    ChartPart chartPart = (ChartPart)wordDoc.MainDocumentPart.GetPartById(chartRid);
    //My guess is that it would be best to copy the chartPart here and add it to the MainDocumentPart instead of just updating chart. I have not manage to do this though.
    Helpers.ChartUpdater.UpdateChart(chartPart, chart1Data);
    }

    return content.Descendants(w + “drawing”).FirstOrDefault();

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.