javascript - 使用 chart.js 混合图表散点图

标签 javascript charts chart.js

我正在尝试在 chart.js 中创建一个散点图,混合折线图和气泡图,以绘制某些数据的离散度与预测数学模型的“理想”情况。
我正在使用 scales分散 xAxes并画一条直线(代表“理想”)。问题出在气泡图中,气泡的数据不能完全越线,技术上是不可能的(理论上),但它们正好越过“理想”线。
这是我的图表示例代码:

var chart = new Chart(ctx, {
  type: 'bubble',
  data: {
      labels: makeLabels().labels,
      datasets: [
        {
          type: 'line',
          label: 'Data',
          data: makeLabels().labels,
          fill: false,
          backgroundColor: "rgba(218,83,79, .7)",
          borderColor: "rgba(218,83,79, .7)",
          pointRadius: 0
        },
        {
          type: 'bubble',
          label: 'Data2',
          data: makeBubbles(),
          backgroundColor: "rgba(76,78,80, .7)",
          borderColor: "transparent"
        }
      ]
  },
  options: {
    scales: {
      xAxes: [{
        type: 'linear',
        position: 'bottom',
        ticks: {
          min: 0,
          max: Math.max(...makeLabels().array)
        }
     }]
    }
  }
});
here is the complete code使用虚拟数据和 codepen 中的操场。
无论如何,这是实际结果:
enter image description here
这是预期的结果:
enter image description here
线的位置无关紧要(捕获中的图表未使用相同的数据绘制),但我想要的是使气泡散开。
¿知道如何实现它吗?

最佳答案

您的点呈线性出现的原因是因为您在 makeBubbles() 中将每个数据点的 x 和 y 值设置为相同.这导致线性定位的点。

要使点散布,只需使用不属于线性模式的点。为了证明我的意思,我修改了你的 makeBubbles()函数,以便您的 x 和 y 值不相等。现在你得到了一个非常广泛的分散。

arr = arr.map(function(item, i) {
  return {x: item, y:arr[i - 1]}
});

这是一个 example .

关于javascript - 使用 chart.js 混合图表散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841925/

相关文章:

php - 将 PHP 转换为 JavaScript。将键/值存储到数组中

javascript - angularjs中的嵌套表达式

php - 设置临时打印文件的属性

javascript - 如何重新格式化柱形图的 json 输出?

reactjs - React Js 和 Chart Js(折线图)

javascript - Mongoose -> findOne -> 排序 -> 如果数据库中没有记录

d3.js - 使用 dc 或 d3.js 中具有正负 y 轴的面积图制作图表

javascript - 悬停在谷歌图表图例上,工具提示在模态后面

angularjs - 加载 JSON 以显示数据,使用 ng-repeat {ANGULAR.JS}

php - ChartJS 和 Ajax 调用