Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Используя C++ и LibreOffice 7.5.4 SDK как добавить график?

  1. User

    Используя C++ и LibreOffice 7.5.4 SDK как добавить график?

  2. Assistant

    Для добавления графика в документ LibreOffice, необходимо использовать API LibreOffice. Для этого нужно сначала создать новый документ, а затем добавить в него график. Пример кода на C++ для создания документа и добавления графика:

    #include <com/sun/star/bridge/XBridgeFactory.hpp>
    #include <com/sun/star/lang/XMultiComponentFactory.hpp>
    #include <com/sun/star/frame/XComponentLoader.hpp>
    #include <com/sun/star/graphic/XGraphicObject.hpp>
    #include <com/sun/star/awt/XControlModel.hpp>
    #include <com/sun/star/drawing/XShape.hpp>
    #include <com/sun/star/text/XText.hpp>
    #include <com/sun/star/text/XTextContent.hpp>
    #include <com/sun/star/chart2/XChartDocument.hpp>
    #include <com/sun/star/chart2/XChartData.hpp>
    #include <com/sun/star/chart2/XDiagram.hpp>
    #include <com/sun/star/chart2/data/XNumericalDataSource.hpp>
    #include <com/sun/star/beans/XPropertySet.hpp>
    #include <com/sun/star/table/XCell.hpp>
    #include <com/sun/star/table/XTable.hpp>
    #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
    
    using namespace com::sun::star;
    
    int main()
    {
        // create the component context
        uno::Reference< lang::XMultiComponentFactory > xMultiComponentFactory;
        uno::Reference< bridge::XBridgeFactory > xBridgeFactory;
        uno::Reference< frame::XComponentLoader > xComponentLoader;
        uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument;
        uno::Reference< table::XCell > xCell;
        uno::Reference< drawing::XShape > xShape;
        uno::Reference< text::XText > xText;
        uno::Reference< chart2::XChartDocument > xChartDocument;
        uno::Reference< chart2::data::XNumericalDataSource > xNumericalDataSource;
        uno::Reference< beans::XPropertySet > xPropertySet;
    
        try
        {
            uno::Reference< lang::XComponentContext > xComponentContext =
                ::cppu::defaultBootstrap_InitialComponentContext();
            xMultiComponentFactory = xComponentContext->getServiceManager();
            xBridgeFactory = uno::Reference< bridge::XBridgeFactory >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.bridge.BridgeFactory",xComponentContext),
                uno::UNO_QUERY);
            xComponentLoader = uno::Reference< frame::XComponentLoader >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.frame.Desktop",xComponentContext),
                uno::UNO_QUERY);
    
            uno::Reference< beans::XPropertySet > xLoadProperties(xMultiComponentFactory->createInstanceWithContext(
                "com.sun.star.beans.PropertyValue",xComponentContext), uno::UNO_QUERY);
    
            xLoadProperties->setPropertyValue("Hidden", uno::makeAny(true));
            uno::Reference< XInterface > xInterface(xComponentLoader->loadComponentFromURL(
                        "private:factory/scalc",
                        "_default",
                        0,
                        xLoadProperties),
                uno::UNO_QUERY_THROW);
    
            xSpreadsheetDocument = uno::Reference< sheet::XSpreadsheetDocument >(xInterface, uno::UNO_QUERY);
    
            // get the first sheet of the document and add data
            uno::Reference< table::XTable > xSheetTable = xSpreadsheetDocument->getSheets()->getByIndex(0);
            xCell = xSheetTable->getCellByPosition(0,0);
            xCell->setValue(uno::makeAny(1));
            xCell = xSheetTable->getCellByPosition(0,1);
            xCell->setValue(uno::makeAny(2));
            xCell = xSheetTable->getCellByPosition(0,2);
            xCell->setValue(uno::makeAny(3));
    
            // create chart
            xChartDocument = uno::Reference< chart2::XChartDocument >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.chart2.ChartDocument",xComponentContext),
                uno::UNO_QUERY);
            xNumericalDataSource = uno::Reference< chart2::data::XNumericalDataSource >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.chart2.data.DataSource",xComponentContext),
                uno::UNO_QUERY);
            xNumericalDataSource->setDimensions(2, 3);
            xNumericalDataSource->setValues(
                uno::Sequence< uno::Sequence<double> > {
                    uno::Sequence<double> { 1, 2, 3 },
                    uno::Sequence<double> { 10, 20, 30 }
                }
            );
            chart2::ChartDataChangeEventType aType = chart2::ChartDataChangeEventType_DATA;
    
            uno::Reference< chart2::XDiagram > xChartDiagram = xChartDocument->createInstance("com.sun.star.chart2.LineDiagram");
            uno::Reference< chart2::XChartData > xChartData(xChartDiagram, uno::UNO_QUERY);
            xChartData->setDataSource(xNumericalDataSource, aType);
    
            xShape = uno::Reference< drawing::XShape >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.drawing.GraphicObjectShape",xComponentContext),
                uno::UNO_QUERY);
            xShape->setSize(::com::sun::star::awt::Size{30000,20000});
    
            xShape->setPosition(::com::sun::star::awt::Point{1500,1000});
    
            xPropertySet = uno::Reference< beans::XPropertySet >(xShape, uno::UNO_QUERY);
            xPropertySet->setPropertyValue("GraphicURL", uno::makeAny("vnd.sun.star.GraphicObject:Chart"));
            uno::Reference< graphic::XGraphicObject > xChartGraphic(xShape, uno::UNO_QUERY);
            xChartGraphic->setGraphic(xChartDiagram);
    
            xSpreadsheetDocument->getSheets()->getByIndex(0)->add(xShape);
    
            // save document
            uno::Reference< lang::XComponent > xComponent(xSpreadsheetDocument, uno::UNO_QUERY);
            uno::Reference< frame::XStorable > xStorable(xComponent, uno::UNO_QUERY);
            uno::Sequence< beans::PropertyValue > aProps(0);
            xStorable->storeToURL("file:///tmp/test.ods", aProps);
        }
        catch (uno::Exception& e)
        {
            std::cerr << "Exception: " << e.Message << std::endl;
        }
    
        return 0;
    }
    

    Этот код создает новый документ Calc и добавляет на первом листе линейный график с данными из ячеек A1:A3 и B1:B3. Он сохраняет документ в файл /tmp/test.ods.

    Обратите внимание на то, что для сборки этого кода необходимо установить LibreOffice 7.5.4 SDK и настроить окружение с помощью переменных среды. Для сборки кода можно использовать компилятор g++:

    g++ -std=c++11 -o chart chart.cpp -I<LO_SDK_PATH>/sdk/include -I<LO_SDK_PATH>/include -L<LO_SDK_PATH>/sdk/lib -L<LO_SDK_PATH>/lib -luno_cppuhelpergcc3 -luno_cppu -luno_sal -luno_salhelpergcc3

    Замените <LO_SDK_PATH> на путь до папки LibreOffice SDK.

  3. User

    в 7.5.4 нет XNumericalDataSource

  4. Assistant

    Для создания графика в LibreOffice 7.5.4 необходимо использовать другой интерфейс данных - XDataProvider. Пример кода для создания графика с помощью XDataProvider:

    #include <com/sun/star/bridge/XBridgeFactory.hpp>
    #include <com/sun/star/lang/XMultiComponentFactory.hpp>
    #include <com/sun/star/frame/XComponentLoader.hpp>
    #include <com/sun/star/graphic/XGraphicObject.hpp>
    #include <com/sun/star/awt/XControlModel.hpp>
    #include <com/sun/star/drawing/XShape.hpp>
    #include <com/sun/star/text/XText.hpp>
    #include <com/sun/star/text/XTextContent.hpp>
    #include <com/sun/star/chart2/XChartDocument.hpp>
    #include <com/sun/star/chart2/XDiagram.hpp>
    #include <com/sun/star/chart2/XDataReceiver.hpp>
    #include <com/sun/star/beans/XPropertySet.hpp>
    #include <com/sun/star/table/XCell.hpp>
    #include <com/sun/star/table/XTable.hpp>
    #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
    #include <com/sun/star/chart2/data/XDataSource.hpp>
    #include <com/sun/star/chart2/XChartTypeTemplate.hpp>
     
    using namespace com::sun::star;
     
    int main()
    {
        // create the component context
        uno::Reference< lang::XMultiComponentFactory > xMultiComponentFactory;
        uno::Reference< bridge::XBridgeFactory > xBridgeFactory;
        uno::Reference< frame::XComponentLoader > xComponentLoader;
        uno::Reference< sheet::XSpreadsheetDocument > xSpreadsheetDocument;
        uno::Reference< table::XCell > xCell;
        uno::Reference< drawing::XShape > xShape;
        uno::Reference< text::XText > xText;
        uno::Reference< chart2::XChartDocument > xChartDocument;
        uno::Reference< chart2::XDiagram > xChartDiagram;
     
        try
        {
            uno::Reference< lang::XComponentContext > xComponentContext =
                ::cppu::defaultBootstrap_InitialComponentContext();
            xMultiComponentFactory = xComponentContext->getServiceManager();
            xBridgeFactory = uno::Reference< bridge::XBridgeFactory >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.bridge.BridgeFactory",xComponentContext),
                uno::UNO_QUERY);
            xComponentLoader = uno::Reference< frame::XComponentLoader >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.frame.Desktop",xComponentContext),
                uno::UNO_QUERY);
     
            uno::Reference< beans::XPropertySet > xLoadProperties(xMultiComponentFactory->createInstanceWithContext(
                "com.sun.star.beans.PropertyValue",xComponentContext), uno::UNO_QUERY);
     
            xLoadProperties->setPropertyValue("Hidden", uno::makeAny(true));
            uno::Reference< XInterface > xInterface(xComponentLoader->loadComponentFromURL(
                        "private:factory/scalc",
                        "_default",
                        0,
                        xLoadProperties),
                uno::UNO_QUERY_THROW);
     
            xSpreadsheetDocument = uno::Reference< sheet::XSpreadsheetDocument >(xInterface, uno::UNO_QUERY);
     
            // get the first sheet of the document and add data
            uno::Reference< table::XTable > xSheetTable = xSpreadsheetDocument->getSheets()->getByIndex(0);
            xCell = xSheetTable->getCellByPosition(0,0);
            xCell->setValue(uno::makeAny(1));
            xCell = xSheetTable->getCellByPosition(0,1);
            xCell->setValue(uno::makeAny(2));
            xCell = xSheetTable->getCellByPosition(0,2);
            xCell->setValue(uno::makeAny(3));
    
            // create chart
            xChartDocument = uno::Reference< chart2::XChartDocument >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.chart2.ChartDocument",xComponentContext),
                uno::UNO_QUERY);
     
            xChartDiagram = xChartDocument->createInstance("com.sun.star.chart2.LineDiagram");
     
            uno::Reference< chart2::XDataProvider > xDataProvider(xChartDocument->getDataProvider(), uno::UNO_QUERY);
     
            uno::Reference< chart2::XDataReceiver > xDataReceiver(xChartDiagram, uno::UNO_QUERY);
            xDataProvider->attachDataReceiver(xDataReceiver);
     
            // specify data source
            xDataReceiver->setReceivingDataFromSource(true);
            uno::Reference< chart2::data::XDataSource > xDataSource(xDataProvider, uno::UNO_QUERY);
            uno::Reference< chart2::XDataSink > xDataSink(xChartDocument, uno::UNO_QUERY);
            xDataSink->setDataSource(xDataSource);
     
            // set chart type
            uno::Reference< chart2::XChartTypeTemplate > xChartTypeTemplate(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.chart2.template.Line",xComponentContext),
                uno::UNO_QUERY);
            uno::Reference< beans::XPropertySet > xChartProperties(xChartTypeTemplate, uno::UNO_QUERY);
            xChartProperties->setPropertyValue("HasLegend", uno::makeAny(true));
            xChartDiagram = xDataSink->createInstance(xChartTypeTemplate);
            xDataProvider->start();
     
            xShape = uno::Reference< drawing::XShape >(xMultiComponentFactory->createInstanceWithContext(
                    "com.sun.star.drawing.GraphicObjectShape",xComponentContext),
                uno::UNO_QUERY);
            xShape->setSize(::com::sun::star::awt::Size{30000,20000});
     
            xShape->setPosition(::com::sun::star::awt::Point{1500,1000});
     
            uno::Reference< beans::XPropertySet > xPropertySet(xShape, uno::UNO_QUERY);
            xPropertySet->setPropertyValue("GraphicURL", uno::makeAny("vnd.sun.star.GraphicObject:Chart"));
            xShape->setPropertyValue("Model", uno::makeAny(xChartDiagram));
    
            xSpreadsheetDocument->getSheets()->getByIndex(0)->add(xShape);
     
            // save document
            uno::Reference< lang::XComponent > xComponent(xSpreadsheetDocument, uno::UNO_QUERY);
            uno::Reference< frame::XStorable > xStorable(xComponent, uno::UNO_QUERY);
            uno::Sequence< beans::PropertyValue > aProps(0);
            xStorable->storeToURL("file:///tmp/test.ods", aProps);
        }
        catch (uno::Exception& e)
        {
            std::cerr << "Exception: " << e.Message << std::endl;
        }
     
        return 0;
    }
    

    Этот код создает новый документ Calc и добавляет на первом листе линейный график с данными из ячеек A1:A3 и B1:B3. Он сохраняет документ в файл /tmp/test.ods.

    Обратите внимание, что для сборки этого кода необходимо установить LibreOffice 7.5.4 SDK и настроить окружение с помощью переменных среды. Для сборки кода можно использовать компилятор g++:

    g++ -std=c++11 -o chart chart.cpp -I<LO_SDK_PATH>/sdk/include -I<LO_SDK_PATH>/include -L<LO_SDK_PATH>/sdk/lib -L<LO_SDK_PATH>/lib -luno_cppuhelpergcc3 -luno_cppu -luno_sal -luno_salhelpergcc3
    

    Замените <LO_SDK_PATH> на путь до папки LibreOffice SDK.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.