PHPOffice / PHPPresentation

A pure PHP library for reading and writing presentations documents
https://phpoffice.github.io/PHPPresentation
Other
1.3k stars 519 forks source link

How to place several chart types to one plot area (with dirty solution to rework) #672

Open pal-software opened 3 years ago

pal-software commented 3 years ago

I use this solution when I need to place several charts to one plot area such as:

Screenshot 2021-08-23 011215

here placed 3 charts:

  1. stacked chart (stage 1 + stage 2 + stage 3)
  2. line graph (dark red line with value eq 7)
  3. transparent graph, which mark every stacked bar (values 12, 16, 14 at the top of bars).

so... I change 2 files (attached as zip-archive):

  1. PhpOffice\PhpPresentation\src\PhpPresentation\Shape\Chart\PlotArea.php
  2. PhpOffice\PhpPresentation\src\PhpPresentation\Writer\PowerPoint2007\PptCharts.php

You can see changes such as they marked by comments with word 'PAL'.

changed_files.zip

and now I can make this:

    /**
     * Make slide with monitoring information
     *
     * @param mixed $data    Monitoring data
     * @param mixed $slide    Slide 
     *
     * @return void
     */
    private function _makeSlideMonitoring(
       $data,
       $slide
    ) {

        $barChart = new Bar();
        // ---
        // make bar chart here ..... (make series, add series to barChart)
        //
        //    ......
        //
        $barChart->setBarGrouping(Bar::GROUPING_STACKED);
        // ---

        $targetChart = new \PhpOffice\PhpPresentation\Shape\Chart\Type\Line();
        // ---
        // make target chart for example:
        //
        $targetSeria = [ 
           'date 1' => 7, 
           'date 2' => 7, 
           'date 3' => 7 
         ];
         $seria = new Series('Target level', $targetSeria);
         $outline = new Outline();

         $outline->getFill()
              ->setFillType(Fill::FILL_SOLID)
              ->setStartColor(new Color('FF800000'));
          $outline->setWidth(3);

          $seria->setOutline($outline);
          $seria->getFont()->setSize(12)->getColor()->setRGB('000000');
          $seria->getFont()->setBold(true);
          $seria->setLabelPosition(Series::LABEL_TOP);

          $targetChart->addSeries($seria);
          $targetChart->setBarGrouping(Bar::GROUPING_STACKED);
          //
          // ----

          $totalChart = new \PhpOffice\PhpPresentation\Shape\Chart\Type\Line();
          // ---
          // make some total chart here, for example:
          //
          $totalSeria = [
             'date 1' => 12, 
             'date 2' => 16, 
             'date 3' => 14
           ];

           $outline = new Outline();
           $outline->getFill()->setFillType(Fill::FILL_NONE);

           $seria = new Series('Total', $totalSeria);
           $seria->setOutline($outline);
           $seria->getFont()->setSize(13)->getColor()->setRGB('000000');
           $seria->setLabelPosition(Series::LABEL_TOP);
           $totalChart->addSeries($seria);
           $totalChart->setBarGrouping(Bar::GROUPING_STACKED);
           //
           // ----

            // make chart shape - area for view (here will be viewed: charts, legend, grids, titles)
            // ------
            $shape = $slide->createChartShape();
            $shape
                ->setResizeProportional(false)
                ->setOffsetX(Units::convert(10))
                ->setOffsetY(Units::convert(40))
                ->setWidth(Units::convert(280))
                ->setHeight(Units::convert(160));

            $shape->getTitle()->setText(' ');

            $shape
                ->getBorder()
                ->setLineStyle(Border::LINE_SINGLE);
           // ------

           // get & configure plot area - part of the chart shape where will be viewed charts, grids, axis
           // ----
            $area = $shape->getPlotArea();

            // add charts to plot area of chart shape
            //
            if ($barChart !== null) {
                $area->setType($barChart);
            }
            if ($totalChart !== null) {
                $area->setType($totalChart);
            }
            if ($targetChart !== null) {
                $area->setType($targetChart);
            }

            // grids
            $gridLines = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines();
            $gridLines->getOutline()->setWidth(1);
            $gridLines->getOutline()
                ->getFill()
                ->setFillType(Fill::FILL_SOLID)
                ->setStartColor(new Color('FFDDDDDD'));

            // format grids
            $area->getAxisX()->setTitle('Dates');
            $area->getAxisY()->setTitle('Time, s' );
            $area->getAxisY()->setMajorGridlines($gridLines);
            //
            // -------

            // legend
            $legend = $shape->getLegend();
            $legend->getBorder()->setLineStyle(Border::LINE_SINGLE);
            $legend->setPosition(
                \PhpOffice\PhpPresentation\Shape\Chart\Legend::POSITION_RIGHT
            );
    }

...but now method setType() of PlotArea will be add new type to chart & getType() of PlotArea will be get array ... but it was very quickly (& dirty) solution for my one project. :)

pal-software commented 3 years ago

... method Units::convert(mm) - convert millimeters to pixels here..

dormadekhin commented 2 years ago

any news on this topic?

pal-software commented 2 years ago

which news?...

dormadekhin commented 2 years ago

@pal-software, Is this already working on the master branch? or do I need to make these changes locally?

pal-software commented 2 years ago

@pal-software, Is this already working on the master branch? or do I need to make these changes locally?

It was only my personal decision. I don't about including this feature to this project

dormadekhin commented 2 years ago

@pal-software, why don't you want to include this solution in the project? I think this is a useful feature.

pal-software commented 2 years ago

@pal-software, why don't you want to include this solution in the project? I think this is a useful feature.

Maybe, but... I don't know how to do it.. If somebody will include this to proj, then it will be good.

dormadekhin commented 2 years ago

@pal-software, You can file these changes as a pull request

pal-software commented 2 years ago

Nu, eto esli vremya budet i pri etom budet ne slishkom lenno, i esche nado pro eto vspomnit'.... ;)

..vot kto by sam eto sdelal...

Progi1984 commented 3 weeks ago

@pal-software Hi, have you got a PowerPoint file with multiple charts, for analysis, please ?