How to create an object of type ::com::sun::star::chart2::data::XLabeledDataSequence ?

Jan Rheinländer jrheinlaender at gmx.de
Sat May 23 08:24:02 PDT 2015


Hi,

thanks for the hints.

Finally I managed to get my object from the ServiceManager. Here is the
complete code for inserting an (empty) XY-Chart into a Writer document:

Reference < XTextContent > insertChart(const Reference < XController >
&xCtrl, const Reference < XComponentContext > &xCC) {
  Reference < XTextCursor > xModelCursor;
  Reference < XText > xDocumentText = getDocumentText(xCtrl, xModelCursor);
  Reference < XTextContent > xChart = createObject(xCtrl, CLSID_CHART);

  // Insert the chart
  // TODO: This does not replace a selection with the chart (because we
are accessing the model cursor to get xRange?)
  // But do we want this? The selection might be the iFormula we are
basing the chart on!
  Reference < XTextRange > xRange(xModelCursor, UNO_QUERY_THROW);
  xDocumentText->insertTextContent(xRange, xChart, true);

  // Get a factory to create objects for this chart
  Reference < XEmbeddedObjectSupplier > xEOS(xChart, UNO_QUERY_THROW);
  Reference < com::sun::star::chart::XChartDocument >
cDoc(xEOS->getEmbeddedObject(), UNO_QUERY_THROW);
  Reference < XMultiServiceFactory > cDocMSF(cDoc, UNO_QUERY_THROW);

  // Make sure that we have a clear interpretation of where data series are
  Reference < XPropertySet > cDocProps (cDoc, UNO_QUERY_THROW);
  cDocProps->setPropertyValue(OU("DataRowSource"),
makeAny(::com::sun::star::chart::ChartDataRowSource_ROWS));

  // Create a new XYDiagram
  Reference < XDiagram >
xyDiagram(cDocMSF->createInstance(OU("com.sun.star.chart.XYDiagram")),
UNO_QUERY_THROW);
  cDoc->setDiagram(xyDiagram);

  // Create two new sequences based on the two first default sequences,
setting the roles correctly for an XYDiagram
  Reference < ::com::sun::star::chart2::XChartDocument >
chart(xEOS->getEmbeddedObject(), UNO_QUERY_THROW);
  chart->createInternalDataProvider(true);
  Reference< XDataProvider > dataProvider = chart->getDataProvider();
    Reference< XDataSequence > seqDataX =
dataProvider->createDataSequenceByRangeRepresentation(OU("0"));
    Reference < XPropertySet > seqProps (seqDataX, UNO_QUERY_THROW);
    seqProps->setPropertyValue(OU("Role"), makeAny(OU("values-x")));
    Reference< XDataSequence > seqLabelX =
dataProvider->createDataSequenceByRangeRepresentation(OU("label 0"));
    Reference< XDataSequence > seqDataY =
dataProvider->createDataSequenceByRangeRepresentation(OU("1"));
    seqProps = Reference< XPropertySet >(seqDataY, UNO_QUERY_THROW);
    seqProps->setPropertyValue(OU("Role"), makeAny(OU("values-y")));
    Reference< XDataSequence > seqLabelY =
dataProvider->createDataSequenceByRangeRepresentation(OU("label 1"));

    // Get write access to the chart series
    Reference< ::com::sun::star::chart2::XDiagram > diagram =
chart->getFirstDiagram();
    Reference< XCoordinateSystemContainer > xCoordCnt(diagram,
UNO_QUERY_THROW);
    Reference< XChartTypeContainer >
xChartTypeCnt(xCoordCnt->getCoordinateSystems()[0], UNO_QUERY_THROW);
    Reference< XDataSeriesContainer >
chartType(xChartTypeCnt->getChartTypes()[0], UNO_QUERY_THROW);
   
    // Create two sequences
  Reference< XMultiComponentFactory >
xServiceManager(xCC->getServiceManager()); // get the service manager
(the document service factory cannot create a LabeledDataSequence!)
    Sequence< Reference< XLabeledDataSequence > > sequences(2);
    sequences[0] = Reference< XLabeledDataSequence >
(xServiceManager->createInstanceWithContext(OU("com.sun.star.chart2.data.LabeledDataSequence"),
xCC), UNO_QUERY_THROW);
    sequences[1] = Reference< XLabeledDataSequence >
(xServiceManager->createInstanceWithContext(OU("com.sun.star.chart2.data.LabeledDataSequence"),
xCC), UNO_QUERY_THROW);
    sequences[0]->setValues(seqDataX);
    sequences[0]->setLabel(seqLabelX);
    sequences[1]->setValues(seqDataY);
    sequences[1]->setLabel(seqLabelY);

  // Set the data sequences into the chart
  Reference< XDataSink >
XYSink(xServiceManager->createInstanceWithContext(OU("com.sun.star.chart2.DataSeries"),
xCC), UNO_QUERY_THROW);
  XYSink->setData(sequences);
  Sequence< Reference< XDataSeries > > XYSeries(1);
  XYSeries[0] = Reference< XDataSeries > (XYSink, UNO_QUERY_THROW);
  chartType->setDataSeries(XYSeries);

  return xChart;
} // insertChart()



More information about the LibreOffice mailing list