2017-06-23 183 views
0

我對圖表非常陌生,我被卡住了。我有兩個具有相同數據名稱但值不同的餅圖。我試圖實現的是在餅圖片上單擊時從兩個餅圖中獲取值,並在點擊時旋轉餅圖。這裏是我的JSFiddle,我試圖實現的是在下面的圖像URL。比較兩個具有相同數據名稱的餅圖(Highcharts)

Image Link

而且代碼是在這裏

var options = { 
 
    chart: { 
 
    renderTo: 'chart-wrap', 
 
    animation: false, 
 
    type: 'pie', 
 
    backgroundColor: null, 
 
    height: 450, 
 
    events: { 
 
     load: function() { 
 
\t \t \t \t 
 
     } 
 
    } 
 
    }, 
 
    colors: ['#0f5880', '#5db2cb', '#5c6a75', '#8aaeba', '#00b1b5', '#19315a'], 
 
    credits: { 
 
    enable: false 
 
    }, 
 
    title: { 
 
    text: null 
 
    }, 
 
    tooltip: { 
 
    valueSuffix: '%', 
 
    enabled: false, 
 
    formatter: function() { 
 
      
 
    } 
 
    }, 
 
    plotOptions: { 
 
    pie: { 
 
     animation: { 
 
     duration: 500, 
 
     easing: 'easeOutQuad' 
 
     }, 
 
     shadow: false, 
 
     center: ['50%', '50%'], 
 
     cursor: 'pointer', 
 
     dataLabels: { 
 
     enabled: false 
 
     }, 
 
     point: { 
 
     events: { 
 
      click: function() {   \t 
 
      moveToPoint(this); 
 
      selectedInfo(this.name, this.y);    
 
      } 
 
     } 
 
     } 
 
    }, 
 
    series: { 
 
     animation: { 
 
     duration: 750, 
 
     easing: 'easeOutQuad' 
 
     } 
 
    } 
 
    }, 
 
    series: [{ 
 
    data: [{ 
 
     name: 'Financial', 
 
     y: 18.29 
 
     }, 
 
     { 
 
     \t name: 'Information Technology', 
 
     y: 16.66 
 
     }, 
 
     { 
 
     \t name: 'Consumer Discretionary', 
 
     y: 12.31 
 
     }, 
 
     { 
 
     \t name: 'Health Care', 
 
     y: 11.17 
 
     }, 
 
     { 
 
     \t name: 'Industrials', 
 
     y: 10.79 
 
     }, 
 
     { 
 
     \t name: 'Consumer Staples', 
 
     y: 9.49 
 
     }, 
 
     { 
 
     \t name: 'Energy', 
 
     y: 6.43 
 
     }, 
 
     { 
 
     \t name: 'Materials', 
 
     y: 5.28 
 
     }, 
 
     { 
 
     \t name: 'Telecommunication Services', 
 
     y: 3.31 
 
     }, 
 
     { 
 
     \t name: 'Real Estate', 
 
     y: 3.14 
 
     }, 
 
     { 
 
     \t name: 'Utilities', 
 
     y: 3.13  
 
    }], 
 
    startAngle: 50, 
 
    name: 'Pie 1', 
 
    size: '104%', 
 
    innerSize: '75%', 
 
    center: ['20%', '50%'], 
 
    events: { 
 
    \t click: function() { 
 

 
     } 
 
    } 
 
    }, { 
 
    data: [{ 
 
     name: 'Financial', 
 
     y: 39.2 
 
     }, 
 
     { 
 
     \t name: 'Information Technology', 
 
     y: 1.2 
 
     }, 
 
     { 
 
     \t name: 'Consumer Discretionary', 
 
     y: 4.8 
 
     }, 
 
     { 
 
     \t name: 'Health Care', 
 
     y: 7 
 
     }, 
 
     { 
 
     \t name: 'Industrials', 
 
     y: 6.8 
 
     }, 
 
     { 
 
     \t name: 'Consumer Staples', 
 
     y: 7 
 
     }, 
 
     { 
 
     \t name: 'Energy', 
 
     y: 4.1 
 
     }, 
 
     { 
 
     \t name: 'Materials', 
 
     y: 15.5 
 
     }, 
 
     { 
 
     \t name: 'Telecommunication Services', 
 
     y: 3.5 
 
     }, 
 
     { 
 
     \t name: 'Real Estate', 
 
     y: 8.2 
 
     }, 
 
     { 
 
     \t name: 'Utilities', 
 
     y: 2.8  
 
    }], 
 
    startAngle: -90, 
 
    name: 'Pie 2', 
 
    size: '37%', 
 
    innerSize: '55%', 
 
    center: ['80%', '50%'] 
 
    }] 
 
}; 
 

 
var initChart = new Highcharts.Chart(options); 
 

 
var lastAngle = 0; 
 
