Re: Copying chart

2018-09-04 Thread Alain FAGOT BÉAREZ
Hi FD,

The code has been merged by r1840076. It might appear in release 4.0.0 or 4.0.1 
(we are currently busy with 4.0.0 RC1).

Best regards,
Alain

> On 4 Sep 2018, at 00:27, Alain FAGOT BÉAREZ  wrote:
> 
> Hi FD,
> 
> I think all you need is the following method on XSSFDrawing:
> 
> ```
>/**
> * Imports the chart from the srcChart into this drawing.
> *
> * @param srcChart
> *the source chart to be cloned into this drawing.
> * @return the newly created chart.
> * @throws XmlException
> * @throws IOException
> */
>public XSSFChart importChart(XSSFChart srcChart) throws IOException, 
> XmlException {
>CTTwoCellAnchor anchor = ((XSSFDrawing) 
> srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0);
>CTMarker from = (CTMarker) anchor.getFrom().copy();
>CTMarker to = (CTMarker) anchor.getTo().copy();
>XSSFClientAnchor destAnchor = new XSSFClientAnchor(from, to);
>destAnchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
>XSSFChart destChart = createChart(destAnchor);
>destChart.getCTChartSpace().set(srcChart.getCTChartSpace().copy());
>destChart.getCTChart().set(srcChart.getCTChart().copy());
>return destChart;
>}
> ```
> 
> I feel that retrieving the source chart and creating the new destination 
> drawing are better handled outside.
> 
> I will open a PR for code review.
> 
> Best regards,
> Alain
> 
> 
>> On 24 Aug 2018, at 10:50, monnomiznog...@gmail.com wrote:
>> 
>> Hi Alain,
>> 
>> I'll paste some test code here. In my work we have a lot of restrictions 
>> when it comes to websites.
>> 
>> Sorry I don't know how to format this code.
>> 
>> Important: the chartModel.xlsx should contain a chart, preferably with no 
>> relations. After all you need the model to be copied to your report but then 
>> you must at least implement the data source for the chart, maybe also the 
>> title, the series names, etc. But the heavy customization of the chart is 
>> already done in the model.
>> 
>>   //Open chart model file
>>  //My use experience: one chart par xlsx model file, in one sheet. Could 
>> be otherwise of course.
>>  FileInputStream in = new FileInputStream("c:/temp/chartModel.xlsx");
>>   XSSFWorkbook srcWB = new XSSFWorkbook(in);
>>   XSSFWorkbook destWB = new XSSFWorkbook();
>>   XSSFSheet destSH = destWB.createSheet();
>>   XSSFDrawing destDR = destSH.createDrawingPatriarch();
>>   int sheetNum = 0; 
>>   XSSFSheet srcSH = srcWB.getSheetAt(sheetNum);
>> 
>>   List srcRels = 
>> srcSH.createDrawingPatriarch().getRelations();
>>   for (POIXMLDocumentPart srcRel : srcRels)
>>   {
>> if (srcRel instanceof XSSFChart)
>> {
>>   XSSFChart srcChart = (XSSFChart) srcRel;
>>  
>>  //Create chartSpace and chart by parsing source chart
>>  CTChartSpace chartSpace = 
>> ChartSpaceDocument.Factory.parse(srcChart.getPackagePart().getInputStream(), 
>> POIXMLTypeLoader.DEFAULT_XML_OPTIONS).getChartSpace(); 
>>  CTChart ctc = chartSpace.getChart();
>> 
>>   //XSSFClientAnchor origAnch = chart.getGraphicFrame().getAnchor();
>>  //Frame is null ! I tried to get anchor using the following:
>>   CTMarker from = ((XSSFDrawing) 
>> srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0).getFrom();
>>   CTMarker to = ((XSSFDrawing) 
>> srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0).getTo();
>>   XSSFClientAnchor anchor = new XSSFClientAnchor((int) from.getColOff(), 
>> (int) from.getRowOff(), (int) to.getColOff(), (int) to.getRowOff(), 
>> from.getCol(), from.getRow(), to.getCol(), to.getRow());
>>   anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
>>  //Create chart in the destination workbook
>>   XSSFChart destChart = destDR.createChart(anchor);
>>  
>>  //A new XSSFChart constructor is needed maybe. The following 2 
>> lines use reflection in order to force values for these 2 private variables 
>> of XSSFChart
>>   Utilities.setVariable(destChart, "chartSpace", chartSpace, true);
>>   Utilities.setVariable(destChart, "chart", ctc, true);
>>}
>>  }
>>  
>>  //Write the new workbook (could also be one you already have opened for 
>> edition)
>>   FileOutputStream out = new FileOutputStream("c:/temp/test.xlsx");
>>   destWB.write(out);
>>   out.flush();
>>   out.close();
>>   in.close();
>>   srcWB.close();
>>   destWB.close();
>>  
>> 
>> 
>> -
>> To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
>> For additional commands, e-mail: dev-h...@poi.apache.org
> 



