PhpSpreadsheet和Excel图表,Y轴位置错误

时间:2019-01-29 14:01:14

标签: excel charts phpspreadsheet

我正在尝试使用PhpSpreadsheet在Excel中创建单个系列条形图或折线图。我无法在正确的位置获得Y轴。无论是折线图还是条形图,Y轴都会显示在图表中的任意位置。这是我的代码,主要来自示例代码。

    //  Set the Labels for each data series we want to plot
    $label=$sheetname."!\$H\$1";
    $dataSeriesLabels = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $label, null, 1),
    ];

    //  Set the X-Axis Labels
    $xrange=$sheetname."!\$C\$2:\$C\$".$lastrow;
    $xAxisTickValues = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $xrange, null, $lastrow),
    ];

    //  Set the Data values for each data series we want to plot
    $data=$sheetname."!\$H\$2:\$H\$".$lastrow;
    $dataSeriesValues = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $data, null, $lastrow),
    ];

    //  Build the dataseries
    $series = new DataSeries(
        DataSeries::TYPE_BARCHART,   // plotType
        DataSeries::GROUPING_CLUSTERED,  // plotGrouping
        range(0, count($dataSeriesValues) - 1),     // plotOrder
        $dataSeriesLabels,                // plotLabel
        $xAxisTickValues,               // plotCategory
        $dataSeriesValues               // plotValues
    );

    //  Set the series in the plot area
    $plotArea = new PlotArea(null, array($series));

    //  Set the chart legend
    $legend = new Legend(Legend::POSITION_RIGHT, null, false);
    $title = new Title('Labor');
    $xAxisLabel = new Title('Part Number');
    $yAxisLabel = new Title('Minutes');

    //  Create the chart
    $chart = new Chart(
        'chart1',   // name
        $title,     // title
        $legend,    // legend
        $plotArea,    // plotArea
        true,     // plotVisibleOnly
        0,        // displayBlanksAs
        $xAxisLabel,     // xAxisLabel
        $yAxisLabel   // yAxisLabel
    );
    //  Set the position where the chart should appear in the worksheet
    $chart->setTopLeftPosition('A17');
    $chart->setBottomRightPosition('N34');

    //  Add the chart to the worksheet
    $sheet->addChart($chart);

上面的代码生成以下图表: Example

我对折线图也有同样的问题。如何获取Y轴值以移动到左侧的适当位置?

1 个答案:

答案 0 :(得分:0)

我在一些PHPExel文档中找到了答案。我刚刚添加了X和Y轴样式。

    $yaxis = new Axis();
    $xaxis = new Axis();
    $yaxis->setAxisOptionsProperties('low', null, null, null, null, null, -20, 20, null, null);
    $yaxis->setLineParameters('FFFFFF',100,Axis::EXCEL_COLOR_TYPE_ARGB);
    $xaxis->setAxisOptionsProperties('low', null, null, null, null, null, 0, 0, null, null);

然后将值添加到我的图表中

    //  Create the chart
    $chart = new Chart(
        'chart1',   // name
        $title,     // title
        $legend,    // legend
        $plotArea,    // plotArea
        true,     // plotVisibleOnly
        0,        // displayBlanksAs
        $xAxisLabel,     // xAxisLabel
        $yAxisLabel,   // yAxisLabel
        $yaxis,
        $xaxis
    );

PHPExcel Documentation