QXlsx icon indicating copy to clipboard operation
QXlsx copied to clipboard

Charts do not work properly

Open edosad opened this issue 7 years ago • 9 comments

Hi j2doll!

I wrote a script to extract the data from the .txt file into .xslx and to draw the graphs. The script is that every new data set is extracted to the new sheet in an already existed .xslx file. The issue is that every time the script is adding the new sheet with the new data, the format of the previously created graphs appears to be lost. How it can be changed?

Thank you very much!!

https://app.leanboard.io/board/f7640887-b7e9-4762-b9ad-1dc99e6bba50

edosad avatar Sep 23 '18 20:09 edosad

Dear edosad. Could you show me the script you wrote? I do not know the exact meaning you said.

j2doll avatar Sep 24 '18 10:09 j2doll

Dear Jay Two,

Thank you for a quick reply! Please see the the script and the example of the data attached with this email. The point of our script is to extract the data from a .txt file and to present it in a convenient way in .xslx document and to create a chart based on this data. The script is creating a new sheet with the data in an already existed .xslx document. However, with every new script run the format of the charts and graphs appears to be lost, so I can't see no legend or axes titles. This is the problem.

Also I wanted to ask is there any other method to check the existence of any file, such as File::exists ()

Thank you very much for you help!

Kind regards,

пн, 24 сент. 2018 г. в 13:17, Jay Two [email protected]:

Dear edosad. Could you show me the script you wrote? I do not know the exact meaning you said.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/j2doll/QXlsx/issues/11#issuecomment-423930067, or mute the thread https://github.com/notifications/unsubscribe-auth/ApggA_kphXYxktOrlCNh7Iff1neH3oJVks5ueLEfgaJpZM4W12zR .

edosad avatar Sep 24 '18 18:09 edosad

Dear edosad.

:two: You can check the existence of the file as follows. See https://forum.qt.io/post/352670.

// [static] bool QFile::exists(const QString &fileName)
qDebug() << QFile::exists("/home/pw/docs/file.txt");

j2doll avatar Sep 25 '18 10:09 j2doll

From @edosad

Dear Jay Two,

Thanks for looking into this. I'm afraid I failed to explain the problem properly. So I create a chart, then I open it in the xslx file and format the chart - add axes title,chart title,legend etc. Then I use the script to add more charts to the very same xslx file. And then I found that all of the changes I made (axes titles,legend etc) are gone because of the addition of the new data to the same file. My question is how it can be overcome? In other words how I can keep all of the changes while adding new sheets using the script to the same file?

void sat_calc::generate_report(){

    double d1, d2, d3;

    QFile input(chromatogram);

    if(!report_file.contains(".xlsx",Qt::CaseInsensitive))
        report_file+=".xlsx";

    if (!input.open(QIODevice::ReadOnly)){
        std::cout<<"\nNo such file or directory: "<<chromatogram.toStdString()<<"!\n\n";
        //system("pause");
        //exit(1);

    }
    else{

        QXlsx::Document output(report_file);

        output.addSheet(sheet_name);
        output.selectSheet(sheet_name);
        QString chrom_data_array="A"+QString::number(output_line_count+3);


        unsigned int input_line_count=0;

        int step=1;

        QString string_buffer, trash;
        QTextStream stream_buffer(&input);

        QXlsx::Format row_data, simple_green, title_stile;
        row_data.setBorderStyle(QXlsx::Format::BorderThin);
        row_data.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
        row_data.setNumberFormat("0.000");
        simple_green.setFontColor(QColor(Qt::green));
        simple_green.setBorderStyle(QXlsx::Format::BorderThin);
        title_stile.setFontBold(true);
        title_stile.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
        title_stile.setVerticalAlignment(QXlsx::Format::AlignVCenter);
        title_stile.setPatternBackgroundColor(QColor(225,225,225));
        title_stile.setBorderStyle(QXlsx::Format::BorderMedium);
        output.write(output_line_count+2,1,"volume, ml",title_stile);
        output.write(output_line_count+2,2,"OD, mAu",title_stile);

            while (!stream_buffer.atEnd()) {
                if(input_line_count%step==0){
                    for(int j=0; j<2; j++){
                        if(input_line_count>2){
                            if(input_line_count==2&&j==0){
                                stream_buffer>>string_buffer;

                            }
                            if(input_line_count==3&&j==0){
                                stream_buffer>>d1;
                                output.write(output_line_count,j+1,d1,simple_green);
                            }
                            if(input_line_count==4&&j==0){
                                stream_buffer>>d2;
                                step=(int)(precision/(d2-d1));
                                std:: cout<<"\nstep  "<<step<<endl;
                                output.write(output_line_count,j+1,d2,simple_green);
                            }
                            if(j||input_line_count>4){
                                stream_buffer>>d3;
                                output.write(output_line_count,j+1,d3,row_data);
                            }
                        }

                }
                output_line_count++;
            }
            trash=stream_buffer.readLine();
            input_line_count++;
            }

            chrom_data_array+=":B"+QString::number(output_line_count-1);
            Chart * Crom = output.insertChart(3, 5, QSize(600, 500));
            Crom->setChartType(Chart::CT_Scatter);
            Crom->addSeries(CellRange(chrom_data_array));

            output.saveAs(report_file);
            input.close();
    }

       std:: cout<<"end"<<std::endl;
}

j2doll avatar Oct 02 '18 00:10 j2doll

Dear @edosad

  • I have implemented some function that you want. v1.3.14

    • saving axis title of line/bar/scatter chart.
    • loading function is not supported, yet.
  • See chartsquestions example that is based on your example.

