Code Monkey home page Code Monkey logo

Comments (4)

LeeLenaleee avatar LeeLenaleee commented on May 22, 2024

With some carefull own logic with creating the bubbles you could create this already using a bubble chart. But if you want it as an chart type that does that logic for you I think it would suit better as an external chart type and not something for the core

from chart.js.

Offbeatmammal avatar Offbeatmammal commented on May 22, 2024

With some carefull own logic with creating the bubbles you could create this already using a bubble chart. But if you want it as an chart type that does that logic for you I think it would suit better as an external chart type and not something for the core

agreed, it's probably not core. I've got close using the sample I linked, but getting the returned X,Y, and Radius scaled to fit into the canvas is making my head hurt! Sample code here in case it's of use to anyone (and hopefully someone points to where I'm going wrong) - at the moment there's still sometimes gaps or overlay of the circles that I don't see in the jsfiddle, and bubbles get clipped at the edge of the canvas.

 <canvas id="canvas" style="width:450px; height:450px;"></canvas>

// include the calculation code from https://jsfiddle.net/TDzVE/

  var radiuses = [];
  for(var i=0; i<15; i++) {
    radiuses.push(Math.random() * (200) + 1)
  }
    
  var packer = new Packer (radiuses, 1);

  var chart = $("#canvas")

  var margin_factor = 0.1;
  var mx = chart.width() * margin_factor / 2;
  var my = chart.height() * margin_factor / 2;
  dx=Number(packer.w)/2
  dy=Number(packer.h)/2
  var zx = chart.width() / packer.w;
  var zy = chart.height() / packer.h;
  var zoom = zx < zy ? zx : zy;

  datapoints = [];
  for (var i=0; i<packer.list.length;i++) {
    circle = packer.list[i]
    datapoints.push({'x':(circle.c.x+dx)*zoom+mx, 'y':(circle.c.y+dy)*zoom+my, 'r':circle.r*zoom}) 
  }

  const data = {
    datasets: [
      {
        label: 'Dataset 1',
        data: datapoints,
        backgroundColor: "rgba(123,123,123,1)",
      },
    ]
  };

  var myBarChart = new Chart(chart, {
    type: 'bubble',
    data: data,
    options: {
      responsive : false,
      maintainAspectRatio : true,
      layout : {
        autoPadding : false,
      },
      scales: {
                      x: {
                        ticks: {display: false,},
                        grid: { display: false },
                        border: {display: false}
                      },
                      y: {display:false,
                          ticks: {display: false,},
                          grid: { display: false, },
                          border: {display: false}
                        }
                  },    
      plugins: {
        legend: {display: false,},
        title: {display: false,}
      }
    },
  })


from chart.js.

Offbeatmammal avatar Offbeatmammal commented on May 22, 2024

another, clunky, solution using the D3 v3 layout.pack algorithm https://stackoverflow.com/a/76911169/1569675

from chart.js.

jmtimper avatar jmtimper commented on May 22, 2024

Would love this as well

from chart.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.