87 lines
2.8 KiB
HTML
87 lines
2.8 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Formula widget | CARTO</title>
|
|
<meta name="viewport" content="initial-scale=1.0">
|
|
<meta charset="utf-8">
|
|
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Open+Sans:300,400,600" rel="stylesheet">
|
|
<!-- Include CARTO.js -->
|
|
<script src="../../../dist/public/carto.js"></script>
|
|
<link href="../style.css" rel="stylesheet">
|
|
</head>
|
|
<body class="bg-gray">
|
|
<div class="dataview">
|
|
<ul>
|
|
<li>
|
|
<h2 class="h2">Column</h2>
|
|
<input id="column" type="text" value="pop_max" class="input_text open-sans"></input>
|
|
</li>
|
|
<li>
|
|
<h2 class="h2">Operation</h2>
|
|
<select id="operation" class="select open-sans">
|
|
<option value="count">COUNT</option>
|
|
<option value="sum">SUM</option>
|
|
<option value="avg">AVG</option>
|
|
<option value="max">MAX</option>
|
|
<option value="min">MIN</option>
|
|
</select>
|
|
</li>
|
|
</ul>
|
|
<button onclick="applyDataviewChanges()" class="button open-sans">Apply</button>
|
|
<pre class="code" id="data"></pre>
|
|
</div>
|
|
<aside class="toolbox">
|
|
<div class="box">
|
|
<header>
|
|
<h1>Formula widget</h1>
|
|
<button class="github-logo js-source-link"></button>
|
|
</header>
|
|
<section>
|
|
<p class="description open-sans">Create a widget with the formula dataview.</p>
|
|
<div class="separator"></div>
|
|
<section class="usage">
|
|
<header>USAGE</header>
|
|
<p class="open-sans">Change column or operation on the form.</p>
|
|
<p class="open-sans">Example columns: rank_max, pop_max, pop_min.</p>
|
|
</section>
|
|
<div id="controls">
|
|
<div id="info"></div>
|
|
</div>
|
|
</section>
|
|
<footer class="js-footer"></footer>
|
|
</div>
|
|
</aside>
|
|
|
|
<script>
|
|
const client = new carto.Client({
|
|
apiKey: 'default_public',
|
|
username: 'cartojs-test'
|
|
});
|
|
|
|
const source = new carto.source.Dataset('ne_10m_populated_places_simple');
|
|
|
|
const formulaDataview = new carto.dataview.Formula(source, 'pop_max', {
|
|
operation: carto.operation.COUNT
|
|
});
|
|
|
|
formulaDataview.on('dataChanged', data => {
|
|
document.getElementById('data').innerHTML = JSON.stringify(data, null, 4);
|
|
});
|
|
|
|
formulaDataview.on('error', error => {
|
|
alert(error.message);
|
|
});
|
|
|
|
client.addDataview(formulaDataview);
|
|
|
|
function applyDataviewChanges() {
|
|
const column = document.getElementById('column').value;
|
|
const operation = document.getElementById('operation').value;
|
|
|
|
formulaDataview.setColumn(column);
|
|
formulaDataview.setOperation(operation);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|