Skip to content

Commit 607365e

Browse files
committed
Added Sample Diagrams for people to switch between by clicking buttons
- The page first checks if the user has changes they might want to keep
1 parent ec991ad commit 607365e

File tree

4 files changed

+206
-30
lines changed

4 files changed

+206
-30
lines changed

build/build.css

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ p#chart {
166166
width: 600px;
167167
height: 500px;
168168
border: 1px dotted #666;
169-
margin: 10px auto;
169+
margin: 0.4em auto;
170170
background-color: white;
171171
}
172172

@@ -201,7 +201,7 @@ th#title {
201201
font-size: large;
202202
}
203203

204-
div#input_options {
204+
.center_text {
205205
text-align: center;
206206
}
207207

@@ -237,3 +237,53 @@ td#flows_entry {
237237
text-align: left;
238238
margin: auto;
239239
}
240+
241+
#example_diagrams {
242+
background-color: #F3F4FF;
243+
border: 1px dotted #069;
244+
width: 75%;
245+
border-radius: 0.7em;
246+
margin: 0.4em auto 1.1em;
247+
padding: 0.5em;
248+
box-shadow: 0.2em 0.2em 0.6em gray;
249+
}
250+
251+
#example_diagrams h4 {
252+
margin-top: 0;
253+
margin-bottom: 0;
254+
}
255+
256+
#example_diagrams button {
257+
margin-left: 0.5em;
258+
margin-right: 0.5em;
259+
}
260+
261+
#reset_graph_warning {
262+
background-color: #FFA;
263+
color: darkred;
264+
margin: 0.2em 3em 0;
265+
padding: 0.3em;
266+
border-radius: 0.5em;
267+
}
268+
269+
#reset_graph_yes {
270+
color: darkred;
271+
}
272+
273+
.callout_container {
274+
text-align: center;
275+
margin: 0.3em;
276+
}
277+
278+
.callout {
279+
background-color: #E3FF9F;
280+
padding: 0.3em 1.5em;
281+
font-size: 90%;
282+
border-style: dotted;
283+
border-color: #6AE58B;
284+
border-width: 1px;
285+
}
286+
287+
#layout_options_section {
288+
margin-top: 4px;
289+
}

build/index.html

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<script src="sankey.js"></script>
1111
<script src="canvg.js"></script>
1212
<script src="rgbcolor.js"></script>
13+
<script src="sample_diagrams.js"></script>
1314
<script src="sankeymatic.js"></script>
1415
</head>
1516
<body>
@@ -29,20 +30,33 @@
2930
</div>
3031
</nav>
3132
</header>
32-
<div style="text-align: center; margin-top: 1px; margin-bottom: 16px;">
33-
<span style="background-color: #E3FF9F; padding: 2px 20px; font-size: 90%; border-width: 0px 1px 1px; border-style: dotted; border-color: #6AE58B;">
34-
If you find SankeyMATIC useful, please consider <a href="../about/" target="_blank">donating to support further development</a>. Thanks!</span>
35-
</div>
3633

37-
<main>
34+
<div class="callout_container"><span class="callout">
35+
If you find SankeyMATIC useful, please consider <a href="../about/" target="_blank">donating to support further development</a>. Thanks!
36+
</span></div>
3837

38+
<main>
3939
<div class="center_basic">
4040

4141
<form onsubmit="process_sankey(); return false;">
42+
<div id="example_diagrams" class="center_text">
43+
<h4>Sample Diagrams:
44+
<button type="button" id="load_example_job_search" onclick="reset_graph('job_search'); return false;">Job Search</button>
45+
<button type="button" id="load_example_election" onclick="reset_graph('election'); return false;">Ranked Election</button>
46+
<!-- <button type="button" id="load_example_energy_all" onclick="reset_graph('energy_flows_all'); return false;">Energy Flows</button> &nbsp; -->
47+
<button type="button" id="load_example_basic_budget" onclick="reset_graph('default_budget'); return false;">Basic Budget (Default)</button>
48+
</h4>
49+
50+
<p id="reset_graph_warning" style="display: none;">
51+
<em>This will <strong>erase</strong> your changes. Are you sure?</em>
52+
<input type="hidden" id="demo_graph_chosen" value="" />
53+
<button type="button" id="reset_graph_yes" onclick="reset_graph_confirmed(); return false;">Yes, reset the diagram</button>
54+
<button type="button" id="reset_graph_no" onclick="cancel_reset_graph(); return false;">Cancel</button>
55+
</p>
56+
</div>
4257

