Skip to content

Commit

Permalink
New features when creating charts (PHPOffice#1332)
Browse files Browse the repository at this point in the history
* add stacked bar and column charts
* add chart colors feature
* adding preliminary chart axis title functionality to XMLwriter
* added percent_stacked to available types array
* Make tick mark and tick label positions configurable
* scrutinizer fixes
* update changelog
  • Loading branch information
jaek-s authored and troosan committed May 11, 2018
1 parent 94be56b commit 65b0f06
Show file tree
Hide file tree
Showing 6 changed files with 531 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ v0.15.0 (?? ??? 2018)
- Added the ability to enable gridlines and axislabels on charts @FrankMeyer #576
- Add support for table indent (tblInd) @Trainmaster #1343
- Added parsing of internal links in HTML reader @lalop #1336
- Several improvements to charts @JAEK-S #1332

### Fixed
- Fix reading of docx default style - @troosan #1238
Expand Down
4 changes: 2 additions & 2 deletions samples/Sample_32_Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
$section->addTitle('2D charts', 1);
$section = $phpWord->addSection(array('colsNum' => 2, 'breakType' => 'continuous'));

$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar');
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar');
$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
$threeSeries = array('bar', 'line');
$categories = array('A', 'B', 'C', 'D', 'E');
$series1 = array(1, 3, 2, 5, 4);
Expand Down
16 changes: 11 additions & 5 deletions src/PhpWord/Element/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ class Chart extends AbstractElement
* @param array $categories
* @param array $values
* @param array $style
* @param null|mixed $seriesName
*/
public function __construct($type, $categories, $values, $style = null)
public function __construct($type, $categories, $values, $style = null, $seriesName = null)
{
$this->setType($type);
$this->addSeries($categories, $values);
$this->addSeries($categories, $values, $seriesName);
$this->style = $this->setNewStyle(new ChartStyle(), $style, true);
}

Expand All @@ -86,7 +87,7 @@ public function getType()
*/
public function setType($value)
{
$enum = array('pie', 'doughnut', 'line', 'bar', 'column', 'area', 'radar', 'scatter');
$enum = array('pie', 'doughnut', 'line', 'bar', 'stacked_bar', 'percent_stacked_bar', 'column', 'stacked_column', 'percent_stacked_column', 'area', 'radar', 'scatter');
$this->type = $this->setEnumVal($value, $enum, 'pie');
}

Expand All @@ -95,10 +96,15 @@ public function setType($value)
*
* @param array $categories
* @param array $values
* @param null|mixed $name
*/
public function addSeries($categories, $values)
public function addSeries($categories, $values, $name = null)
{
$this->series[] = array('categories' => $categories, 'values' => $values);
$this->series[] = array(
'categories' => $categories,
'values' => $values,
'name' => $name,
);
}

/**
Expand Down
212 changes: 212 additions & 0 deletions src/PhpWord/Style/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,60 @@ class Chart extends AbstractStyle
private $is3d = false;

/**
* A list of colors to use in the chart
*
* @var array
*/
private $colors = array();

/**
* A list of display options for data labels
*
* @var array
*/
private $dataLabelOptions = array(
'showVal' => true, // value
'showCatName' => true, // category name
'showLegendKey' => false, //show the cart legend
'showSerName' => false, // series name
'showPercent' => false,
'showLeaderLines' => false,
'showBubbleSize' => false,
);

/**
* A string that tells the writer where to write chart labels or to skip
* "nextTo" - sets labels next to the axis (bar graphs on the left) (default)
* "low" - labels on the left side of the graph
* "high" - labels on the right side of the graph
*
* @var string
*/
private $categoryLabelPosition = 'nextTo';

/**
* A string that tells the writer where to write chart labels or to skip
* "nextTo" - sets labels next to the axis (bar graphs on the bottom) (default)
* "low" - labels are below the graph
* "high" - labels above the graph
*
* @var string
*/
private $valueLabelPosition = 'nextTo';

/**
* @var string
*/
private $categoryAxisTitle;

/**
* @var string
*/
private $valueAxisTitle;

private $majorTickMarkPos = 'none';

/*
* Show labels for axis
*
* @var bool
Expand Down Expand Up @@ -146,6 +200,28 @@ public function set3d($value = true)
}

/**
* Get the list of colors to use in a chart.
*
* @return array
*/
public function getColors()
{
return $this->colors;
}

/**
* Set the colors to use in a chart.
*
* @param array $value a list of colors to use in the chart
*/
public function setColors($value = array())
{
$this->colors = $value;

return $this;
}

/*
* Show labels for axis
*
* @return bool
Expand All @@ -169,6 +245,31 @@ public function setShowAxisLabels($value = true)
}

/**
* get the list of options for data labels
*
* @return array
*/
public function getDataLabelOptions()
{
return $this->dataLabelOptions;
}

/**
* Set values for data label options.
* This will only change values for options defined in $this->dataLabelOptions, and cannot create new ones.
*
* @param array $values [description]
*/
public function setDataLabelOptions($values = array())
{
foreach (array_keys($this->dataLabelOptions) as $option) {
if (isset($values[$option])) {
$this->dataLabelOptions[$option] = $this->setBoolVal($values[$option], $this->dataLabelOptions[$option]);
}
}
}

/*
* Show Gridlines for Y-Axis
*
* @return bool
Expand All @@ -192,6 +293,117 @@ public function setShowGridY($value = true)
}

/**
* Get the categoryLabelPosition setting
*
* @return string
*/
public function getCategoryLabelPosition()
{
return $this->categoryLabelPosition;
}

/**
* Set the categoryLabelPosition setting
* "none" - skips writing labels
* "nextTo" - sets labels next to the (bar graphs on the left)
* "low" - labels on the left side of the graph
* "high" - labels on the right side of the graph
*
* @param mixed $labelPosition
* @return self
*/
public function setCategoryLabelPosition($labelPosition)
{
$enum = array('nextTo', 'low', 'high');
$this->categoryLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->categoryLabelPosition);

return $this;
}

