phpphpspreadsheetphpoffice

When i Try to reverse the Axis with the following code i do not obtain the reverse orientation


When I try to reverse the axis with the following code I do not obtain the reverse orientation

$chart1->getChartAxisX()->setAxisOrientation(Axis::ORIENTATION_REVERSED);

How to change the coordinate axis from small to large?

pic1

pic2


Solution

  • My guess is that you want the data sources to appear in the table in the same order as the bar chart, and your graph is a horizontal bar, so your code is fine, and it does invert the X-axis. But why doesn't it work, and so I looked at the source code. The code for getChartAxisX() in your method looks like this

        public function getChartAxisX()
        {
            if ($this->xAxis !== null) {
                return $this->xAxis;
            }
    
            return new Axis();
        }
    

    As you can see, when the member variable xAxis does not exist, this method creates a new object and returns it. But this new object is not set into the $chart object, so your set method does not work on the $chart object.

    However, I have found that Chart does not have a setXAxis() method in PhpSpreadsheet, so if you want it to work, you should set the Axis object in when you create the $chart object.

    $axisX = new Axis();
    $axisX->setAxisOrientation(Axis::ORIENTATION_REVERSED);
    $chart = new Chart(
        "chart", // name
        $title, // title
        $legend, // legend
        $plotArea, // plotArea
        true, // plotVisibleOnly
        DataSeries::EMPTY_AS_GAP, // displayBlanksAs null,
        $xAxisLabel, // xAxisLabel - Owen added null xAxisLabel
        $yAxisLabel, // yAxisLabel
        $axisX //xAxis
    );
    

    Now it should do what you want.