2013-12-19 62 views
1

請幫忙。一直在我的頭靠在牆上這一個星期,嘗試我可以找到的每一個選項,但沒有運氣...將json數據加載到與多個系列的highcharts

我正在使用HighCharts與C#/ Razor/WebMatrix網站。試圖創建一個堆積柱形圖 - 讓我的類別動態加載,但無法加載系列。

我是新來的Javascript和一般的網絡編程,所以請善待!

的JSON返回的數據是:

["Clubs","1850","2600","5600","4950","8535","6050","8640", 
"Fesitvals","0","0","1000","750","0","1500","3250", 
"Private","650","2300","1600","2500","0","400","900", 
"Weddings","0","1400","3600","0","0","0","0", 
"Other Income","0","0","70","540","50","0","0"] 

我試圖加載之前的一系列數據繪製圖表... 以下是格式化整個腳本等

腳本處理和加載Json數據集的函數彈出(請參閱粗體/斜體)。 圖看起來不錯,如果硬編碼的序列數據,但由於該網站是數據庫驅動和數據定期更換....

下面是腳本:

$(function() { 
    var ChartType = document.getElementById("CType").value; 
    var SelectMember = document.getElementById("SMbmr").value; 
    Highcharts.theme = { 

     chart: { 
      backgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
      [0, 'rgb(0, 0, 0'], 
      [1, 'rgb(0, 0, 0)'] 
     ] 
      }, 
      borderWidth: 0, 
      borderRadius: 15, 
      plotBackgroundColor: null, 
      plotShadow: false, 
      plotBorderWidth: 0 
     }, 
     title: { 
      style: { 
       color: '#FFF', 
       font: '16px Lucida Grande, Lucida Sans Unicode, Verdana, Arial, Helvetica, sans-serif' 
      } 
     }, 
     subtitle: { 
      style: { 
       color: '#DDD', 
       font: '12px Lucida Grande, Lucida Sans Unicode, Verdana, Arial, Helvetica, sans-serif' 
      } 
     }, 
     xAxis: { 
      gridLineWidth: 0, 
      lineColor: '#999', 
      tickColor: '#999', 
      labels: { 
       style: { 
        color: '#999', 
        fontWeight: 'bold' 
       } 
      }, 
      title: { 
       style: { 
        color: '#AAA', 
        font: 'bold 12px Lucida Grande, Lucida Sans Unicode, Verdana, Arial, Helvetica, sans-serif' 
       } 
      } 
     }, 
     yAxis: { 
      alternateGridColor: null, 
      minorTickInterval: null, 
      gridLineColor: 'rgba(255, 255, 255, .1)', 
      minorGridLineColor: 'rgba(255,255,255,0.07)', 
      lineWidth: 0, 
      tickWidth: 0, 
      labels: { 
       style: { 
        color: '#999', 
        fontWeight: 'bold' 
       } 
      }, 
      title: { 
       style: { 
        color: '#AAA', 
        font: 'bold 12px Lucida Grande, Lucida Sans Unicode, Verdana, Arial, Helvetica, sans-serif' 
       } 
      } 
     }, 
     legend: { 
      itemStyle: { 
       color: '#CCC' 
      }, 
      itemHoverStyle: { 
       color: '#FFF' 
      }, 
      itemHiddenStyle: { 
       color: '#333' 
      } 
     }, 
     labels: { 
      style: { 
       color: '#CCC' 
      } 
     }, 
     tooltip: { 
      backgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
      [0, 'rgba(96, 96, 96, .8)'], 
      [1, 'rgba(16, 16, 16, .8)'] 
     ] 
      }, 
      borderWidth: 0, 
      style: { 
       color: '#FFF' 
      } 
     }, 


     plotOptions: { 
      series: { 
       shadow: true 
      }, 
      line: { 
       dataLabels: { 
        color: '#CCC' 
       }, 
       marker: { 
        lineColor: '#333' 
       } 
      }, 
      spline: { 
       marker: { 
        lineColor: '#333' 
       } 
      }, 
      scatter: { 
       marker: { 
        lineColor: '#333' 
       } 
      }, 
      candlestick: { 
       lineColor: 'white' 
      } 
     }, 

     toolbar: { 
      itemStyle: { 
       color: '#CCC' 
      } 
     }, 

     navigation: { 
      buttonOptions: { 
       symbolStroke: '#DDDDDD', 
       hoverSymbolStroke: '#FFFFFF', 
       theme: { 
        fill: { 
         linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
         stops: [ 
        [0.4, '#606060'], 
        [0.6, '#333333'] 
       ] 
        }, 
        stroke: '#000000' 
       } 
      } 
     }, 

     // scroll charts 
     rangeSelector: { 
      buttonTheme: { 
       fill: { 
        linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
        stops: [ 
       [0.4, '#888'], 
       [0.6, '#555'] 
      ] 
       }, 
       stroke: '#000000', 
       style: { 
        color: '#CCC', 
        fontWeight: 'bold' 
       }, 
       states: { 
        hover: { 
         fill: { 
          linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
          stops: [ 
        [0.4, '#BBB'], 
        [0.6, '#888'] 
        ] 
         }, 
         stroke: '#000000', 
         style: { 
          color: 'white' 
         } 
        }, 
        select: { 
         fill: { 
          linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
          stops: [ 
        [0.1, '#000'], 
        [0.3, '#333'] 
        ] 
         }, 
         stroke: '#000000', 
         style: { 
          color: 'yellow' 
         } 
        } 
       } 
      }, 
      inputStyle: { 
       backgroundColor: '#333', 
       color: 'silver' 
      }, 
      labelStyle: { 
       color: 'silver' 
      } 
     }, 

     navigator: { 
      handles: { 
       backgroundColor: '#666', 
       borderColor: '#AAA' 
      }, 
      outlineColor: '#CCC', 
      maskFill: 'rgba(16, 16, 16, 0.5)', 
      series: { 
       color: '#7798BF', 
       lineColor: '#A6C7ED' 
      } 
     }, 

     scrollbar: { 
      barBackgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
       [0.4, '#888'], 
       [0.6, '#555'] 
      ] 
      }, 
      barBorderColor: '#CCC', 
      buttonArrowColor: '#CCC', 
      buttonBackgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
       [0.4, '#888'], 
       [0.6, '#555'] 
      ] 
      }, 
      buttonBorderColor: '#CCC', 
      rifleColor: '#FFF', 
      trackBackgroundColor: { 
       linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, 
       stops: [ 
      [0, '#000'], 
      [1, '#333'] 
     ] 
      }, 
      trackBorderColor: '#666' 
     }, 

     // special colors for some of the demo examples 
     legendBackgroundColor: 'rgba(48, 48, 48, 0.8)', 
     legendBackgroundColorSolid: 'rgb(70, 70, 70)', 
     dataLabelsColor: '#444', 
     textColor: '#E0E0E0', 
     maskColor: 'rgba(255,255,255,0.3)' 
    }; 

    // Apply the theme 
    var highchartsOptions = Highcharts.setOptions(Highcharts.theme); 
    var CatName = new Array(); 
    var DataSeries = new Array(); 
    var currentDate = new Date(); 
    var currentYear = currentDate.getFullYear(); 
    for (var i = 2007; i <= currentYear; i++) { 
     CatName.push(i.toString()); 
    } 
    ***$.getJSON('/Admin/Accounting/ChartJSon/' + ChartType + '/0', function (DataSet) { 
      options.series = DataSet; 
    });*** 
    $('#container').highcharts(
     { 

      chart: 
      { 
       renderTo: 'container', 
       defaultSeriesType: 'column', 
       plotBorderWidth: 1, 
       plotBorderColor: '#3F4044' 
      }, 
      credits: { enabled: false }, 
      title: 
      { 
       text: 'Sales by Type and Year' 
      }, 

      xAxis: 
      { 
       categories: CatName 
      }, 

      yAxis: 
      { 
       allowDecimals: false, 
       min: 0, 
       title: 
       { 
        text: 'Sales' 
       } 
      }, 

      tooltip: 
      { 
       formatter: function() { 
        return '<b>' + this.x + '</b><br/>' + 
         this.series.name + ': ' + this.y + '<br/>' + 
         'Total: ' + this.point.stackTotal; 
       } 
      }, 

      plotOptions: 
      { 
       column: 
       { 
        stacking: 'normal' 
       } 
      }, 

      series: [] 
     }); 

}); 
+0