Re: Copying chart

2018-09-03 Thread Alain FAGOT BÉAREZ
Hi FD,

I think all you need is the following method on XSSFDrawing:

```
/**
 * Imports the chart from the srcChart into this drawing.
 *
 * @param srcChart
 *the source chart to be cloned into this drawing.
 * @return the newly created chart.
 * @throws XmlException
 * @throws IOException
 */
public XSSFChart importChart(XSSFChart srcChart) throws IOException, 
XmlException {
CTTwoCellAnchor anchor = ((XSSFDrawing) 
srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0);
CTMarker from = (CTMarker) anchor.getFrom().copy();
CTMarker to = (CTMarker) anchor.getTo().copy();
XSSFClientAnchor destAnchor = new XSSFClientAnchor(from, to);
destAnchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
XSSFChart destChart = createChart(destAnchor);
destChart.getCTChartSpace().set(srcChart.getCTChartSpace().copy());
destChart.getCTChart().set(srcChart.getCTChart().copy());
return destChart;
}
```

I feel that retrieving the source chart and creating the new destination 
drawing are better handled outside.

I will open a PR for code review.

Best regards,
Alain


> On 24 Aug 2018, at 10:50, monnomiznog...@gmail.com wrote:
> 
> Hi Alain,
> 
> I'll paste some test code here. In my work we have a lot of restrictions when 
> it comes to websites.
> 
> Sorry I don't know how to format this code.
> 
> Important: the chartModel.xlsx should contain a chart, preferably with no 
> relations. After all you need the model to be copied to your report but then 
> you must at least implement the data source for the chart, maybe also the 
> title, the series names, etc. But the heavy customization of the chart is 
> already done in the model.
> 
>//Open chart model file
>   //My use experience: one chart par xlsx model file, in one sheet. Could 
> be otherwise of course.
>   FileInputStream in = new FileInputStream("c:/temp/chartModel.xlsx");
>XSSFWorkbook srcWB = new XSSFWorkbook(in);
>XSSFWorkbook destWB = new XSSFWorkbook();
>XSSFSheet destSH = destWB.createSheet();
>XSSFDrawing destDR = destSH.createDrawingPatriarch();
>int sheetNum = 0; 
>XSSFSheet srcSH = srcWB.getSheetAt(sheetNum);
> 
>List srcRels = 
> srcSH.createDrawingPatriarch().getRelations();
>for (POIXMLDocumentPart srcRel : srcRels)
>{
>  if (srcRel instanceof XSSFChart)
>  {
>XSSFChart srcChart = (XSSFChart) srcRel;
>   
>   //Create chartSpace and chart by parsing source chart
>   CTChartSpace chartSpace = 
> ChartSpaceDocument.Factory.parse(srcChart.getPackagePart().getInputStream(), 
> POIXMLTypeLoader.DEFAULT_XML_OPTIONS).getChartSpace(); 
>   CTChart ctc = chartSpace.getChart();
> 
>//XSSFClientAnchor origAnch = chart.getGraphicFrame().getAnchor();
>   //Frame is null ! I tried to get anchor using the following:
>CTMarker from = ((XSSFDrawing) 
> srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0).getFrom();
>CTMarker to = ((XSSFDrawing) 
> srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0).getTo();
>XSSFClientAnchor anchor = new XSSFClientAnchor((int) from.getColOff(), 
> (int) from.getRowOff(), (int) to.getColOff(), (int) to.getRowOff(), 
> from.getCol(), from.getRow(), to.getCol(), to.getRow());
>anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
>   //Create chart in the destination workbook
>XSSFChart destChart = destDR.createChart(anchor);
>   
>   //A new XSSFChart constructor is needed maybe. The following 2 
> lines use reflection in order to force values for these 2 private variables 
> of XSSFChart
>Utilities.setVariable(destChart, "chartSpace", chartSpace, true);
>Utilities.setVariable(destChart, "chart", ctc, true);
> }
>   }
>   
>   //Write the new workbook (could also be one you already have opened for 
> edition)
>FileOutputStream out = new FileOutputStream("c:/temp/test.xlsx");
>destWB.write(out);
>out.flush();
>out.close();
>in.close();
>srcWB.close();
>destWB.close();
>   
> 
> 
> -
> To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
> For additional commands, e-mail: dev-h...@poi.apache.org



Re: Copying chart

2018-08-24 Thread monnomiznogoud
Hi Alain,

I'll paste some test code here. In my work we have a lot of restrictions when 
it comes to websites.

Sorry I don't know how to format this code.

