1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<!DOCTYPE html>
<html>
<head>
<title>Vega Theme Test</title>
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<script src="build/vega-themes.js"></script>
<style>
body {
margin: 10px;
font-size: 14px;
font-family: sans-serif;
position: relative;
}
#header {
padding: 10px 0 20px;
height: 20px;
}
#views {
display: flex;
flex-wrap: wrap;
justify-content: left;
}
.view {
flex: 0;
margin-right: 5px;
margin-bottom: 10px;
}
.view > canvas,
.view > svg {
border: 1px dashed #ccc;
}
.vega-bind {
line-height: 18px;
margin-bottom: 2px;
}
.vega-bind-name {
display: inline-block;
width: 100px;
}
.vega-bind input[type="range"] {
width: 400px;
}
.vega-bind label {
margin: 0 0.5em 0 2px;
}
</style>
</head>
<body>
<div id="header">
Theme:
<select id="themes">
<option value="default">default</option>
<option value="excel">excel</option>
<option value="ggplot2">ggplot2</option>
<option value="quartz">quartz</option>
<option value="vox">vox</option>
<option value="dark">dark</option>
<option value="fivethirtyeight">fivethirtyeight</option>
<option value="latimes">latimes</option>
<option value="urbaninstitute">urbaninstitute</option>
<option value="googlecharts">googlecharts</option>
<option value="powerbi">powerbi</option>
<option value="carbonwhite">carbonwhite</option>
<option value="carbong10">carbong10</option>
<option value="carbong90">carbong90</option>
<option value="carbong100">carbong100</option>
</select>
Renderer:
<select id="render">
<option value="svg" selected>svg</option>
<option value="canvas">canvas</option>
</select>
<br />
</div>
<div id="views"></div>
<script>
var files = [
"bars.vg.json",
"lines.vg.json",
"scatter.vg.json",
"stacked.vl.json",
"area.vl.json",
"heatmap.vl.json",
"ramp.vl.json",
"diverging.vl.json",
];
var specs = [];
var container = document.querySelector("#views"),
loader = vega.loader(),
renderType = "svg",
theme = {};
Promise.all(
files.map(function (file) {
var index = specs.length;
specs.push(file);
return loader.load(file).then(function (json) {
specs[index] = JSON.parse(json);
});
})
)
.then(function () {
var earlierChoosenTheme = url.searchParams.get("theme");
var selectedGraph = url.searchParams.get("renderer");
if (earlierChoosenTheme) {
theme = vegaThemes[earlierChoosenTheme];
themes.value = earlierChoosenTheme;
}
if (selectedGraph) {
render.value = selectedGraph;
renderType = selectedGraph;
}
refresh();
})
.catch(function (err) {
console.error(err, err.stack);
});
var themes = document.querySelector("#themes");
var currentLocation = window.location;
var url = new URL(currentLocation);
themes.addEventListener("change", function () {
theme = vegaThemes[themes.options[themes.selectedIndex].value];
url.searchParams.set("theme", `${themes.options[themes.selectedIndex].value}`);
window.history.replaceState(null, null, url);
refresh();
});
var render = document.querySelector("#render");
render.addEventListener("change", function () {
renderType = render.options[render.selectedIndex].value;
url.searchParams.set("renderer", `${render.options[render.selectedIndex].value}`);
window.history.replaceState(null, null, url);
refresh();
});
function refresh() {
container.innerHTML = "";
setBackground(theme);
specs.forEach(function (spec) {
var el = document.createElement("div");
el.setAttribute("class", "view");
container.appendChild(el);
vegaEmbed(el, spec, {
renderer: renderType,
config: theme,
defaultStyle: true,
});
});
}
function setBackground(theme) {
document.body.style.background = null;
document.body.style.color = null;
if (!theme) {
return;
}
if ("background" in theme) {
document.body.style.background = theme.background;
}
if ("title" in theme && "color" in theme.title) {
document.body.style.color = theme.title.color;
}
}
</script>
</body>
</html>
|