2017-10-04 23 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 –

相关问题