4358
<table><tr><td>
4459

45-
4660
<table class="center_basic">
4761
<tr>
4862
<td id="other_entry">
@@ -51,28 +65,29 @@ <h2 class="ui_head" onclick="toggle_panel('input_options');">
5165
<span id="input_options_indicator" class="indicator">&ndash;</span>
5266
Inputs<span id="input_options_hint">:</span></h2>
5367

54-
<div id="input_options">
55-
<textarea id="flows_in" rows="22" cols="40" onchange="process_sankey();">' Type a list of Flows between Nodes, like:
68+
<div id="input_options" class="center_text">
69+
<textarea id="flows_in" rows="22" cols="40" onchange="process_sankey();">' Enter Flows between Nodes, like this:
5670
' Source [AMOUNT] Target
57-
' Examples:
58-
Wages [2000] Budget
59-
Interest [25] Budget
6071

61-
Budget [500] Taxes
62-
Budget [450] Housing
63-
Budget [310] Food
64-
Budget [205] Transportation
65-
Budget [400] Health Care
72+
Wages [1500] Budget
73+
Other [250] Budget
74+
75+
Budget [450] Taxes
76+
Budget [420] Housing
77+
Budget [400] Food
78+
Budget [295] Transportation
79+
Budget [25] Savings
6680

6781
' You can set a specific color for a Node:
68-
:Taxes #CC66FF
69-
' ...and for a Flow:
70-
Budget [160] Other Necessities #00CC00
71-
' Visit the 'Manual' link for more!
82+
:Taxes #90C
83+
' ...and for a single Flow:
84+
Budget [160] Other Necessities #0F0
7285

7386
' After entering your data, use the
7487
' controls below to customize your
75-
' diagram's appearance.</textarea>
88+
' diagram's appearance.
89+
90+
' Visit the 'Manual' link for more!</textarea>
7691

7792
<p class="center_basic"><button id="preview_graph" type="submit">Preview</button></p>
7893
</div>
@@ -86,7 +101,7 @@ <h2 class="ui_head" onclick="toggle_panel('diagram_options');">
86101
<div id="diagram_options" class="form_chunk">
87102
<p class="form_elements1">
88103
<label for="canvas_width"><strong>Diagram Width:</strong></label>
89-
<span class="no_wrap"><input type="text" class="wholenumber" id="canvas_width" size="5" maxlength="6" value="600" onchange="process_sankey();"> px</span>
104+
<span class="no_wrap"><input type="text" class="wholenumber" id="canvas_width" size="5" maxlength="6" value="700" onchange="process_sankey();"> px</span>
90105
<label for="canvas_height" class="spaced_label"><strong>Height:</strong></label>
91106
<span class="no_wrap"><input type="text" class="wholenumber" id="canvas_height" size="5" maxlength="6" value="600" onchange="process_sankey();"> px</span>
92107
</p>
@@ -304,7 +319,7 @@ <h3 class="ui_head" onclick="toggle_panel('svg_options');"><span id="svg_options
304319
</td><td id="grapharea">
305320

306321
<div class="expandable_box">
307-
<h2 class="ui_head" onclick="toggle_panel('layout_options');">
322+
<h2 id="layout_options_section" class="ui_head" onclick="toggle_panel('layout_options');">
308323
<span id="layout_options_indicator" class="indicator">+</span>
309324
Layout Options<span id="layout_options_hint">...</span></h2>
310325
<div id="layout_options" class="form_chunk" style="display: none;">
@@ -355,9 +370,8 @@ <h2 class="ui_head" onclick="toggle_panel('layout_options');">
355370
</main>
356371

357372