/**
* Get the valueAxisLabelPosition setting
*
* @return string
*/
public function getValueLabelPosition()
{
return $this->valueLabelPosition;
}

/**
* Set the valueLabelPosition setting
* "none" - skips writing labels
* "nextTo" - sets labels next to the value
* "low" - sets labels are below the graph
* "high" - sets labels above the graph
*
* @param string
* @param mixed $labelPosition
*/
public function setValueLabelPosition($labelPosition)
{
$enum = array('nextTo', 'low', 'high');
$this->valueLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->valueLabelPosition);

return $this;
}

/**
* Get the categoryAxisTitle
* @return string
*/
public function getCategoryAxisTitle()
{
return $this->categoryAxisTitle;
}

/**
* Set the title that appears on the category side of the chart
* @param string $axisTitle
*/
public function setCategoryAxisTitle($axisTitle)
{
$this->categoryAxisTitle = $axisTitle;

return $this;
}

/**
* Get the valueAxisTitle
* @return string
*/
public function getValueAxisTitle()
{
return $this->valueAxisTitle;
}

/**
* Set the title that appears on the value side of the chart
* @param string $axisTitle
*/
public function setValueAxisTitle($axisTitle)
{
$this->valueAxisTitle = $axisTitle;

return $this;
}

public function getMajorTickPosition()
{
return $this->majorTickMarkPos;
}

/**
* set the position for major tick marks
* @param string $position [description]
*/
public function setMajorTickPosition($position)
{
$enum = array('in', 'out', 'cross', 'none');
$this->majorTickMarkPos = $this->setEnumVal($position, $enum, $this->majorTickMarkPos);
}

/*
* Show Gridlines for X-Axis
*
* @return bool
Expand Down
Loading

0 comments on commit 65b0f06

Please sign in to comment.