您有這方面的JSFiddle嗎? http://jsfiddle.net/ –

回答

0

嗯,我在這裏看到了一些問題。首先嚐試用下面提到的代碼替換高位代碼。因爲您需要爲系列選項中的每個系列指定數據。並提供唯一的名稱。

$('#container').highcharts(
       { 

        chart: 
        { 
         renderTo: 'container', 
         defaultSeriesType: 'column', 
         plotBorderWidth: 1, 
         plotBorderColor: '#3F4044' 
        }, 
        credits: { enabled: false }, 
        title: 
        { 
         text: 'Sales by Type and Year' 
        }, 

        xAxis: 
        { 
         categories: CatName 
        }, 

        yAxis: 
        { 
         allowDecimals: false, 
         min: 0, 
         title: 
         { 
          text: 'Sales' 
         } 
        }, 

        tooltip: 
        { 
         formatter: function() { 
          return '<b>' + this.x + '</b><br/>' + 
           this.series.name + ': ' + this.y + '<br/>' + 
           'Total: ' + this.point.stackTotal; 
         } 
        }, 

        plotOptions: 
        { 
         column: 
         { 
          stacking: 'normal' 
         } 
        }, 

        series: [{ 
         name: 'Clubs', 
         data: [] 
        }, { 
         name: 'Festivals', 
         data: [] 
        }, { 
         name: 'Private', 
         data: [], 
        }, { 
         name: 'Weddings', 
         data: [], 
        }, { 
         name: 'OtherIncome', 
         data: [], 
        }] 
       }); 

     }); 

而你的JSON應該是一個只包含數字的數組。

0
  1. 圖初始化應在insie的getJSON身體,因爲你嘗試初始化圖表,然後前面的數據是從URL
  2. 在JSON你需要使用數字,而不是琴絃。如果不是可能的,你需要通過parseFloat在每個數據值中解析JavaScript中的值。
  3. 您的系列數據對象應該只包含數字,而不是像「Fesitvals」這樣的名稱。如果你喜歡有幾個系列,並且每個系列都有自己的名字,你需要解析你的json和praaper正確的數組結構。
+0

正如我所說的,新手對於這個非常新手來說,所以我不清楚你在#2和#3上說些什麼。系列名稱,序列號不同,所以Saumil建議的硬編碼不可行。有沒有什麼辦法可以將第一個數組元素作爲Series名稱提取出來,其餘的數組元素作爲Series? – user3117622

+0

我是否需要在JSon上做一個$ each,然後將第一個元素移至系列名稱,將其餘元素重新格式化爲另一個數組並將其推送至該系列? – user3117622

+0

所以我建議先熟悉javascript –