【问题标题】:javascript graph library for percentage bar [closed]百分比栏的javascript图形库[关闭]
【发布时间】:2014-08-26 00:40:39
【问题描述】:

我正在使用 Google 图表以图形格式表示我的数据。但我需要以如下格式显示其中一个图形。从技术上讲,它类似于饼图,但呈直线。

你们知道我可以用来获取这种格式的任何 javascript 库吗?

谢谢

【问题讨论】:

  • 这个叫做堆积条形图,很多库都支持,google一下就可以了。我知道 highcharts 实现了它们。
  • 如果您使用 Visualization API BarCharts,您可以将isStacked 选项设置为true 以获得类似的效果。
  • 感谢您的帮助....我们无法通过条形堆叠图给出像上面这样的视觉结果。

标签: javascript graph google-visualization data-visualization


【解决方案1】:

您的典型条形图与您想要的不相似,但使用数据和图表选项可以达到非常相似的效果:

  google.setOnLoadCallback(drawChart);
  function drawChart() {

      //your data in kbytes
      var data = {Free: 226480000, Audio: 6300, Movies: 48100, Photos: 1300000, Apps: 9120000, Backups: 0, Other: 13190000 }


      var sorted_data=[]; // seems you want it sorted by lowest to highest
      var total=0; // total space

      for(var key in data){
          total+=data[key];
          if(key != 'Free'){
              sorted_data.push([key + ' '+  bytesToSize(data[key]), {v:data[key], f:bytesToSize(data[key]) }]) // add the label we will use and the size for the bar
          }
      }

      sorted_data.sort(function(a,b){return a[1]-b[1]}) // sort by size, lowest to highest

      sorted_data.push(['Free' + bytesToSize(data.Free), data.Free]) // add free space to the end of the array

      var chart_data=[['Machine'], ['']];



      for(var i=0;i<sorted_data.length;i++){
          chart_data[0].push(sorted_data[i][0]); // add the label
          chart_data[1].push(sorted_data[i][1]); // add the value
      }


      var title = 'Macintosh HD        ' + bytesToSize(data.Free) + ' free out of ' + bytesToSize(total); // create title (if you want to have one part on the right and the other on left, add it with html and put title='')


    chart_data = google.visualization.arrayToDataTable(chart_data);



      var options = {
      title: title,
        isStacked: true,
        legend:{position:'bottom'}, // put legends on bottom
        hAxis: {gridlines:{color:'transparent'}, ticks:[]}, // remove gridlines and ticks
        height: 200, // little height, we don't need much
        series:{}

    };
      var cols=chart_data.getNumberOfColumns()-2;
      options.series[cols]={color:'transparent', visibleInLegend: false} // put free space as transparent

    var chart = new google.visualization.BarChart(document.getElementById('chart_div'));

    chart.draw(chart_data, options);
  }

function bytesToSize(kbytes) { //function that translates kbytes to the fortmat you wanted
   var bytes=kbytes*1000;
   if(bytes == 0) return '0 Byte';
   var k = 1000;
   var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
   var i = Math.floor(Math.log(bytes) / Math.log(k));
   return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
}

工作小提琴:http://jsfiddle.net/byneqLhm/1/

【讨论】:

    【解决方案2】:

    设法解决了http://jsfiddle.net/vivekbhusal/3n78tc8p/ @juvian 给出的解决方案也很好

              google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['subscribers', 'administrators', 'Authors','Publishers'],
              [120,  2, 0, 5],
            ]);
    
            var options = {
                isStacked: true,
          title: '',
          vAxis: {
                textPosition : 'none',
            title: '',
            gridlines : {
              count : 1,
              color: 'white'
            }
          },
          hAxis: {
            title: '',
            format : '#%',
           'gridlines':{
                'color':'transparent',
                },
                'baselineColor':'transparent',
                'textStyle':{
                'fontSize':'11',
                }
            },
          colors: ['#be1e2d', '#74b749', '#0daed3', '#ffb400', '#f63131'],
          legend : {
            position: 'bottom'
          }
        };
    
            var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
    
    
                var view = new google.visualization.DataView(data);
    view.setColumns([0, {
        type: 'number',
        label: data.getColumnLabel(1),
        calc: function (dt, row) {
            var val = dt.getValue(row, 1);
            for (var i = 1, total = 0, cols = dt.getNumberOfColumns(); i < cols; i++) {
                total += dt.getValue(row, i);
            }
            var percent = val / total;
            // what you do here depends on how you want to display the data in the tooltips
    
            // option 1: use the value of the data point:
            return {v: percent, f: dt.getFormattedValue(row, 1)};
    
    
            // option 2: use the percent:
            return {v: percent, f: (percent * 100).toFixed(2) + '%'};
        }
    }, {
        type: 'number',
        label: data.getColumnLabel(2),
        calc: function (dt, row) {
            var val = dt.getValue(row, 2);
            for (var i = 1, total = 0, cols = dt.getNumberOfColumns(); i < cols; i++) {
                total += dt.getValue(row, i);
            }
            var percent = val / total;
    
            return {v: percent, f: dt.getFormattedValue(row, 2)};
    
            return {v: percent, f: (percent * 100).toFixed(2) + '%'};
        }
    }, {
        type: 'number',
        label: data.getColumnLabel(3),
        calc: function (dt, row) {
            var val = dt.getValue(row, 3);
            for (var i = 1, total = 0, cols = dt.getNumberOfColumns(); i < cols; i++) {
                total += dt.getValue(row, i);
            }
            var percent = val / total;
    
            return {v: percent, f: dt.getFormattedValue(row, 3)};
    
            return {v: percent, f: (percent * 100).toFixed(2) + '%'};
        }
    }]);
            chart.draw(view, options);
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多