358-
<div style="text-align: center; margin-top: 12px; margin-bottom: 1px;">
359-
<span style="background-color: #E3FF9F; padding: 2px 20px; font-size: 90%; border-width: 1px 1px 0px; border-style: dotted; border-color: #6AE58B;">
360-
<!-- <div style="text-align: center; background-color: #ECFF60; font-size: 90%; padding: 4px; "> -->
373+
<div class="callout_container">
374+
<span class="callout">
361375
If you find SankeyMATIC useful, please consider <a href="../about/" target="_blank">donating to support further development</a>. Thanks!</span>
362376
</div>
363377

build/sample_diagrams.js

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/sankeymatic.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ function produce_svg_code() {
198198
return;
199199
}
200200

201-
// render_exportable_outputs: After the diagram is updated (or when the user
202-
// changes their preferred resolution), kick off a re-render of the image & SVG.
201+
// render_exportable_outputs: Kick off a re-render of the static image and the
202+
// user-copyable SVG code.
203+
// Called after the initial draw & when the user chooses a new PNG resolution
203204
glob.render_exportable_outputs = function () {
204205
// Reset the existing export output areas:
205206
var png_link_el = document.getElementById("download_png_link"),
@@ -219,6 +220,81 @@ glob.render_exportable_outputs = function () {
219220
return null;
220221
};
221222

223+
function hide_reset_warning() {
224+
// Hide the overwrite-warning paragraph (if it's showing)
225+
const warning_el = document.getElementById("reset_graph_warning");
226+
warning_el.style.display = "none";
227+
return null;
228+
}
229+
230+
glob.cancel_reset_graph = function () {
231+
hide_reset_warning();
232+
return null;
233+
}
234+
235+
glob.reset_graph_confirmed = function () {
236+
const graphname = document.getElementById("demo_graph_chosen").value;
237+
const replacement_flow_data = (
238+
sample_diagram_recipes.hasOwnProperty(graphname)
239+
? sample_diagram_recipes[graphname].flows.replace("\\n","\n")
240+
: "Requested sample diagram not found"
241+
);
242+
const flows_el = document.getElementById("flows_in")
243+
244+
hide_reset_warning();
245+
246+
// Select all the text...
247+
flows_el.focus();
248+
flows_el.select();
249+
// ... then replace it with the new content.
250+
flows_el.setRangeText(replacement_flow_data,
251+
0, flows_el.selectionEnd, 'start');
252+
253+
// Draw the new diagram immediately:
254+
process_sankey();
255+
// Put the cursor in the input field afresh:
256+
flows_el.focus();
257+
return null;
258+
}
259+
260+
glob.reset_graph = function (graphname) {
261+
// Is there a recipe with the given key? If so, let's proceed.
262+
if (sample_diagram_recipes.hasOwnProperty(graphname)) {
263+
// Set the 'demo_graph_chosen' value according to the user's click:
264+
const chosen_el = document.getElementById("demo_graph_chosen");
265+
chosen_el.value = graphname;
266+
267+
// Test the user's current input against the saved samples:
268+
const user_input = document.getElementById("flows_in").value;
269+
let flows_match_a_sample = false;
270+
Object.keys(sample_diagram_recipes).forEach(
271+
graph => {
272+
if (user_input == sample_diagram_recipes[graph].flows) {
273+
flows_match_a_sample = true;
274+
}
275+
}
276+
);
277+
if (flows_match_a_sample) {
278+
// If the user has NOT changed the input from one of the samples,
279+
// just go ahead with the change:
280+
reset_graph_confirmed();
281+
} else {
282+
// Otherwise, show the warning and do NOT reset the graph:
283+
const warning_el = document.getElementById("reset_graph_warning");
284+
warning_el.style.display = "";
285+
const yes_button_el = document.getElementById("reset_graph_yes");
286+
yes_button_el.innerHTML = `Yes, replace the graph with '${sample_diagram_recipes[graphname].name}'`;
287+
}
288+
} else {
289+
console.log('graph name not found');
290+
// the graph name wasn't valid.
291+
// (this shouldn't happen unless the user is messing around in the DOM)
292+
// give the user some feedback?
293+
}
294+
295+
return null;
296+
};
297+
222298
// render_sankey: given nodes, flows, and other config, UPDATE THE DIAGRAM:
223299
function render_sankey(nodes_in, flows_in, config_in) {
224300
var graph_width, graph_height, colorset,

0 commit comments

Comments
 (0)