$spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); // changed data to simulate a trend chart - Xaxis are dates; Yaxis are 3 meausurements from each date $worksheet->fromArray( [ ['', 'metric1', 'metric2', 'metric3'], ['=DATEVALUE("2021-01-01")', 12.1, 15.1, 21.1], ['=DATEVALUE("2021-04-01")', 56.2, 73.2, 86.2], ['=DATEVALUE("2021-07-01")', 52.2, 61.2, 69.2], ['=DATEVALUE("2021-10-01")', 30.2, 32.2, 24.2], ] ); $worksheet->getStyle('A2:A5')->getNumberFormat()->setFormatCode('mm/dd/yyyy'); $worksheet->getColumnDimension('A')->setAutoSize(true); $worksheet->setSelectedCells('A1'); // 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 (null) // Color (null) $dataSeriesLabels = [ new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // was 2010 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // was 2011 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // was 2012 ]; // Set the X-Axis Labels // changed from STRING to NUMBER // added 2 additional x-axis values associated with each of the 3 metrics // added FORMAT_CODE_NUMBER // Number of datapoints in series $xAxisTickValues = [ //new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4), new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4), new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', Properties::FORMAT_CODE_DATE, 4), ]; // 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 // Data Marker Color fill/[fill,Border] // Data Marker size // Color(s) added $dataSeriesValues = [ new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', Properties::FORMAT_CODE_NUMBER, 4, null, 'diamond', null, 7), new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', Properties::FORMAT_CODE_NUMBER, 4, null, 'square', 'accent1', 7), new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', Properties::FORMAT_CODE_NUMBER, 4, null, 'triangle', null, 7), ]; // series 1 // marker details $dataSeriesValues[0]->getMarkerColor1()->setColorProperties('0070C0',null,'srgbClr'); // diamond fill color $dataSeriesValues[0]->getMarkerColor2()->setColorProperties('002060',null,'srgbClr'); // diamond border color // line details - smooth line, connected $dataSeriesValues[0]->setScatterLines(TRUE) ->setSmoothLine(TRUE) ->setLineColorProperties('accent1',40,'schemeClr'); // value, alpha, type // line style special effects: dashed, head arrow, end arrow $dataSeriesValues[0]->setLineStyleProperties(2.5,'tri','sysDash','sq','miter','arrow',4,'triangle',6); // width compound dash cap join headtype headsize(med,sm) endtype endsize(med,lg) # series 2 - straight line - no special effects, connected, straight line $dataSeriesValues[1]->getMarkerColor1()->setColorProperties('accent6',3,'schemeClr'); // square fill $dataSeriesValues[1]->getMarkerColor2()->setColorProperties('0FFF00',null,'srgbClr'); // square border $dataSeriesValues[1]->setScatterLines(TRUE) ->setSmoothLine(FALSE) ->setLineColorProperties('FF0000',80,'srgbClr'); $dataSeriesValues[1]->setLineWidth(2.0); # series 3 - markers, no line $dataSeriesValues[2]->getMarkerColor1()->setColorProperties('FFFF00',null,'srgbClr'); // triangle fill $dataSeriesValues[2]->getMarkerColor2()->setColorProperties('accent4',null,'schemeClr'); // triangle border $dataSeriesValues[2]->setScatterLines(FALSE); // points not connected // set Xaxis to m/d/yyyy dates instead of Excel-equivalent-year1900-numbers $xAxis = new Axis(); $xAxis->setAxisNumberProperties(Properties::FORMAT_CODE_DATE ); // Build the dataseries $series = new DataSeries( DataSeries::TYPE_SCATTERCHART, // plotType null, // plotGrouping (Scatter charts don't have grouping) range(0, count($dataSeriesValues) - 1), // plotOrder $dataSeriesLabels, // plotLabel $xAxisTickValues, // plotCategory $dataSeriesValues, // plotValues null, // plotDirection null, // smooth line DataSeries::STYLE_SMOOTHMARKER // plotStyle //DataSeries::STYLE_MARKER // plotStyle ); // Set the series in the plot area $plotArea = new PlotArea(null, [$series]); // Set the chart legend $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false); $title = new Title('Test Scatter Trend Chart'); $yAxisLabel = new Title('Value ($k)'); // Create the chart $chart = new Chart( 'chart1', // name $title, // title $legend, // legend $plotArea, // plotArea true, // plotVisibleOnly DataSeries::EMPTY_AS_GAP, // displayBlanksAs null, // xAxisLabel $yAxisLabel, // yAxisLabel // added xAxis for correct date display $xAxis, // xAxis ); // Set the position where the chart should appear in the worksheet $chart->setTopLeftPosition('A7'); $chart->setBottomRightPosition('H20'); // Add the chart to the worksheet $worksheet->addChart($chart); // Save Excel 2007 file $filename = '33_Chart_create_Color_scatter_newlib.xlsx'; $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->setIncludeCharts(true); $writer->save($filename);