Support for D3.js #2008
falkoschindler
started this conversation in
Ideas / Feature Requests
Replies: 2 comments 2 replies
-
I was looking into this again (D3), and wanted to see if NiceGUI had support for D3 yet. |
Beta Was this translation helpful? Give feedback.
2 replies
-
I'm trying to retrieve in nicegui the features that are hovered over in a map made with d3. The code works, but I'm sure there are other ways of doing it. Suggestions would be appreciated: from nicegui import ui
topojson_data = {
"type": "Topology",
"objects": {
"features": {
"type": "GeometryCollection",
"geometries": [
{"type": "Polygon", "arcs": [[0]], "properties": {"codgeo": "0001"}},
{"type": "Polygon", "arcs": [[1]], "properties": {"codgeo": "0002"}}
]
}
},
"arcs": [
[[0, 0], [100, 0], [0, 100], [-100, 0], [0, -100]], # 1er carré
[[100, 0], [100, 0], [0, 100], [-100, 0], [0, -100]] # 2e carré à droite
],
"transform": {"scale": [0.01, 0.01], "translate": [0, 0]}
}
url_root = 'http://127.0.0.1:8080'
ui.add_head_html('''
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
''')
ui.add_body_html('''
<div id="info" style="position:absolute; top:10px; left:10px; background:white; padding:5px; border:1px solid black;"></div>
<svg width="400" height="200"></svg>
''')
d3 = ui.element()
d3.client.on_connect(lambda: ui.run_javascript(f'''
setTimeout(() => {{
const svg = d3.select("svg");
const width = +svg.attr("width");
const height = +svg.attr("height");
const topo = {topojson_data};
const geojson = topojson.feature(topo, topo.objects.features);
const projection = d3.geoIdentity().reflectY(true).fitSize([width, height], geojson);
const path = d3.geoPath(projection);
svg.selectAll("path")
.data(geojson.features)
.enter().append("path")
.attr("d", path)
.attr("fill", "lightblue")
.attr("stroke", "black")
.on("mouseover", function(event, d) {{
const infoBox = document.getElementById('info');
if (infoBox) {{
infoBox.innerText = "Feature: " + d.properties.codgeo;
}}
fetch('{url_root}/map_hover/' + d.properties.codgeo);
}})
.on("mouseout", function() {{
const infoBox = document.getElementById('info');
if (infoBox) {{
infoBox.innerText = "";
}}
fetch('{url_root}/map_hover/' + '_no_codgeo');
}});
}}, 100);
'''))
selected = ui.label('')
@ui.page('/map_hover/{codgeo}')
def map_over(codgeo):
selected.text = codgeo if codgeo != '_no_codgeo' else ''
ui.run() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been playing around with NiceGUI and D3.js, trying to find out if an integration might be possible. Here is my code:
The JSON file can be downloaded from here: https://observablehq.com/@d3/zoomable-circle-packing
I'm not sure if this is worth the effort. D3.js seems to heavily rely on JavaScript code to create the plot - in contrast to Highcharts or ECharts which primarily use an options dictionary. So we either need do mimic the API in Python (like
ui.scene
orui.leaflet
in PR #1217), or we simply pass a block of JavaScript code to the client like in the example above.Beta Was this translation helpful? Give feedback.
All reactions