Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option for Dynamic Chart Legend Position #1699 #1922

Merged
merged 9 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Available Chart style options:
- ``colors``. A list of colors to use in the chart.
- ``title``. The title for the chart.
- ``showLegend``. Show legend, *true* or *false*.
- ``LegendPosition``. Legend position, *r* (default), *b*, *t*, *l* or *tr*.
- ``categoryLabelPosition``. Label position for categories, *nextTo* (default), *low* or *high*.
- ``valueLabelPosition``. Label position for values, *nextTo* (default), *low* or *high*.
- ``categoryAxisTitle``. The title for the category axis.
Expand Down
5 changes: 5 additions & 0 deletions samples/Sample_32_Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
$series3 = array(8, 3, 2, 5, 4);
$showGridLines = false;
$showAxisLabels = false;
$showLegend = true;
$legendPosition = 't';
// r = right, l = left, t = top, b = bottom, tr = top right

foreach ($chartTypes as $chartType) {
$section->addTitle(ucfirst($chartType), 2);
Expand All @@ -33,6 +36,8 @@
$chart->getStyle()->setShowGridX($showGridLines);
$chart->getStyle()->setShowGridY($showGridLines);
$chart->getStyle()->setShowAxisLabels($showAxisLabels);
$chart->getStyle()->setShowLegend($showLegend);
$chart->getStyle()->setLegendPosition($legendPosition);
if (in_array($chartType, $twoSeries)) {
$chart->addSeries($categories, $series2);
}
Expand Down
48 changes: 24 additions & 24 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected static function parseInlineStyle($node, $styles = array())
// tables, cells
if (false !== strpos($val, '%')) {
// e.g. <table width="100%"> or <td width="50%">
$styles['width'] = intval($val) * 50;
$styles['width'] = (int) $val * 50;
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT;
} else {
// e.g. <table width="250> where "250" = 250px (always pixels)
Expand All @@ -125,7 +125,7 @@ protected static function parseInlineStyle($node, $styles = array())
break;
case 'cellspacing':
// tables e.g. <table cellspacing="2">, where "2" = 2px (always pixels)
$val = intval($val).'px';
$val = (int) $val . 'px';
$styles['cellSpacing'] = Converter::cssToTwip($val);
break;
case 'bgcolor':
Expand Down Expand Up @@ -693,7 +693,7 @@ protected static function parseStyle($attribute, $styles)
// This may be adjusted, if better ratio or formula found.
// BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size)
$size = Converter::cssToTwip($matches[1]);
$size = intval($size / 2);
$size = (int) ($size / 2);
// valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
$styles["border{$which}Size"] = $size; // twips
$styles["border{$which}Color"] = trim($matches[2], '#');
Expand Down Expand Up @@ -871,9 +871,9 @@ protected static function mapAlign($cssAlignment)
}

/**
* Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc
* Transforms a HTML/CSS vertical alignment
*
* @param string $cssAlignment
* @param string $alignment
* @return string|null
*/
protected static function mapAlignVertical($alignment)
Expand Down Expand Up @@ -901,10 +901,10 @@ protected static function mapAlignVertical($alignment)
}

