javascript - Highcharts:如何绘制不同箱线图的相邻散点

标签 javascript highcharts boxplot

Outlier Scatter Points are being merged instead of being shown side by side just like boxes

在上图中,我有两个箱线图系列及其各自的异常值散点。但由于两个系列的数据相同,因此一个箱线图系列的异常值与其他箱线图系列的异常值重叠。我可以实现类似将每个框的离群点恰好位于其下方而不合并到单个点之类的目标吗?也就是说,散点应该并排放置,就像放置盒子一样,并且不应该重叠。

最佳答案

更新

基于已经绘制的箱线图,可以通过动态计算来实现更准确的散点位置。

在加载事件中,您可以抓取箱线图的中间并将该值设置为散点 x 坐标。

 events: {
   load: function() {
        const boxplotSeries = this.series.slice(0, 2);

        const calculateOutlierX = (category, series) => {
          const point = series.data[category];
          const shapeArgs = point.shapeArgs;
          const corr = (point.stem.strokeWidth() % 2) / 2;

        return series.xAxis.toValue(shapeArgs.x + (shapeArgs.width / 2) + series.group.translateX + corr);
      }



        const adjustedOutliers = this.series.slice(2).map((series, i, ser) => {
        return series.data.map(point => [calculateOutlierX(point.x, boxplotSeries[i]), point.y]);
      });

         this.series[2].setData(adjustedOutliers[0], false);
        this.series[3].setData(adjustedOutliers[1], true);
    }
  }

实例:http://jsfiddle.net/sza4odkz/1/

更新前

您需要调整散点的位置。例如,如果您想调整散点以适合左侧箱线图,请从其 x 坐标中减去 0.2。同样,要适合右侧箱线图,请添加 0.2

{
  data: [
    [0 - 0.2, 644],
    [4 - 0.2, 718],
    [4 - 0.2, 951],
    [4 - 0.2, 969]
  ]
}, {
  data: [
    [0 + 0.2, 644],
    [4 + 0.2, 718],
    [4 + 0.2, 951],
    [4 + 0.2, 969]
  ]

实例和输出

http://jsfiddle.net/sza4odkz/

Highcharts.chart('container', {

  chart: {
    type: 'boxplot'
  },

  series: [{
    name: 'Observations',
    data: [
      [760, 801, 848, 895, 965],
      [733, 853, 939, 980, 1080],
      [714, 762, 817, 870, 918],
      [724, 802, 806, 871, 950],
      [834, 836, 864, 882, 910]
    ],
  }, {
    data: [
      [760, 801, 848, 895, 965],
      [733, 853, 939, 980, 1080],
      [714, 762, 817, 870, 918],
      [724, 802, 806, 871, 950],
      [834, 836, 864, 882, 910]
    ]
  }, {
    name: 'Outlier',
    color: Highcharts.getOptions().colors[0],
    type: 'scatter',
    data: [ // x, y positions where 0 is the first category
      [0 - 0.2, 644],
      [4 - 0.2, 718],
      [4 - 0.2, 951],
      [4 - 0.2, 969]
    ]
  }, {
    name: 'Outlier 2',
    color: Highcharts.getOptions().colors[1],
    type: 'scatter',
    data: [ // x, y positions where 0 is the first category
      [0 + 0.2, 644],
      [4 + 0.2, 718],
      [4 + 0.2, 951],
      [4 + 0.2, 969]
    ]
  }]

});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>

<div id="container" style="height: 400px; margin: auto; min-width: 310px; max-width: 600px"></div>

Boxplot

关于javascript - Highcharts:如何绘制不同箱线图的相邻散点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42224403/

相关文章:

javascript - 使用 JS 在对象中选择 #...

javascript - 条形 Highcharts 中 stackLabels 格式化程序中的 X 轴值

r - 可以在 ggplot boxplot 中偏移抖动点吗

python - 如何对箱形图的列名称的部分进行分组?

python - 如何将seaborn箱线图与具有数组值的数据框一起使用

javascript - AmCharts 4 - drill map - 获取国家iso代码

javascript - 使用 Keen 时,使用 JavaScript 以每月为间隔的绝对时间范围查询不会按预期拆分

javascript - 预加载器不预加载并第二次加载页面

javascript - 在这个 highcharts 示例中控制 x 轴值的是什么?

javascript - 文档模式 IE8 中的奇怪行为 Highcharts 饼图