2017-10-04 29 views
0

我是使用highcharts.js的新手。我想使用json文件或網址中的數據創建活動量表圖表。我已經理解他們如何繪製圖表,但未能理解json中用於顯示圖表的數據格式。 這裏是我的代碼使用來自文件的json數據的highchart活動測量圖表

var options = { 
 

 
      chart: { 
 
       type: 'solidgauge', 
 
       marginTop: 50 
 
      }, 
 

 
      title: { 
 
       text: 'Activity', 
 
       style: { 
 
        fontSize: '24px' 
 
       } 
 
      }, 
 

 
      tooltip: { 
 
       borderWidth: 0, 
 
       backgroundColor: 'none', 
 
       shadow: false, 
 
       style: { 
 
        fontSize: '16px' 
 
       }, 
 
       pointFormat: '{series.name}<br><span style="font-size:2em; color: {point.color}; font-weight: bold">{point.y}%</span>', 
 
       positioner: function (labelWidth, labelHeight) { 
 
        return { 
 
         x: 200 - labelWidth/2, 
 
         y: 180 
 
        }; 
 
       } 
 
      }, 
 

 
      pane: { 
 
       startAngle: 0, 
 
       endAngle: 360, 
 
       background: [{ // Track for Move 
 
        outerRadius: '112%', 
 
        innerRadius: '88%', 
 
        backgroundColor: Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0.3).get(), 
 
        borderWidth: 0 
 
       }, { // Track for Exercise 
 
        outerRadius: '87%', 
 
        innerRadius: '63%', 
 
        backgroundColor: Highcharts.Color(Highcharts.getOptions().colors[1]).setOpacity(0.3).get(), 
 
        borderWidth: 0 
 
       }, { // Track for Stand 
 
        outerRadius: '62%', 
 
        innerRadius: '38%', 
 
        backgroundColor: Highcharts.Color(Highcharts.getOptions().colors[2]).setOpacity(0.3).get(), 
 
        borderWidth: 0 
 
       }] 
 
      }, 
 

 
      yAxis: { 
 
       min: 0, 
 
       max: 100, 
 
       lineWidth: 0, 
 
       tickPositions: [] 
 
      }, 
 

 
      plotOptions: { 
 
       solidgauge: { 
 
        borderWidth: '34px', 
 
        dataLabels: { 
 
         enabled: false 
 
        }, 
 
        linecap: 'round', 
 
        stickyTracking: false 
 
       } 
 
      }, 
 

 
      series: [] 
 
     }; 
 

 
    var gauge1; 
 
    $.getJSON('bryan.json', function(json){ 
 
     console.log(json) 
 
     options.chart.renderTo = 'container'; 
 
     options.series.data = json 
 
     gauge1 = new Highcharts.Chart(options); 
 
    }); 
 
     /** 
 
     * In the chart load callback, add icons on top of the circular shapes 
 
     */ 
 
     function callback() 
 
    { 
 

 
     // Move icon 
 
     this.renderer.path(['M', -8, 0, 'L', 8, 0, 'M', 0, -8, 'L', 8, 0, 0, 8]) 
 
      .attr({ 
 
       'stroke': '#ffffff', 
 
       'stroke-linecap': 'round', 
 
       'stroke-linejoin': 'round', 
 
       'stroke-width': 2, 
 
       'zIndex': 10 
 
      }) 
 
      .translate(190, 26) 
 
      .add(this.series[2].group); 
 

 
     // Exercise icon 
 
     this.renderer.path(['M', -8, 0, 'L', 8, 0, 'M', 0, -8, 'L', 8, 0, 0, 8, 'M', 8, -8, 'L', 16, 0, 8, 8]) 
 
      .attr({ 
 
       'stroke': '#ffffff', 
 
       'stroke-linecap': 'round', 
 
       'stroke-linejoin': 'round', 
 
       'stroke-width': 2, 
 
       'zIndex': 10 
 
      }) 
 
      .translate(190, 61) 
 
      .add(this.series[2].group); 
 

 
     // Stand icon 
 
     this.renderer.path(['M', 0, 8, 'L', 0, -8, 'M', -8, 0, 'L', 0, -8, 8, 0]) 
 
      .attr({ 
 
       'stroke': '#ffffff', 
 
       'stroke-linecap': 'round', 
 
       'stroke-linejoin': 'round', 
 
       'stroke-width': 2, 
 
       'zIndex': 10 
 
      }) 
 
      .translate(190, 96) 
 
      .add(this.series[2].group); 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="container" style="width: 400px; height: 400px; margin: 0 auto"> 
 
</div>

這裏是我thout可能被渲染的json數據,但它因此未。 data.json

+0

專爲任何人誰願意來看待這個plunkr:https://plnkr.co/edit/lyWQClG2nuPbtmkxg0lt,JSON數據目前加載到1個系列,3點。沒有任何運氣的情況下嘗試了3分,得到1分。 – ewolden

回答

0

首先,您需要創建第一個系列,然後使用您的數據填充其數據陣列,而不是options.series.data = json。另外,在每個點設置不同的radiusinnerRadius屬性。看看下面的例子。

API參考:
http://api.highcharts.com/highcharts/series.solidgauge.data.radius
http://api.highcharts.com/highcharts/series.solidgauge.data.innerRadius

例子:
http://jsfiddle.net/x3cne1ng/

+0

非常感謝@d_paul的解決方案..但是,我仍然如何將系列名稱從系列1更改爲json文件中的名稱屬性。當我嘗試在同一頁面上使用不同的數據創建更多的活動規範,但仍然來自同一個json文件時,highcharts對我來說也很複雜。 –

+0

沒問題。我把所有的點放在一個系列中,這就是爲什麼系列的名字沒有改變的原因(他們是點名)。所以如果你想把點分成不同的系列,你需要稍微修改一下JSON對象。在這裏你可以找到一個獨立系列的例子:http://jsfiddle.net/g2evz0co/。至於多個圖表,請看這裏:http://jsfiddle.net/z9m0cv81/。如有任何問題,請隨時詢問。希望它會有所幫助。 –

+1

是的。它真的很有幫助。其實我盡我所能,我也想出了這個[https://plnkr.co/edit/Pu0qNi959KlKOdr0ut1f?p=preview](https://plnkr.co/edit/Pu0qNi959KlKOdr0ut1f?p=preview)。雖然不是完全符合我想要的,但是每個代表自己的數據的計量器都有。非常感謝你的幫助@d_paul –

相關問題