/**
* Map list style for ordered list
*
* @param string $cssListType
*/
* Map list style for ordered list
*
* @param string $cssListType
*/
protected static function mapListType($cssListType)
{
switch ($cssListType) {
Expand Down Expand Up @@ -959,32 +959,32 @@ protected static function parseLink($node, $element, &$styles)
}

/**
* Render horizontal rule
* Note: Word rule is not the same as HTML's <hr> since it does not support width and thus neither alignment
*
* @param \DOMNode $node
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
*/
* Render horizontal rule
* Note: Word rule is not the same as HTML's <hr> since it does not support width and thus neither alignment
*
* @param \DOMNode $node
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
*/
protected static function parseHorizRule($node, $element)
{
$styles = self::parseInlineStyle($node);

// <hr> is implemented as an empty paragraph - extending 100% inside the section
// Some properties may be controlled, e.g. <hr style="border-bottom: 3px #DDDDDD solid; margin-bottom: 0;">

$fontStyle = $styles + ['size' => 3];
$fontStyle = $styles + array('size' => 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you changed [] => array() ???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/Shared/Html.php
To keep the same code style. Other code of 'Html.php' file use 'array()' instead of '[]' too.


$paragraphStyle = $styles + [
'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc
'spacing' => 0, // twip
'spaceBefore' => 120, // twip, 240/2 (default line height)
'spaceAfter' => 120, // twip
'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'],
$paragraphStyle = $styles + array(
'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc
'spacing' => 0, // twip
'spaceBefore' => 120, // twip, 240/2 (default line height)
'spaceAfter' => 120, // twip
'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'],
'borderBottomColor' => empty($styles['color']) ? '000000' : $styles['color'],
'borderBottomStyle' => 'single', // same as "solid"
];
);

$element->addText("", $fontStyle, $paragraphStyle);
$element->addText('', $fontStyle, $paragraphStyle);

// Notes: <hr/> cannot be:
// - table - throws error "cannot be inside textruns", e.g. lists
Expand Down
47 changes: 46 additions & 1 deletion src/PhpWord/Style/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class Chart extends AbstractStyle
*/
private $showLegend = false;

/**
* Chart legend Position.
* Possible values are 'r', 't', 'b', 'l', 'tr'
*
* @var string
*/
private $legendPosition = 'r';

/**
* A list of display options for data labels
*
Expand Down Expand Up @@ -233,6 +241,7 @@ public function getColors()
* Set the colors to use in a chart.
*
* @param array $value a list of colors to use in the chart
* @return self
*/
public function setColors($value = array())
{
Expand All @@ -255,6 +264,7 @@ public function getTitle()
* Set the chart title
*
* @param string $value
* @return self
*/
public function setTitle($value = null)
{
Expand All @@ -277,6 +287,7 @@ public function isShowLegend()
* Set chart legend visibility
*
* @param bool $value
* @return self
*/
public function setShowLegend($value = false)
{
Expand All @@ -285,6 +296,37 @@ public function setShowLegend($value = false)
return $this;
}

/**
* Get chart legend position
*
* @return string
*/
public function getLegendPosition()
{
return $this->legendPosition;
}

/**
* Set chart legend position. choices:
* "r" - right of chart
* "b" - bottom of chart
* "t" - top of chart
* "l" - left of chart
* "tr" - top right of chart
*
* default: right
*
* @param string $legendPosition
* @return self
*/
public function setLegendPosition($legendPosition = 'r')
{
$enum = array('r', 'b', 't', 'l', 'tr');
$this->legendPosition = $this->setEnumVal($legendPosition, $enum, $this->legendPosition);

return $this;
}

/*
* Show labels for axis
*
Expand Down Expand Up @@ -328,7 +370,10 @@ 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]);
$this->dataLabelOptions[$option] = $this->setBoolVal(
$values[$option],
$this->dataLabelOptions[$option]
);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/PhpWord/Writer/Word2007/Part/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private function writePlotArea(XMLWriter $xmlWriter)

$title = $style->getTitle();
$showLegend = $style->isShowLegend();
$legendPosition = $style->getLegendPosition();

//Chart title
if ($title) {
Expand All @@ -154,7 +155,7 @@ private function writePlotArea(XMLWriter $xmlWriter)

//Chart legend
if ($showLegend) {
$xmlWriter->writeRaw('<c:legend><c:legendPos val="r"/></c:legend>');
$xmlWriter->writeRaw('<c:legend><c:legendPos val="' . $legendPosition . '"/></c:legend>');
}

$xmlWriter->startElement('c:plotArea');
Expand Down
36 changes: 18 additions & 18 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,14 @@ public function testParseLetterSpacing()
}

/**
* Parse widths in tables and cells, which also allows for controlling column width
*/
* Parse widths in tables and cells, which also allows for controlling column width
*/
public function testParseTableAndCellWidth()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
$section = $phpWord->addSection(array(
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
]);
));

// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
Expand Down Expand Up @@ -703,14 +703,14 @@ public function testParseTableAndCellWidth()
}

/**
* Test parsing background color for table rows and table cellspacing
*/
* Test parsing background color for table rows and table cellspacing
*/
public function testParseCellspacingRowBgColor()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
$section = $phpWord->addSection(array(
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
]);
));

// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
Expand Down Expand Up @@ -744,8 +744,8 @@ public function testParseCellspacingRowBgColor()
}

/**
* Parse horizontal rule
*/
* Parse horizontal rule
*/
public function testParseHorizRule()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand Down Expand Up @@ -774,7 +774,7 @@ public function testParseHorizRule()
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:pBdr/w:bottom';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val'));
$this->assertEquals(intval(5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz'));
$this->assertEquals((int) (5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz'));
$this->assertEquals('lightblue', $doc->getElement($xpath)->getAttribute('w:color'));

$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';
Expand All @@ -785,8 +785,8 @@ public function testParseHorizRule()
}

/**
* Parse ordered list start & numbering style
*/
* Parse ordered list start & numbering style
*/
public function testParseOrderedList()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand Down Expand Up @@ -846,8 +846,8 @@ public function testParseOrderedList()
}

/**
* Parse ordered list start & numbering style
*/
* Parse ordered list start & numbering style
*/
public function testParseVerticalAlign()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand Down Expand Up @@ -886,8 +886,8 @@ public function testParseVerticalAlign()
}

/**
* Fix bug - don't decode double quotes inside double quoted string
*/
* Fix bug - don't decode double quotes inside double quoted string
*/
public function testDontDecodeAlreadyEncodedDoubleQuotes()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand All @@ -900,6 +900,6 @@ public function testDontDecodeAlreadyEncodedDoubleQuotes()

Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue(is_object($doc));
$this->assertInternalType('object', $doc);
}
}