// sat_calc.cpp
// ...
    Chart * Crom = output.insertChart( 3, 5, QSize(600, 500) );
    Crom->setChartType( Chart::CT_Scatter );
    Crom->addSeries( CellRange(chrom_data_array) );
    Crom->setAxisTitle( Chart::ChartAxisPos::Left, QString("left title") ); // dev22
    Crom->setAxisTitle( Chart::ChartAxisPos::Bottom, QString("bottom title") ); // dev22
// ...

Try the test.

2018-12-20-18_34_37

j2doll avatar Dec 20 '18 09:12 j2doll

  • The function to set the title of the chart has been added. v.1.3.16
 Chart * Crom = output.insertChart( 3, 5, QSize(600, 500) );
 Crom->setChartType( Chart::CT_Scatter );
 Crom->addSeries( CellRange(chrom_data_array) );
 Crom->setAxisTitle( Chart::Left, QString("left title") ); 
 Crom->setAxisTitle( Chart::Bottom, QString("bottom title") );
 Crom->setChartTitle( QString("hello chart") ); 

j2doll avatar Dec 27 '18 03:12 j2doll

Dear Jay Two,

Thank you very much for your help and reply and apologies for the late answer. We have tested the new features,it's great,thanks a lot for adding them! However, the problem I was referring to earlier still remains: addition of a new chart into the file with the chart that has been already modified cancels all of the previous changes. Just try to add one chart,modify it and then add another one,you will see. It would be very helpful if you could also look into it, when you'd have time of course. Also just a thought,would it be possible to add other tools for chart design? Such as selection of chart type,curves colour etc.

Thank you again very much,it's a great tool that helped us in many ways.

Kind wishes,

On 27 Dec 2018, at 06:10, Jay Two [email protected] wrote:

The function to set the title of the chart has been added. v.1.3.16 Chart * Crom = output.insertChart( 3, 5, QSize(600, 500) ); Crom->setChartType( Chart::CT_Scatter ); Crom->addSeries( CellRange(chrom_data_array) ); Crom->setAxisTitle( Chart::Left, QString("left title") ); Crom->setAxisTitle( Chart::Bottom, QString("bottom title") ); Crom->setChartTitle( QString("hello chart") ); — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

edosad avatar Jan 10 '19 04:01 edosad

Dear @edosad

Thank you for reporting issue.

It looks like I will need to re-check the chart's architecture of previous project.

❕ Notice: The URL of the project has changed. https://github.com/QtExcel/QXlsx

j2doll avatar Jan 10 '19 07:01 j2doll

Dear Jay Two, Thank you very much for your help and reply and apologies for the late answer. We have tested the new features,it's great,thanks a lot for adding them! However, the problem I was referring to earlier still remains: addition of a new chart into the file with the chart that has been already modified cancels all of the previous changes. Just try to add one chart,modify it and then add another one,you will see. It would be very helpful if you could also look into it, when you'd have time of course. Also just a thought,would it be possible to add other tools for chart design? Such as selection of chart type,curves colour etc. Thank you again very much,it's a great tool that helped us in many ways. Kind wishes, On 27 Dec 2018, at 06:10, Jay Two @.***> wrote: The function to set the title of the chart has been added. v.1.3.16 Chart * Crom = output.insertChart( 3, 5, QSize(600, 500) ); Crom->setChartType( Chart::CT_Scatter ); Crom->addSeries( CellRange(chrom_data_array) ); Crom->setAxisTitle( Chart::Left, QString("left title") ); Crom->setAxisTitle( Chart::Bottom, QString("bottom title") ); Crom->setChartTitle( QString("hello chart") ); — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Dear @edosad Can I see the test code for the case you mentioned? Is this the following code type?

int test( QVector<QVariant> params )
{
    qDebug() << " current path : " << QDir::currentPath();

    using namespace QXlsx;

    Document output;

    QString strSheet = "c-sheet1";
    output.addSheet(strSheet);
    output.selectSheet(strSheet);

    output.write( 1, 1, QVariant(11) );
    output.write( 1, 2, QVariant(12) );
    output.write( 1, 3, QVariant(13) );
    output.write( 2, 1, QVariant(21) );
    output.write( 2, 2, QVariant(22) );
    output.write( 2, 3, QVariant(23) );

    // [1] create chart 1
    Chart* ptrChart = output.insertChart( 3, 5, QSize(600, 500) );
    ptrChart->setChartType( Chart::CT_ScatterChart );
    ptrChart->addSeries( CellRange("A3:B3") );
    ptrChart->setAxisTitle( Chart::Left, QString("left title") );
    ptrChart->setAxisTitle( Chart::Bottom, QString("bottom title") );
    ptrChart->setChartTitle( QString("hello chart") );

    // [2] create chart 2
    Chart* ptrChart2 = output.insertChart( 3, 15, QSize(600, 500) );
    ptrChart2->setChartType( Chart::CT_ScatterChart );
    ptrChart2->addSeries( CellRange("A1:B3") );
    ptrChart2->setAxisTitle( Chart::Left, QString("left title") );
    ptrChart2->setAxisTitle( Chart::Bottom, QString("bottom title") );
    ptrChart2->setChartTitle( QString("hello chart") );

    // [3] change data of chart 1
    ptrChart->addSeries( CellRange("A1:B3") );
    ptrChart->setAxisTitle( Chart::Left, QString("new left title") );
    ptrChart->setAxisTitle( Chart::Bottom, QString("new bottom title") );
    ptrChart->setChartTitle( QString("new hello chart") );

    if ( !output.saveAs("test2.xlsx") )
    {
        qDebug() << "Failed to save xlsx file.";
    }

    return 0;
}

j2doll avatar Feb 21 '19 02:02 j2doll