javascript - 任何人都可以指导我使用 js 或 python 组合可视化吗?

标签 javascript python reactjs css-animations visualization


// set the dimensions and margins of the graph
var width = 460
var height = 460

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .append("svg")
    .attr("width", width)
    .attr("height", height)

// Read data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/11_SevCatOneNumNestedOneObsPerGroup.csv", function(data) {

  // Filter a bit the data -> more than 1 million inhabitants
  data = data.filter(function(d){ return d.value>10000000 })

  // Color palette for continents?
  var color = d3.scaleOrdinal()
    .domain(["Asia", "Europe", "Africa", "Oceania", "Americas"])
    .range(d3.schemeSet1);

  // Size scale for countries
  var size = d3.scaleLinear()
    .domain([0, 1400000000])
    .range([7,55])  // circle will be between 7 and 55 px wide

  // create a tooltip
  var Tooltip = d3.select("#my_dataviz")
    .append("div")
    .style("opacity", 0)
    .attr("class", "tooltip")
    .style("background-color", "white")
    .style("border", "solid")
    .style("border-width", "2px")
    .style("border-radius", "5px")
    .style("padding", "5px")

  // Three function that change the tooltip when user hover / move / leave a cell
  var mouseover = function(d) {
    Tooltip
      .style("opacity", 1)
  }
  var mousemove = function(d) {
    Tooltip
      .html('<u>' + d.key + '</u>' + "<br>" + d.value + " inhabitants")
      .style("left", (d3.mouse(this)[0]+20) + "px")
      .style("top", (d3.mouse(this)[1]) + "px")
  }
  var mouseleave = function(d) {
    Tooltip
      .style("opacity", 0)
  }

  // Initialize the circle: all located at the center of the svg area
  var node = svg.append("g")
    .selectAll("circle")
    .data(data)
    .enter()
    .append("circle")
      .attr("class", "node")
      .attr("r", function(d){ return size(d.value)})
      .attr("cx", width / 2)
      .attr("cy", height / 2)
      .style("fill", function(d){ return color(d.region)})
      .style("fill-opacity", 0.8)
      .attr("stroke", "black")
      .style("stroke-width", 1)
      .on("mouseover", mouseover) // What to do when hovered
      .on("mousemove", mousemove)
      .on("mouseleave", mouseleave)
      .call(d3.drag() // call specific function when circle is dragged
           .on("start", dragstarted)
           .on("drag", dragged)
           .on("end", dragended));

  // Features of the forces applied to the nodes:
  var simulation = d3.forceSimulation()
      .force("center", d3.forceCenter().x(width / 2).y(height / 2)) // Attraction to the center of the svg area
      .force("charge", d3.forceManyBody().strength(.1)) // Nodes are attracted one each other of value is > 0
      .force("collide", d3.forceCollide().strength(.2).radius(function(d){ return (size(d.value)+3) }).iterations(1)) // Force that avoids circle overlapping

  // Apply these forces to the nodes and update their positions.
  // Once the force algorithm is happy with positions ('alpha' value is low enough), simulations will stop.
  simulation
      .nodes(data)
      .on("tick", function(d){
        node
            .attr("cx", function(d){ return d.x; })
            .attr("cy", function(d){ return d.y; })
      });

  // What happens when a circle is dragged?
  function dragstarted(d) {
    if (!d3.event.active) simulation.alphaTarget(.03).restart();
    d.fx = d.x;
    d.fy = d.y;
  }
  function dragged(d) {
    d.fx = d3.event.x;
    d.fy = d3.event.y;
  }
  function dragended(d) {
    if (!d3.event.active) simulation.alphaTarget(.03);
    d.fx = null;
    d.fy = null;
  }

})

</script>

可以在此处找到此可视化的链接:https://www.d3-graph-gallery.com/graph/circularpacking_template.html

我的目标是在圆环图的孔内设置此可视化效果。我无休止地通过文档和谷歌搜索 python 和 js 进行搜索。我想对此使用 React,因为我认为这会让事情变得更容易,甚至使用 python,但我认为 JS 为这样的目标提供了更好的资源。我需要帮助的是弄清楚如何将此可视化放置在圆环图的孔中。如果有人有任何信息可以让我走上正确的道路或快速解释,我将不胜感激。我在这里绞尽脑汁!预先感谢您。

最佳答案

我不确定我是否得到它,但我会尽力提供帮助。

Python 有很多功能并且用途广泛,甚至还有一些用于可视化的库。然而,它对图表类型有限制。 另一方面,你有 Javascript,它有很多用于网络和友好绘图的功能。您还可以自定义很多您需要的内容。

我认为您需要考虑图表和图表的主要目的是什么。如果你需要的是简单而客观的东西,Python 将是最好的选择。 但如果您需要更“奇特”、定制、带有动画并且对一般用户友好的东西,Javascript 是最合适的。 您还可以在互联网上找到大量代码示例,即使使用 React,它们也可以在开始时为您提供帮助。

我的建议:后端处理数据用Python,可视化用JS。

希望这能有所帮助!

关于javascript - 任何人都可以指导我使用 js 或 python 组合可视化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59993876/

相关文章:

javascript - 正则表达式 -/\w\b\w/

javascript - 使用mooTools获取div的大小

python - 使用 ibm-cos-sdk 时出现意外的关键字参数 'ibm_api_key_id'?

unit-testing - 在 React 中使用 setTimeout() 并测试它

javascript - Internet Explorer 支持 XPATH 吗?

javascript - 使用 jQuery 垂直居中 div

Python/Numpy : How do I renumber an existing array with repeating instances?

python - 从 txt 文件读取向量并排序

javascript - 如何正确处理双嵌套数组(ReactJS + Redux)中的状态?

node.js - 无法使用 Promise all 读取未定义的属性 'map'