Search code examples
phpfontsphpspreadsheet

how can i make my label values with BOLD style in a 3D bar chart in phpspreadsheet


Hello i was able to create a clustered 3D bar chart using PHPspreadsheet, version 1.23

However i want my label values, for each each column, to become more visible, by stylizing them to become BOLD. However after several tries, nothing worked here is my code for my chart


        $worksheet = $this->spreadsheet->getActiveSheet();

        // Set the Labels for each data series we want to plot
        //     Datatype
        //     Cell reference for data
        //     Format Code
        //     Number of datapoints in series
        //     Data values
        //     Data Marker
        $dataSeriesLabelsTotals = [
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$H$30', null, 1),   // Quantity OK
        //    new DataSeriesValues(),
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$J$30', null, 1),     // Quantity NOK
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$L$30', null, 1),     // Quantity Re-work
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$R$30', null, 1),     // Quantity total
        ];
        // Set the X-Axis Labels
        //     Datatype
        //     Cell reference for data
        //     Format Code
        //     Number of datapoints in series
        //     Data values
        //     Data Marker
        $xAxisTickValuesTotals = [
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$20:$A$21', null, 1), // Labels X
        ];
        // Set the Data values for each data series we want to plot
        //     Datatype
        //     Cell reference for data
        //     Format Code
        //     Number of datapoints in series
        //     Data values
        //     Data Marker
        $dataSeriesValuesTotals = [
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$H$' . ($cursor + 1) . ':$I$'. ($cursor + 1) , null, 1), 
            // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$Z$3' , null, 1),        // Quantity OK
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$J$' . ($cursor + 1) . ':$K$'. ($cursor + 1) , null, 1),
            // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$Z$3' , null, 1),        // Quantity NOK
            new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$L$' . ($cursor + 1) . ':$M$'. ($cursor + 1) , null, 1),
            // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$Z$3' , null, 1),        // Quantity Re-work
          new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$R$' . ($cursor + 1) . ':$S$'. ($cursor + 1) , null, 1),      // Quantity total 
        ];

        // Build the dataseries
        $series1 = new DataSeries(
            DataSeries::TYPE_BARCHART_3D, // plotType
            DataSeries::GROUPING_CLUSTERED, // plotGrouping
            range(0, count($dataSeriesValuesTotals) - 1), // plotOrder
            $dataSeriesLabelsTotals, // plotLabel
            $xAxisTickValuesTotals, // plotCategory
            $dataSeriesValuesTotals // plotValues
        );
    
        // Set additional dataseries parameters
        //     Make it a vertical column rather than a horizontal bar graph
        $series1->setPlotDirection(DataSeries::DIRECTION_VERTICAL);



        // Set labels in chart columns
        $layoutTotals = new Layout();
        $layoutTotals->setShowVal(true);

       
      

        // Set the series in the plot area
        $plotAreaTotals = new PlotArea($layoutTotals, [$series1]);
   
        // Set the chart legend
        $legendTotals = new ChartLegend(ChartLegend::POSITION_LEFT, null, false);

        

        $titleTotals = new Title('TOTALS');


        // Create the chart
        $chartTotals = new Chart(
            'Chart Totals', // name
            $titleTotals, // title
            $legendTotals, // legend
            $plotAreaTotals, // plotArea
            false, // plotVisibleOnly
            DATASERIES::EMPTY_AS_GAP, // displayBlanksAs
            null, // xAxisLabel
            null,
              // yAxisLabel
         
           
        );

    

    
        // Set the position where the chart should appear in the worksheet
        $chartTotals->setTopLeftPosition('B' . ($cursor + 3));
        $chartTotals->setBottomRightPosition('M' . ($cursor + 20));

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

And here is how my chart is looking . As you can see the 2 in the green column has less visibility and i want to make the value Bold

My 3D bar chart

Thank You


Solution

  • Didn't found out how to chance the style of the value of the Data label, however i managed to solve my problem doing a work around, by changing the color of the values. To do that, i simple created a new ChartColor object with a specific color and then called the SetLabelFontColor method to change the values color, like this

    $colors= New ChartColor("0D0078");
           
    
     
    
            // Set labels in chart columns
            $layoutTotals = new Layout();
    
            $layoutTotals->setShowVal(true);
            $layoutTotals->setLabelFontColor($colors);