HTML
CSS
JavaScript
HTML
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Chart.js Bubble Chart</title>
<meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
<meta name="description" content="LightYear是一个基于Bootstrap的后台管理系统的HTML模板。">
<meta name="author" content="yinqi">
<link rel="stylesheet" type="text/css" href="http://lyear.itshubao.com/v4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://lyear.itshubao.com/v4/css/style.min.css">
</head>
  
<body>
<div class="chart-example">
  <div class="tab-content">
    <div class="chart-view"><canvas id="myChart"></canvas></div>
    <div class="chart-actions mt-3"></div>
    <ul class="nav nav-tabs mt-3">
      <li class="nav-item">
        <a class="nav-link active" data-toggle="tab" href="#myChart-options" aria-selected="true">Options</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" data-toggle="tab" href="#myChart-data" aria-selected="false">Data</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" data-toggle="tab" href="#myChart-setup" aria-selected="false">Setup</a>
      </li>
    </ul>
    <div class="tab-content">
      <div class="tab-pane fade show active tab-pane-code" id="myChart-options">
        <pre>
function channelValue(x, y, values) {
  return x < 0 && y < 0 ? values[0] : x < 0 ? values[1] : y < 0 ? values[2] : values[3];
}

function colorize(opaque, context) {
  const value = context.raw;
  const x = value.x / 100;
  const y = value.y / 100;
  const r = channelValue(x, y, [250, 150, 50, 0]);
  const g = channelValue(x, y, [0, 50, 150, 250]);
  const b = channelValue(x, y, [0, 150, 150, 250]);
  const a = opaque ? 1 : 0.5 * value.v / 1000;

  return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
}

const config = {
  type: 'bubble',
  data: data,
  options: {
    aspectRatio: 1,
    plugins: {
      legend: false,
      tooltip: false,
    },
    elements: {
      point: {
        backgroundColor: colorize.bind(null, false),

        borderColor: colorize.bind(null, true),

        borderWidth: function(context) {
          return Math.min(Math.max(1, context.datasetIndex + 1), 8);
        },

        hoverBackgroundColor: 'transparent',

        hoverBorderColor: function(context) {
          return Utils.color(context.datasetIndex);
        },

        hoverBorderWidth: function(context) {
          return Math.round(8 * context.raw.v / 1000);
        },

        radius: function(context) {
          const size = context.chart.width;
          const base = Math.abs(context.raw.v) / 1000;
          return (size / 24) * base;
        }
      }
    }
  }
};
        </pre>
      </div>
      <div class="tab-pane fade tab-pane-code" id="myChart-data">
        <pre>
function generateData() {
  const data = [];
  let i;

  for (i = 0; i < DATA_COUNT; ++i) {
    data.push({
      x: Utils.rand(MIN_XY, MAX_XY),
      y: Utils.rand(MIN_XY, MAX_XY),
      v: Utils.rand(0, 1000)
    });
  }

  return data;
}

const data = {
  datasets: [{
    data: generateData()
  }, {
    data: generateData()
  }]
};
        </pre>
      </div>
      <div class="tab-pane fade tab-pane-code" id="myChart-setup">
        <pre>
const DATA_COUNT = 16;
const MIN_XY = -150;
const MAX_XY = 100;
Utils.srand(110);

const actions = [
  {
    name: 'Randomize',
    handler(chart) {
      chart.data.datasets.forEach(dataset => {
        dataset.data = generateData();
      });
      chart.update();
    }
  },
];
        </pre>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/jquery.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/popper.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/Chart.js/4.1.1/chart.umd.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/luxon/3.2.0/luxon.min.js"></script>
<script type="text/javascript" src="http://libs.itshubao.com/chartjs/utils.js"></script>
</body>
</html>
CSS
body{
  background: #fff;
}
.tab-content .chart-view {
  max-width: 800px;
}
.tab-content .tab-pane-code {
  height: 360px;
  overflow: auto;
}
.tab-content .tab-pane-code pre {
  height: 100%;
  margin: 0px;
}
.chart-example {
  width: calc(100% - 2px);
  border: 1px solid #ececec;
  padding: 10px;
}
JavaScript
const DATA_COUNT = 16;
const MIN_XY = -150;
const MAX_XY = 100;
Utils.srand(110);

const actions = [
  {
    name: 'Randomize',
    handler(chart) {
      chart.data.datasets.forEach(dataset => {
        dataset.data = generateData();
      });
      chart.update();
    }
  },
];

function generateData() {
  const data = [];
  let i;

  for (i = 0; i < DATA_COUNT; ++i) {
    data.push({
      x: Utils.rand(MIN_XY, MAX_XY),
      y: Utils.rand(MIN_XY, MAX_XY),
      v: Utils.rand(0, 1000)
    });
  }

  return data;
}

const data = {
  datasets: [{
    data: generateData()
  }, {
    data: generateData()
  }]
};

function channelValue(x, y, values) {
  return x < 0 && y < 0 ? values[0] : x < 0 ? values[1] : y < 0 ? values[2] : values[3];
}

function colorize(opaque, context) {
  const value = context.raw;
  const x = value.x / 100;
  const y = value.y / 100;
  const r = channelValue(x, y, [250, 150, 50, 0]);
  const g = channelValue(x, y, [0, 50, 150, 250]);
  const b = channelValue(x, y, [0, 150, 150, 250]);
  const a = opaque ? 1 : 0.5 * value.v / 1000;

  return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
}

const config = {
  type: 'bubble',
  data: data,
  options: {
    aspectRatio: 1,
    plugins: {
      legend: false,
      tooltip: false,
    },
    elements: {
      point: {
        backgroundColor: colorize.bind(null, false),

        borderColor: colorize.bind(null, true),

        borderWidth: function(context) {
          return Math.min(Math.max(1, context.datasetIndex + 1), 8);
        },

        hoverBackgroundColor: 'transparent',

        hoverBorderColor: function(context) {
          return Utils.color(context.datasetIndex);
        },

        hoverBorderWidth: function(context) {
          return Math.round(8 * context.raw.v / 1000);
        },

        radius: function(context) {
          const size = context.chart.width;
          const base = Math.abs(context.raw.v) / 1000;
          return (size / 24) * base;
        }
      }
    }
  }
};

const ctx = document.getElementById('myChart');

const chart = new Chart(ctx, config);

$.each(actions, function(index, value) {
    let aHtml = $('<a></a>').addClass('btn btn-default m-r-5').html(value.name);
    $('.chart-actions').append(aHtml);
    aHtml.on('click', function () {
        value.handler(chart);
    });
});