Important: the chartModel.xlsx should contain a chart, preferably with no 
relations. After all you need the model to be copied to your report but then 
you must at least implement the data source for the chart, maybe also the 
title, the series names, etc. But the heavy customization of the chart is 
already done in the model.

//Open chart model file
//My use experience: one chart par xlsx model file, in one sheet. Could 
be otherwise of course.
FileInputStream in = new FileInputStream("c:/temp/chartModel.xlsx");
XSSFWorkbook srcWB = new XSSFWorkbook(in);
XSSFWorkbook destWB = new XSSFWorkbook();
XSSFSheet destSH = destWB.createSheet();
XSSFDrawing destDR = destSH.createDrawingPatriarch();
int sheetNum = 0; 
XSSFSheet srcSH = srcWB.getSheetAt(sheetNum);

List srcRels = 
srcSH.createDrawingPatriarch().getRelations();
for (POIXMLDocumentPart srcRel : srcRels)
{
  if (srcRel instanceof XSSFChart)
  {
XSSFChart srcChart = (XSSFChart) srcRel;

//Create chartSpace and chart by parsing source chart
CTChartSpace chartSpace = 
ChartSpaceDocument.Factory.parse(srcChart.getPackagePart().getInputStream(), 
POIXMLTypeLoader.DEFAULT_XML_OPTIONS).getChartSpace(); 
CTChart ctc = chartSpace.getChart();

//XSSFClientAnchor origAnch = chart.getGraphicFrame().getAnchor();
//Frame is null ! I tried to get anchor using the following:
CTMarker from = ((XSSFDrawing) 
srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0).getFrom();
CTMarker to = ((XSSFDrawing) 
srcChart.getParent()).getCTDrawing().getTwoCellAnchorArray(0).getTo();
XSSFClientAnchor anchor = new XSSFClientAnchor((int) from.getColOff(), 
(int) from.getRowOff(), (int) to.getColOff(), (int) to.getRowOff(), 
from.getCol(), from.getRow(), to.getCol(), to.getRow());
anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
   //Create chart in the destination workbook
XSSFChart destChart = destDR.createChart(anchor);

//A new XSSFChart constructor is needed maybe. The following 2 
lines use reflection in order to force values for these 2 private variables of 
XSSFChart
Utilities.setVariable(destChart, "chartSpace", chartSpace, true);
Utilities.setVariable(destChart, "chart", ctc, true);
  }
}

//Write the new workbook (could also be one you already have opened for 
edition)
FileOutputStream out = new FileOutputStream("c:/temp/test.xlsx");
destWB.write(out);
out.flush();
out.close();
in.close();
srcWB.close();
destWB.close();



-
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org



Re: Copying chart

2018-08-23 Thread Alain FAGOT BÉAREZ
Hi FD,

Did you base your patch on 3.17 sources or on current GitHub mirror?

Cloning a Chart in XSLF was not only a matter of cloning some XML hierarchy. 
Some magic was involved at the relationships level. Maybe with the introduction 
of the XDDF package some parts of the magic have been made available.

I must confess I had the same motivation as yourself, but with Charts in 
slides. However I didn't have time and a project to explore the best way to do 
it also for Charts in XSSF and XWPF.

Your code is welcome! Even better if you could open a pull request on 
https://github.com/apache/poi as it is easier for us to review and bring it to 
our machines and run local tests.

Thanks for your collaboration!

Best regards,
Alain FAGOT BÉAREZ



⁣Gesendet mit BlueMail ​


 Originale Nachricht 
Von: "monnomiznog...@gmail.com" 
Gesendet: Thu Aug 23 18:29:09 GMT-03:00 2018
An: dev@poi.apache.org
Betreff: Copying chart

Guys,

You cannot ask even from advanced developers to create a chart from scratch 
with java code or any other code type.

Copying a chart from an existing model Excel file is needed in any real life 
reporting project.

I've tinkered with the code and found a simple way to do it. I just needed 
access to 2 private variables in XSSFChart class.

If I paste the test code could you implement the copy chart method somewhere in 
the library ?

Re
FD

-
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org


Re: Copying chart

2018-08-23 Thread Dave Fisher
Hi -

You should create a Bugzilla and attach your patch.

Regards,
Dave

> On Aug 23, 2018, at 2:29 PM, monnomiznog...@gmail.com wrote:
> 
> Guys,
> 
> You cannot ask even from advanced developers to create a chart from scratch 
> with java code or any other code type.
> 
> Copying a chart from an existing model Excel file is needed in any real life 
> reporting project.
> 
> I've tinkered with the code and found a simple way to do it. I just needed 
> access to 2 private variables in XSSFChart class.
> 
> If I paste the test code could you implement the copy chart method somewhere 
> in the library ?
> 
> Re
> FD
> 
> -
> To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
> For additional commands, e-mail: dev-h...@poi.apache.org
> 



signature.asc
Description: Message signed with OpenPGP