在Highcharts上显示单个图表

时间:2018-09-07 15:20:33

标签: javascript highcharts

你好,我在Highcharts中的代码有问题,我已经声明了我的系列,但是只显示了一组图表。所有数据都是在var dataStr上设置的,关于如何放置数据,我对要传递的每个数据使用push。所以我认为问题出在我的推动上?

var dataStr = [
  {name: 'JOB-182', problemReportCount: 0, totalCost: 50000, lostHour: 0, lostValue: 0},
  {name: 'JOB-185', problemReportCount: 0, totalCost: 3432.65, lostHour: 0, lostValue: 0},
  {name: 'JOB-188', problemReportCount: 4, totalCost: 5000, lostHour: 0, lostValue: 0},
  {name: 'JOB-189', problemReportCount: 0, totalCost: 1000, lostHour: 0, lostValue: 0}
];

var jRefs = [];
var prJRefs = [];
var prLostVals = [];
var prLostHours = [];
var totCosts = [];
for (var i = 0; i < dataStr.length; i++) {
    var jName = '' + dataStr[i].name;
    if (jName != '') {
        jRefs.push(jName);
        var prCount = dataStr[i].problemReportCount;
        if (prCount > 0) {
            prJRefs.push(jName);
            prLostVals.push(dataStr[i].lostValue);
            prLostHours.push(dataStr[i].lostHour);
            totCosts.push(dataStr[i].totalCost);
        }
    }
}

var genGraph = function() {
    $("#backButton").hide();

    $('#prGraph4').highcharts({
     chart: {
      type: 'column',
      plotBackgroundColor: '#F7F7F7'
     },
     title: {
      text: 'Problem Report'
     },
     subtitle: {
      text: 'Estimated Lost Values'
     },
     xAxis: {
      alternateGridColor: '#fff',
      categories: prJRefs
     },
     yAxis: {
      min: 0,
      title: {
       text: 'Amount'
      },
      stackLabels: {
       enabled: true,
       style: {
        fontWeight: 'bold'
       }
      }
     },
     plotOptions: {
      column: {
       stacking: 'normal',
      }
     },
     tooltip: {
      shared: true,
      formatter: function() {
       var s = '<b>' + this.x + '</b>';
       $.each(this.points, function() {
        var lostValue = this.y;
        s += '<br/><span style=" + dbl + "color:' + this.series.color + ';" + dbl + ">\u25CF</span> ' + this.series.name + ': <b>£</b> ' + lostValue.toFixed(2);
       });
       return s;
      }
     },
    series:[{
      name: 'Total Costs',
      data: totCosts,
      color: '#2ecc71',
      stack: 'total'
     }, {
      name: 'Estimated Lost Materials Value',
      data: prLostVals,
      color: '#e74c3c',
      stack: 'lost'
     }, {
      name: 'Estimated Lost Hours Price',
      data: prLostHours,
      color: '#e67e22',
      stack: 'lost'
     }] 
    });


};

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您只有一个可见列,因为只有一组数据满足此条件:

if (prCount > 0) {
    prJRefs.push(jName);
    prLostVals.push(dataStr[i].lostValue);
    prLostHours.push(dataStr[i].lostHour);
    totCosts.push(dataStr[i].totalCost);
}

此外,系列Estimated Lost Materials ValueEstimated Lost Hours Price中的所有值均为0。

实时演示:http://jsfiddle.net/BlackLabel/vu9ahfk5/