2017-01-11 30 views
0

我正在嘗試使用Highcharts創建蝴蝶圖。我想繪製它作爲 Butterfly Chart使用Highcharts的蝴蝶圖

的代碼如下

// Data gathered from http://populationpyramid.net/germany/2015/ 
$(function() { 
// Age categories 
var categories = ['0-4', '5-9', '10-14', '15-19', 
     '20-24', '25-29', '30-34', '35-39', '40-44', 
     '45-49', '50-54', '55-59', '60-64', '65-69', 
     '70-74', '75-79', '80-84', '85-89', '90-94', 
     '95-99', '100 + ']; 
$(document).ready(function() { 
    Highcharts.chart('container', { 
     chart: { 
      type: 'bar' 
     }, 
     title: { 
      text: 'Population pyramid for Germany, 2015' 
     }, 
     subtitle: { 
      text: 'Source: <a href="http://populationpyramid.net/germany/2015/">Population Pyramids of the World from 1950 to 2100</a>' 
     }, 
     xAxis: [{ 
      categories: categories, 
      reversed: false, 
      labels: { 
       step: 1 
      } 
     }, /*{ // mirror axis on right side 
      opposite: true, 
      reversed: false, 
      categories: categories, 
      linkedTo: 0, 
      labels: { 
       step: 1 
      } 
     }*/], 
     yAxis: { 
      title: { 
       text: null 
      }, 
      labels: { 
       formatter: function() { 
        return Math.abs(this.value) + '%'; 
       } 
      } 
     }, 

     plotOptions: { 
      series: { 
       stacking: 'normal' 
      } 
     }, 

     tooltip: { 
      formatter: function() { 
       return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' + 
        'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0); 
      } 
     }, 

     series: [{ 
      name: 'Male', 
      data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2, 
       -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4, 
       -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0] 
     }, { 
      name: 'Female', 
      data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9, 
       3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9, 
       1.8, 1.2, 0.6, 0.1, 0.0] 
     }] 
    }); 
}); 

}); 

這裏是我的小提琴link,我怎樣才能讓我的X軸標籤在系列之間。請指導我做到這一點。

回答

2

我會處理這不同於摩根一樣,儘管這例子是肯定有用。

我會這樣做的一個標準條形圖,利用多個yAxis對象(每個系列一個),這也允許使用左邊的系列的reversed屬性。

代碼示例:

yAxis: [{ 
     title: { text: null }, 
     width: 200, 
     reversed: true 
    },{ 
     offset: 0, 
     title: { text: null }, 
     left: 300, 
     width: 200 
    }], 
    series: [{ 
     yAxis: 0, 
     data: [...] 
    }, { 
     yAxis: 1, 
     data: [...] 
    }] 

小提琴:

示例輸出:

screenshot

+0

Hi @jlriggs,我們是否可以擁有一個Axis標籤,以便他們可以容納超過10個字符的類別名稱。 Plz檢查這個小提琴[鏈接](http://jsfiddle.net/L80cdxm0/3/) – GeekExplorer

+1

軸標籤可用的空間完全取決於'yAxis'位置設置。如果您更改「寬度」和「左側」設置,則可以根據需要創建儘可能多或較少的空間。此外,如果您將標籤居中,無論是通過CSS還是使用「useHTML」功能,他們都會更好地使用該空間。 – jlbriggs

+1

只強調一下,條形圖中兩軸和列的比例是不同的 - 「數據故事」也是如此,即使是圖表看起來也是如此。儘管如此,這是一張很棒的圖表,它代表了圖片中圖表的清晰度。 – morganfree

2

inverted columnrange圖表結合使用cross-specific-values插件是一種方法。列範圍圖表允許您指定列的位置併爲標籤創建空間。插件將軸移動到圖表的中心。

Highcharts.chart('container', { 
    title: { 
     text: 'Butterfly Chart Example' 
    }, 

    subtitle: { 
     text: '<a href="http://stackoverflow.com">stackoverflow.com</a>' 
    }, 

    chart: { 
     type: 'columnrange', 
     inverted: true, 
     marginTop: 100 
    }, 

    legend: { 
     verticalAlign: 'top', 
     y: 60, 
     x: -25, 
     itemDistance: 50 
    }, 

    xAxis: { 
     categories: ['G7', 'A8', 'V9', 'V4', 'V3', 'V1', 'V5'], 
     crossing: 118, 
     lineWidth: 0, 
     tickLength: 0, 
    }, 

    yAxis: { 
     gridLineWidth: 0, 
     tickInterval: 50, 
     min: 0, 
     max: 250, 
     lineWidth: 1, 
     title: { 
     text: null 
     } 
    }, 

    plotOptions: { 
     series: { 
     grouping: false 
     } 
    }, 

    series: [{ 
     name: 'South', 
     color: 'blue', 
     data: [ 
      [55, 100], 
      [60, 100], 
      [65, 100], 
      [55, 100], 
      [75, 100], 
      [52, 100], 
      [60, 100] 
     ] 
    }, { 
     name: 'North', 
     color: 'orange', 
     data: [ 
     [120, 170], 
     [120, 150], 
     [120, 175], 
     [120, 130], 
     [120, 125], 
     [120, 148], 
     [120, 145] 
     ] 
    }] 

}); 

例如:http://jsfiddle.net/7d4mrhuv/

+0

如你同意,我覺得@jlbriggs答案講述完整的故事和欣賞你的努力給我的幫助,人們像你激勵我採取了這樣不同的任務。非常感謝你:) – GeekExplorer