Forum


Replies: 4   Views: 1628
Docxutilities setchartdata - cleanup chart when replacement data has fewer categories than source ch
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by bkidd  · 17-06-2020 - 01:50

I have a template with series (legends): North, South, East West, and categories January thruough December.  The Indexer getOutput returns the following for the source chart:

[
    "title" => "${Annual Sales By Region}"
    "data" => array:4 [
      0 => array:3 [
        "text" => "North"
        "cats" => array:12 [
          0 => "January"
          1 => "February"
          2 => "March"
          3 => "April"
          4 => "May"
          5 => "June"
          6 => "July"
          7 => "August"
          8 => "September"
          9 => "October"
          10 => "November"
          11 => "December"
        ]
        "vals" => array:12 [
          0 => "4.3"
          1 => "2.5"
          2 => "3.5"
          3 => "4.5"
          4 => "4.3"
          5 => "2.5"
          6 => "3.5"
          7 => "4.5"
          8 => "4.3"
          9 => "2.5"
          10 => "3.5"
          11 => "4.5"
        ]
      ]
      1 => array:3 [
        "text" => "South"
        "cats" => array:12 [
          0 => "January"
          1 => "February"
          2 => "March"
          3 => "April"
          4 => "May"
          5 => "June"
          6 => "July"
          7 => "August"
          8 => "September"
          9 => "October"
          10 => "November"
          11 => "December"
        ]
        "vals" => array:12 [
          0 => "2.4"
          1 => "4.4000000000000004"
          2 => "1.8"
          3 => "2.8"
          4 => "2.4"
          5 => "4.4000000000000004"
          6 => "1.8"
          7 => "2.8"
          8 => "2.4"
          9 => "4.4000000000000004"
          10 => "1.8"
          11 => "2.8"
        ]
      ]
      2 => array:3 [
        "text" => "East"
        "cats" => array:12 [
          0 => "January"
          1 => "February"
          2 => "March"
          3 => "April"
          4 => "May"
          5 => "June"
          6 => "July"
          7 => "August"
          8 => "September"
          9 => "October"
          10 => "November"
          11 => "December"
        ]
        "vals" => array:12 [
          0 => "2"
          1 => "2"
          2 => "3"
          3 => "5"
          4 => "2"
          5 => "2"
          6 => "3"
          7 => "5"
          8 => "2"
          9 => "2"
          10 => "3"
          11 => "5"
        ]
      ]
      3 => array:3 [
        "text" => "West"
        "cats" => array:12 [
          0 => "January"
          1 => "February"
          2 => "March"
          3 => "April"
          4 => "May"
          5 => "June"
          6 => "July"
          7 => "August"
          8 => "September"
          9 => "October"
          10 => "November"
          11 => "December"
        ]
        "vals" => array:12 [
          0 => "3.8"
          1 => "4.5"
          2 => "7.8"
          3 => "9.9"
          4 => "3.8"
          5 => "4.5"
          6 => "7.8"
          7 => "9.9"
          8 => "3.8"
          9 => "4.5"
          10 => "7.8"
          11 => "9.9"
        ]
      ]
    ]
]

I am passing the following data to setChartData:

array:1 [
  0 => array:4 [
    "title" => "Annual Sales by Region"
    "legends" => array:4 [
      0 => "North"
      1 => "South"
      2 => "East"
      3 => "West"
    ]
    "categories" => array:8 [
      0 => "January"
      1 => "February"
      2 => "March"
      3 => "April"
      4 => "May"
      5 => "June"
      6 => "July"
      7 => "August"
    ]
    "values" => array:8 [
      0 => array:4 [
        0 => 10.0
        1 => 2.4
        2 => 2.0
        3 => 3.8
      ]
      1 => array:4 [
        0 => 10.0
        1 => 4.4
        2 => 2.0
        3 => 4.5
      ]
      2 => array:4 [
        0 => 10.0
        1 => 1.8
        2 => 3.0
        3 => 7.8
      ]
      3 => array:4 [
        0 => 10.0
        1 => 2.8
        2 => 5.0
        3 => 9.9
      ]
      4 => array:4 [
        0 => 10.0
        1 => 2.4
        2 => 2.0
        3 => 3.8
      ]
      5 => array:4 [
        0 => 9.5
        1 => 4.4
        2 => 2.0
        3 => 4.5
      ]
      6 => array:4 [
        0 => 10.0
        1 => 1.8
        2 => 3.0
        3 => 7.8
      ]
      7 => array:4 [
        0 => 9.0
        1 => 2.8
        2 => 5.0
        3 => 9.9
      ]
    ]
  ]
]

The replacement data only has Januaary through August categories but it seems that, although I am explicitly setting the categories, internally, the chart is expecting the data to be the same size.

Is there any way to clean up the chart so it will not expect the same number of categories as in the source template?  I can understand if only values can be updated but it seems that if I can provide the categories, the categories I provide should take precedence over the source chart categories.

Any suggestions when I am not certain of the number of categories that will be provided?  It is common to have a chart for an entire year but only have data populated up to the current month.

Thanks

Posted by admin  · 17-06-2020 - 06:16

Hello,

replaceChartData (https://www.phpdocx.com/api-documentation/docxutilities/replace-chart-data-Word-document) only replaces existing values in a chart, but it doesn't remove or add values. We have added a task to the dev team to add support.

To add custom charts using any number of values there's no other alternative than using addChart (https://www.phpdocx.com/api-documentation/word-content/add-chart-Word-document-with-PHP).

Regards.

Posted by bkidd  · 17-06-2020 - 09:56

Okay, what is the purpose of providing the legends and categories as part of the data? Can the legends/categories be renamed provided the length of the arrays are the same as in the source chart?

Posted by admin  · 17-06-2020 - 10:32

Hello,

replaceChartData allows replacing data values, legends, categories and the title. We recommend you to run the included sample examples/DocxUtilities/replaceChartData/sample_1.php included in the package and check the DOCX source and the output, this sample illustrates the method changing that data.

Regards.

Posted by bkidd  · 17-06-2020 - 12:13

I reviewed the sample and confirmed that it's okay to rename legends and categories if the replacement arrays are the same size as the source chart.

I will add my suggestion to the "Suggestions" section.