Skip to content

Commit fa05759

Browse files
authored
Merge pull request #1118 from plotly/templates
add makeTemplate and validateTemplate to plotly_js function reference.
2 parents 72f2384 + 757583c commit fa05759

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

_posts/reference_pages/2016-06-03-plotly_js_function_ref.html

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,77 @@ <h4 id="plotly-validate"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-re
361361
// "In data trace 0, key orientation is set to an invalid value (horizontal)"
362362
</code></pre>
363363

364+
<h4 id="plotly-makeTemplate"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-reference/#plotlymaketemplate">Plotly.makeTemplate</a></h4>
365+
366+
<code>Plotly.makeTemplate</code> copies the style information from a figure. It does this by returning a <code>template</code> object which can be passed to the <code>layout.template</code> attribute of another figure.
367+
<br /><br />
368+
<fieldset class="signatures">
369+
<legend>Signature</legend>
370+
<dl>
371+
<dt><code>Plotly.makeTemplate(figure)</code></dt>
372+
<dd>
373+
<dl>
374+
<dt><code>figure</code></dt>
375+
<dd>a plot object, with <code>{data, layout}</code> members
376+
</dl>
377+
</dd>
378+
</dl>
379+
</fieldset>
380+
<br /><br />
381+
382+
<pre><code class="language-javascript hljs" data-lang="javascript">
383+
var figure = {
384+
data: [{
385+
type: 'bar',
386+
marker: {color: 'red'},
387+
y: [2, 1, 3, 2],
388+
}],
389+
layout:{
390+
title: 'Quarterly Earnings'
391+
}
392+
};
393+
394+
var template = Plotly.makeTemplate(figure);
395+
396+
newData = [{
397+
type:'bar',
398+
y:[3,2,5,8]
399+
}]
400+
layoutWithTemplate = {template:template}
401+
402+
Plotly.newPlot(graphDiv,newData,layoutTemplate)
403+
404+
</code></pre>
405+
406+
<h4 id="plotly-validate-template"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-reference/#plotlyvalidatetemplate">Plotly.validateTemplate</a></h4>
407+
408+
<code>Plotly.validateTemplate</code> allows users to Test for consistency between the given figure and a template,
409+
either already included in the figure or given separately. Note that not every issue identified here is necessarily
410+
a problem, it depends on what you're using the template for.
411+
<br /><br />
412+
<fieldset class="signatures">
413+
<legend>Signature</legend>
414+
<dl>
415+
<dt><code>Plotly.validateTemplate(figure, template)</code></dt>
416+
<dd>
417+
<dl>
418+
<dt><code>figure</code> or <code>DOM Node</code></dt>
419+
<dd>where <code>figure</code> is a plot object, with <code>{data, layout}</code> members.
420+
<dt><code>template</code></dt>
421+
<dd>the template, with its own <code>{data, layout}</code>, to test.
422+
If omitted, we will look for a template already attached as
423+
the plot's <code>layout.template</code> attribute.
424+
</dl>
425+
</dd>
426+
</dl>
427+
</fieldset>
428+
<br /><br />
429+
430+
<pre><code class="language-javascript hljs" data-lang="javascript">
431+
var out = Plotly.validateTemplate(figure, template);
432+
console.log(out[0].msg)
433+
// "The template has 1 traces of type bar but there are none in the data."
434+
</code></pre>
364435

365436
<h4 id="plotly-addtraces"><a href="{{ BASE_URL }}/javascript/plotlyjs-function-reference/#plotlyaddtraces">Plotly.addTraces</a></h4>
366437
<em>This function has comparable performance to <a href="#plotlyreact"><code>Plotly.react</code></a> and is faster than redrawing the whole plot with <a href="#plotlynewplot"><code>Plotly.newPlot</code></a>.</em><br /><br />

0 commit comments

Comments
 (0)