.. include:: ../../../Includes.txt .. _designingTemplatesFromExamples: ================================= Designing Templates From Examples ================================= Introduction ============ The `Chart.js documentation `_ provides examples of various chart types. The SAV Charts extension includes templates for all chart types. These templates can be found in :guilabel:`Resources/Private/Templates/ChartExamples/FluidParser` for the Fluid Parser and :guilabel:`Resources/Private/Templates/ChartExamples` for the XML Parser. To display a template, simply create an SAV Charts plugin content element on a page. .. figure:: ../../../Images/Tutorial/NewSavChartsPlugin.png :width: 60% Then click on the plugin tab and select the parser type. .. figure:: ../../../Images/Tutorial/NewSavChartsFlexformPlugin.png :width: 60% Fill in the template field of the Flexform corresponding to the selected parser type, then save. .. important:: Use the EXT: prefix to reference the template file from the extension. .. tabs:: .. tab:: Fluid Parser .. code-block:: xml EXT:sav_charts/Resources/Private/Templates/ChartsExamples/FluidParser/LineChart.fluid or .. code-block:: xml .. tab:: XML Parser .. code-block:: xml .. figure:: ../../../Images/Tutorial/NewSavChartsFlexformPluginFillTemplate.png :width: 60% Go to the front-end and you should see the following image. .. figure:: ../../../Images/Tutorial/LineChartTemplateInFrontend.png :width: 60% Writing Templates ================= The templates provided with the extension were adapted from the examples given in the `Chart.js documentation `_. Let us illustrate the principle for the line chart. The JavaScript code to display the line graph is as follows: .. code-block:: javascript var data = { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [ { label: "My First dataset", fillColor: "rgba(220,220,220,0.2)", strokeColor: "rgba(220,220,220,1)", pointColor: "rgba(220,220,220,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(220,220,220,1)", data: [65, 59, 80, 81, 56, 55, 40] }, { label: "My Second dataset", fillColor: "rgba(151,187,205,0.2)", strokeColor: "rgba(151,187,205,1)", pointColor: "rgba(151,187,205,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(151,187,205,1)", data: [28, 48, 40, 19, 86, 27, 90] } ] }; The template is created by converting the JavaScript code. Thanks to the data tag ID, references can be used and applied. These references provide an easy way to split the data, making it possible to overload them in the FlexForm data section or with queries. The following codes are the Fluid or XML translations of the previous JavaScript code. Data tags with the IDs `labels`, `dataSet1`, and `dataSet2` can be easily overloaded, as explained in the next section. .. tabs:: .. tab:: Fluid Parser .. code-block:: xml My First dataset My Second dataset January, February, March, April, May, June, July 65, 59, 80, 81, 56, 55, 40 28, 48, 40, 19, 86, 27, 90 rgba(220,220,220,0.2) rgba(220,220,220,1) #fff rgba(220,220,220,1) rgba(151,187,205,0.2) rgba(151,187,205,1) #fff rgba(151,187,205,1) .. tab:: XML Parser .. code-block:: xml My First dataset My Second dataset January, February, March, April, May, June, July 65, 59, 80, 81, 56, 55, 40 28, 48, 40, 19, 86, 27, 90 rgba(220,220,220,0.2) rgba(220,220,220,1) #fff rgba(220,220,220,1) rgba(151,187,205,0.2) rgba(151,187,205,1) #fff rgba(151,187,205,1) .. tip:: `` were inserted inside the `` tag just by personal choice. They could have been provided before the `` tag. In that case, the tag would have no content and would simply be written as follows: .. tabs:: .. tab:: Fluid Parser .. code-block:: xml .. tab:: XML Parser .. code-block:: xml Overloading Data ================ Open the plugin in the backend, fill out the `Data` section of the Flexform as follows, and then save. .. tabs:: .. tab:: Fluid Parser .. code-block:: xml 20,22,24,20,19 55,50,51,53,60 Monday, Tuesday, Wednesday, Thursdat, Friday .. tab:: XML Parser .. code-block:: xml 20,22,24,20,19 55,50,51,53,60 Monday, Tuesday, Wednesday, Thursdat, Friday Go in the front-end and you should see the following figure. .. figure:: ../../../Images/Tutorial/LineChartWithModifiedDataInFrontend.png :width: 60% In SAV Charts, the principle is that the first definition is used for a given tag with a given ID. In the previous example, for instance, the `dataSet0` and `dataSet1` tags are defined in the data section before the template section. Consequently, they will not be replaced by the same data tags in the template. The tags in the data section overload the same tags in the template. This provides a simple way to work with generic templates and adapt their behavior to your data. For example, with the same `LineChart.fluid` template, you may want to display only one curve. This can easily be achieved by defining `dataSets` in the data section, as shown below. .. tabs:: .. tab:: Fluid Parser .. code-block:: xml 20,22,24,20,19 Monday, Tuesday, Wednesday, Thursdat, Friday .. tab:: XML Parser .. code-block:: xml 20,22,24,20,19 Monday, Tuesday, Wednesday, Thursdat, Friday Enter the previous code into the FlexForm Data section. Then, save and go to the front-end. .. figure:: ../../../Images/Tutorial/SingleLineChartWithModifiedDataInFrontend.png :width: 60% .. note:: In very specific cases, you can explicitly allow overloading of a given tag by setting the Overload attribute to 1. This feature is used to build a flexible template in the file :guilabel:`Resources/Private/Templates/ChartExamples/LineChartAdvanced.xml`. Overloading Markers =================== Markers are tags that can be used to create strings. In the line chart template, two markers are used to define the labels that are displayed in the legend. You can overlay the markers in the `Markers` section of the Flexform. Enter the following code in the `Markers` and `Data` sections. Then, save and go to the front-end. .. tabs:: .. tab:: Fluid Parser Markers section .. code-block:: xml Temperature Humidity Data section .. code-block:: xml 20,22,24,20,19 55,50,51,53,60 Monday, Tuesday, Wednesday, Thursdat, Friday .. tab:: XML Parser Markers section .. code-block:: xml Temperature Humidity Data section .. code-block:: xml 20,22,24,20,19 55,50,51,53,60 Monday, Tuesday, Wednesday, Thursdat, Friday .. figure:: ../../../Images/Tutorial/LineChartWithModifiedMarkerInFrontend.png :width: 60% Options ======= Options are defined in the Chart.js documentation. They can be set by overloading the `` tag associated with the options attribute of the chart. In the `LineChart.fluid` template, for example, the chart is defined as follows: .. tabs:: .. tab:: Fluid Parser .. code-block:: xml .... .. tab:: XML Parser .. code-block:: xml .... The `options` attribute is a reference to the `` tag whose ID is `lineChartOptions`. Overloading this tag in the `Data` section of the Flexform will set the options. Assume that the `Data` section is still configured with a modified label. Add the following code to the `Data` section of the Flexform. This will cancel the animation, set a linear interpolation between points, and display the line a width of 10 points. .. tabs:: .. tab:: Fluid Parser .. code-block:: xml .. tab:: XML Parser .. code-block:: xml .. note:: A linear interpolation is obtained by setting the tension to 0 in `Chart.defaults.global.elements.line` (see `Chart.js documentation `_). Since the global options are in the `` whose ID is `lineChartOptions`, nested items whose keys are `elements` and `line` provide access to `Chart.defaults.global.elements.line`. .. figure:: ../../../Images/Tutorial/SingleLineChartWithOptionsInFrontend.png :width: 60% .. tip:: To generate a Boolean value, set the attribute `value` to `true` or `false`.