xceedsoftware / DocX

Fast and easy to use .NET library that creates or modifies Microsoft Word files without installing Word.
Other
1.77k stars 472 forks source link

C# .NET Project Editing a Chart #395

Open R-Ziegler opened 3 years ago

R-Ziegler commented 3 years ago

In the word template that I am working with the chart has 2 series. When I access the chart to edit it there is only one series. Adding a series to the existing chart causes a corrupted word file.

charts[0].Series[0].Values = DataList1; charts[0].Series[1].Values = DataList2; //ERROR here, out of range, due to only finding one Series. charts[0].Save();

Capture6 Chart picture to prove that there is more than one series in the template chart.

Trial License to test feasability.

XceedBoucherS commented 3 years ago

Hi, It should work. Here's a sample creating a Chart and then modifying its series's values.

` class Program { static void Main( string[] args ) { using( var document = DocX.Create( @"LineChart.docx" ) ) { // Add a title document.InsertParagraph( "Line Chart" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;

    // Create a line chart.
    var c = new LineChart();

    // Create the data.
    var canada = ChartData.CreateCanadaExpenses();
    var usa = ChartData.CreateUSAExpenses();
    var brazil = ChartData.CreateBrazilExpenses();

    // Create and add series
    var s1 = new Series( "Brazil" );
    s1.Color = Color.Yellow;
    s1.Bind( brazil, "Category", "Expenses" );
    c.AddSeries( s1 );

    var s2 = new Series( "USA" );
    s2.Color = Color.Blue;
    s2.Bind( usa, "Category", "Expenses" );
    c.AddSeries( s2 );

    var s3 = new Series( "Canada" );
    s3.Color = Color.Red;
    s3.Bind( canada, "Category", "Expenses" );
    c.AddSeries( s3 );

    // Insert chart into document
    document.InsertChart( c );

    document.Save();

    var doc = DocX.Load( @"LineChart.docx" );
    var charts = doc.Charts;

    charts[0].Series[0].Values = new List<double>() { 100, 75, 50, 0 };
    charts[0].Series[1].Values = new List<double>() { 0, 25, 50, 100 };

    charts[0].Save();

    doc.SaveAs("UpdatedDocdocx");
  }
}

}

internal class ChartData { public string Category { get; set; } public double Expenses { get; set; }

public static List<ChartData> CreateCanadaExpenses()
{
  var canada = new List<ChartData>();
  canada.Add( new ChartData() { Category = "Food", Expenses = 100 } );
  canada.Add( new ChartData() { Category = "Housing", Expenses = 120 } );
  canada.Add( new ChartData() { Category = "Transportation", Expenses = 140 } );
  canada.Add( new ChartData() { Category = "Health Care", Expenses = 150 } );
  return canada;
}

public static List<ChartData> CreateUSAExpenses()
{
  var usa = new List<ChartData>();
  usa.Add( new ChartData() { Category = "Food", Expenses = 200 } );
  usa.Add( new ChartData() { Category = "Housing", Expenses = 150 } );
  usa.Add( new ChartData() { Category = "Transportation", Expenses = 110 } );
  usa.Add( new ChartData() { Category = "Health Care", Expenses = 100 } );
  return usa;
}

public static List<ChartData> CreateBrazilExpenses()
{
  var brazil = new List<ChartData>();
  brazil.Add( new ChartData() { Category = "Food", Expenses = 125 } );
  brazil.Add( new ChartData() { Category = "Housing", Expenses = 80 } );
  brazil.Add( new ChartData() { Category = "Transportation", Expenses = 110 } );
  brazil.Add( new ChartData() { Category = "Health Care", Expenses = 60 } );
  return brazil;
}

}`

R-Ziegler commented 3 years ago

@XceedBoucherS Thanks for attempting to resolve my problem, unfortunately this is not my problem. I have already looked at the code examples extensively. What I need to do for my company is edit an existing chart in a template, not add a new one. A feature that your premium service claims to offer. Unfortunately when I get the chart object from the word file I am unable to access the second series.

A requirement of my project is in place editing of a chart. Not adding a new chart.

XceedBoucherS commented 3 years ago

Hi R-Ziegler, I was able to open MS Word, add a line chart and close it. Then use "Xceed Words for .NET" and load this docx and modify the charts[0].Series (there are 3 Series).

Maybe your chart template has a specific format. Could you attached it so we can analyze it ?

Thank you.

R-Ziegler commented 3 years ago

image Chart Only.docx

To be clear, on this chart I can edit charts[0].Series[0], however if I attempt to edit the Series[1] I get an out of range error as the construct only has one series in it.

XceedBoucherS commented 3 years ago

Hi, Ok, I found the problem. If I create a Chart with "Xceed Words for .NET" and add 2 series, the ooxml will show a plotArea containing 1 Line Chart which contains 2 series. In your document, the ooxml shows a plotArea containing 2 line charts, each of them containing their own single series. Probably because its a 2 Y-axis graph.

So you have a different chart format than expected. We will look to manage those chart formats. Thank you.

R-Ziegler commented 3 years ago

Okay, so the reason the chart type is different is because I need the second series to be on the secondary axis. After switching to a normal line chart it all works as expected, like you said in the first comment. Unfortunately that isn't an option for the report template my client needs. Is there any work around to this problem? Unfortunately simply trying to edit the phantom chart seems unreliable. The first several times I ended up with a corrupt word file. After deleting and recreating the chart I simply can't access the "second" chart.

XceedBoucherS commented 3 years ago

Hi, The problem has been identified. The fix will be included in the next release v1.9. Basically, your situation is different than usual. After the fix, you will need to retrieve the 2 different charts included in the chart. Something like: `var charts = doc.Charts;

charts[0].Series[0].Values = new List() { 1, 1, 1, 1 }; charts[1].Series[0].Values = new List() { 0, 25, 50, 100 };

charts[0].Save(); charts[1].Save();`

You don't have 2 Series in 1 chart, you have 2 charts with 1 Series in your Chart.

If you are a trial user or a paying customer of "Xceed Words for .NET", I invite you to contact support@xceed.com to get more help with your issues. Thank you.