2015-02-24 368 views
1

是否有方法可以在BoxPlot图表中添加另一条线来表示平均值(平均值)?这看起来像中线将在下面捣鼓Highcharts将平均线添加到BoxPlot图表

在自动绘制你

http://jsfiddle.net/tLucL6mq/3/
小提琴代码:

$('#container').highcharts({ 

    chart: { 
     type: 'boxplot', 
     inverted: true 
    }, 

    title: { 
     text: 'Sample Base Salary Range' 
    }, 

    legend: { 
     enabled: false 
    }, 

    xAxis: { 
     categories: ['4', '3', '2', '1', '0'], 
     title: { 
      text: 'Job Level' 
     } 
    }, 

    yAxis: { 
     title: { 
      text: 'Base Salary' 
     } 
    }, 

    plotOptions: { 
     boxplot: { 
      fillColor: '#F0F0E0', 
      lineWidth: 2, 
      medianColor: '#0C5DA5', 
      medianWidth: 3 
     } 
    }, 

    series: [{ 
     name: "Observation Data", 
     data: [ 
      ['0', 68, 75, 79, 82, 84, 89], //level 4 - category 0 
      ['1', 53, 63, 68, 72, 75, 79], //level 3 - category 1 
      ['2', 47, 52, 59, 64, 67, 68], //level 2 - category 2 
      ['3', 35, 37, 39, 42, 46, 51], //level 1 - category 3 
      ['4', 32, 33, 34, 38, 40, 45] //level 0 - category 4 
     ], 
     tooltip: { 
      headerFormat: '<b>{point.series.name}</b><br/><em>Job Level: {point.x}</em><br/>', 
      pointFormat: '- Max: {point.high}<br/>' + 
          '- Q3: {point.q3}<br/>' + 
          '- Median: {point.median}<br/>' + 
          '- Q1: {point.q1}<br/>' + 
          '- Min: {point.low}<br/>' 
     } 
    }, { 
     name: "Outlier Data", 
     color: Highcharts.getOptions().colors[0], 
     type: 'scatter', 
     data: [ // x, y positions where 0 is the first category 
      [0, 74], 
      [1, 67], 
      [4, 40], 
      [4, 48] 
     ], 
     marker: { 
      fillColor: 'yellow', 
      lineWidth: 2, 
      lineColor: '#0C5DA5' 
     }, 
     tooltip: { 
      headerFormat: '<b>{point.series.name}</b><br/><em>Job Level: {point.x}</em><br/>', 
      pointFormat: 'Base Salary: {point.y}' 
     } 
    }] 

}); 

我不是在文档看到如何做到这一点?这可能吗?

此外,在示例中,当您悬停boxplot的任何部分时,工具提示弹出。 当鼠标悬停中间或其中一个四分位数时,是否有方法显示工具提示,以便它只显示数据点的信息?
我在想这与散点图系列工具提示如何显示单个点的数据相似。


编辑:
我终于得到了我周围的语法头在点对象,而不是值的每个数据集的数组传递。获得该对象语法正确后,我为mean添加了一个新属性,并将该值包含在工具提示中。

http://jsfiddle.net/tLucL6mq/4/

series: [{ 
     name: "Observation Data", 
     data: [ 
{x: '0', low: 68, q1: 75, median: 79, q3: 82, high: 84, mean: 77.6}, //level 4 - category 0 
{x: '1', low: 53, q1: 63, median: 68, q3: 72, high: 75, mean: 66.2}, //level 3 - category 1 
{x: '2', low: 47, q1: 52, median: 59, q3: 64, high: 67, mean: 57.8}, //level 2 - category 2 
{x: '3', low: 35, q1: 37, median: 39, q3: 42, high: 46, mean: 39.8}, //level 1 - category 3 
{x: '4', low: 32, q1: 33, median: 34, q3: 38, high: 40, mean: 35.4} //level 0 - category 4 
     ], 
     tooltip: { 
      headerFormat: '<b>{point.series.name}</b><br/><em>Job Level: {point.x}</em><br/>', 
      pointFormat: '- Min: {point.low}<br/>' + 
          '- Q1: {point.q1}<br/>' + 
          '- Median: {point.median}<br/>' + 
          '- Q3: {point.q3}<br/>' + 
          '- Max: {point.high}<br/>' + 
          '- Mean: {point.mean}<br/>' 
     } 
    } 

这几乎是我想要的东西,但理想的,我想看到每个数据集就像median线是框中显示的mean线。

+0

如何计算数据的平均值? – void 2015-02-24 21:33:30

+0

如果它可以设置在点对象上,就像在[docs](http://www.highcharts.com/docs/chart)中设置低,高,q1,q3和中位数一样, -and系列类型/箱线图系列)。但是,因为它看起来并不是内置选项,所以我正在寻找另一种方法来添加它。 – Stephen 2015-02-25 03:02:58

+0

没有我问的是,您有4个数据集,那么您将如何计算平均值并且是平均值整个图? – void 2015-02-25 07:10:20

回答

4

在一般情况下,它不支持添加这样的行,但它是Highcharts,这样可以延长默认功能:http://jsfiddle.net/tLucL6mq/5/

简单的片断:

(function (H) { 

    // when changing point format for boxplot, values will calculate automatically 
    H.seriesTypes.boxplot.prototype.pointArrayMap = ['low', 'q1', 'median', 'q3', 'high', 'mean']; 

    H.seriesTypes.boxplot.prototype.toYData = function (point) { 
     return [point.low, point.q1, point.median, point.q3, point.high, point.mean]; 
    }; 

    // draw lines after default drawPoints is called: 
    H.wrap(H.seriesTypes.boxplot.prototype, 'drawPoints', function (p) { 
     p.call(this); 
     var chart = this.chart, 
      r = chart.renderer, 
      xAxis = this.xAxis, 
      yAxis = this.yAxis, 
      x, y; 

     // for each point, draw line: 
     H.each(this.points, function (p, i) { 
      x = p.shapeArgs.x; 
      y = p.meanPlot; 

      if (p.meanPlotX) { 
       // update existing path: 
       p.meanPlotX.attr({ 
        d: ['M', x, y, 'L', x + p.shapeArgs.width, y] 
       }); 
      } else { 
       // create mean-line for the first time 
       p.meanPlotX = r.path(['M', x, y, 'L', x + p.shapeArgs.width, y]).attr({ 
        stroke: 'black', 
         'stroke-width': 2 
       }).add(p.series.group); 
      } 

     }); 
    }); 
})(Highcharts) 

附加:

由于前两种方法(pointArrayMaptoYData),它将与您以前的格式[x, y1, y2, y3, y4, y5, y6]一起工作,当然如果y6 =平均值:http://jsfiddle.net/tLucL6mq/7/

+0

@ [Pawel Fus] - 谢谢。这是一个非常有用的例子。 – Stephen 2015-02-25 15:13:25

+0

你知道一种获取数据集中每个“y”值的工具提示的方法吗?意思是如果我把鼠标悬停在median/mean/q1/etc上,我能得到一个只有'y'值的工具提示(就好像它是分散系列中的一个点)? – Stephen 2015-02-25 15:19:27

+0

我认为你不能 - 这是一点,工具提示是按照每个点显示的,而不是每个部分的重点。 – 2015-02-25 15:30:07