PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.32k stars 3.45k forks source link

Can't create contour chart (surface 2d) #2931

Closed lvlukola closed 2 years ago

lvlukola commented 2 years ago

I'm trying to plot contour chart (surface 2d), but I'm getting surface 3d chart

The library can do it?

$dataSeriesLabels = [[
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['5-6']),
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['6-7']),
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['7-8']),
    ];

    $xAxisTickValues = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]),
    ];

    $dataSeriesValues = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [6, 6, 6, 6, 6, 6, 5.9, 6, 6]),
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [6, 6, 6, 6.5, 7, 7, 7, 7, 7]),
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, null, null, 9, [6, 6, 6, 7, 8, 8, 8, 8, 7.9]),
    ];

    $series = new DataSeries(
        DataSeries::TYPE_SURFACECHART,
        DataSeries::GROUPING_STANDARD,
        range(0, count($dataSeriesValues) - 1),
        $dataSeriesLabels,
        $xAxisTickValues,
        $dataSeriesValues,
        null, // plotDirection
        null, // smooth line
        DataSeries::STYLE_LINEMARKER  // plotStyle
    );

    $plotArea = new PlotArea(null, [$series]);
    $legend = new ChartLegend(ChartLegend::POSITION_BOTTOM, null, false);

    $title = new Title('График распредления температур в пределах кр');

    $chart = new Chart(
        'chart2',
        $title,
        $legend,
        $plotArea,
        true,
        DataSeries::EMPTY_AS_GAP,
    );

    $chart->setTopLeftPosition("A$1");
    $chart->setBottomRightPosition("P$20");

    $sheet->addChart($chart);
oleibman commented 2 years ago

You should set your grouping to null rather than DataSeries::GROUPING_STANDARD. Without that change, I was unable even to open the output file successfully. PhpSpreadsheet should probably be changed to not generate the grouping tag for surface charts.

That does not fix your problem. Apparently Excel requires that its view3D tag be populated even for non-3D surface charts. For now, you can accomplish that with the following code:

    $chart
        ->setRotX(90)
        ->setRotY(0)
        ->setRAngAx(0)
        ->setPerspective(0);

While I am looking into the problem from the first paragraph, I will also see if there's a way for PhpSpreadsheet to handle this automatically without the application having to add that statement.

lvlukola commented 2 years ago

Thanks. It's OK!