var moveToPoint = function(clickPoint) { 
 
    var points = clickPoint.series.points; 
 
    var startAngle = 0; 
 
    for (var i = 0; i < points.length; i++) { 
 
    var p = points[i]; 
 
    if (p === clickPoint) { 
 
     break; 
 
    } 
 
    startAngle += (p.percentage/100.0 * 360.0); 
 
    } 
 

 
    var newAngle = -startAngle + 90 - ((clickPoint.percentage/100.0 * 360.0)/2); 
 

 
    $({ 
 
    angle: 0, 
 
    target: newAngle 
 
    }).animate({ 
 
    angle: newAngle - lastAngle 
 
    }, { 
 
    duration: 750, 
 
    easing: 'easeOutQuad', 
 
    step: function() { 
 
     $(this).parents('.highcharts-series').css({ 
 
     transform: 'rotateZ(' + this.angle + 'deg)' 
 
     }); 
 
    }, 
 
    complete: function() { 
 
     $(this).parents('.highcharts-series').css({ 
 
     transform: 'rotateZ(0deg)' 
 
     }); 
 
     clickPoint.series.update({ 
 
     startAngle: newAngle // center at 90  
 
     }); 
 
     lastAngle = newAngle; 
 
    } 
 
    }); 
 
}; 
 

 
var selectedInfo = function(selectName, selectVal) { 
 
    var selectedCategory = $('#selected-slice'); 
 
    setTimeout (function() { 
 
\t \t selectedCategory.text(selectName +', '+ selectVal); 
 
\t }, 1000); 
 
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.highcharts.com/highcharts.js"></script>

JSFiddle

+0

這是否圖表滿足您的要求? https://jsfiddle.net/p2wjwbeo/136/如果是的話,我會發布它作爲答案 –

+0

@GrzegorzBlachliński謝謝,這正是我想要的。但有沒有一種方法可以在加載時顯示第一個切片數據,而不是點擊後顯示?如果有辦法,那將是非常有用的,否則請張貼這個答案。 :) –

回答

0

您應該能夠使用你的系列都使用點更新兩個餅圖。 events.click功能:

point: { 
    events: { 
     click: function() { 
     var chart = this.series.chart, 
      series = chart.series, 
      index = this.index, 
      name = this.name; 
     if (lastIndex >= 0) { 
      Highcharts.each(series, function(s) { 
      s.data[lastIndex].update({ 
       dataLabels: { 
       enabled: false 
       } 
      }); 
      }) 
     } 
     Highcharts.each(series, function(s, i) { 
      s.data[index].update({ 
      dataLabels: { 
       enabled: true 
      } 
      }); 

      moveToPoint(s.data[index], i); 
     }); 
     lastIndex = index; 
     $('.customLabel').remove(); 
     chart.renderer.label(name, chart.chartWidth/2 + 50, 100).attr({ 
      align: 'center', 
      'font-size': 24 
     }).addClass('customLabel').add(); 
     } 
    } 
    } 

你也應該能夠編程火點擊你的圖表負載情況下,使用firePointEvent功能:

var initChart = new Highcharts.Chart(options, function(chart) { 
    chart.series[0].data[0].firePointEvent('click'); 
}); 

活生生的例子: https://jsfiddle.net/p2wjwbeo/138/

+0

非常感謝@GrzegorzBlachliński的幫助。這正是我需要的。 :) –

相關問題