2014-09-23 60 views
0

如何修改這個代碼,以便它會產生基於data grouping基於數據分組

(function(Highcharts) { 

    'use strict'; 

    Highcharts.Chart.prototype.downloadCSV = function(returnOutput) { 
     var titles = [], 
      xValues = [], 
      line, 
      csv = "", 
      col, 
      options = (this.options.exporting || {}).csv || {}, 
      totalSeries = 0, 
      xNeedsSorting = false, 

      // Options 
      dateFormat = options.dateFormat || '%d-%m-%Y', 
      itemDelimeter = options.itemDelimeter || ',', 
      url = options.url || '/service/Export/exportchartcsv.php', 
      lineDelimiter = options.lineDelimeter || '\n';  

     Highcharts.each(this.series, function(series, seriesNo) { 
      if (series.options.includeInCSVExport === true) return; 
      if (series.options.xAxis) { // if option is set and not zero 
       alert("Downloading charts as CSV with multiple axes is no supported, sorry."); 
       throw false; 
      } 

      if (titles.length === 0) { 
       var xTitle = ((series.xAxis.options || {}).title || {}).text; 

       if (xTitle) { 
        // series.xAxis.title.text exists and we use it 
       } else if (series.xAxis.isDatetimeAxis) { 
        xTitle = 'Date/Time'; 
        xNeedsSorting = true; 
       } else if (series.xAxis.categories) { 
        xTitle = 'Category'; 
       } else { 
        xTitle = 'X values'; 
       } 
       titles.push(xTitle); 
      } 

      titles.push(series.name.replace(/[^a-z0-9\s]/gi, '-')); 
      totalSeries++; 
     }); 

     Highcharts.each(this.series, function(series, seriesNo) {    
      if (series.options.includeInCSVExport === true) return;   
      //console.log(series.options.alternateData); 
      Highcharts.each(series.options.alternateData, function(point) { 
       var l = xValues.length; 
       while (l--) { 
        if (xValues[l].x === point[0]) {      
         xValues[l].y[seriesNo] = point[1]; 
         return; 
        } 
       } 

       var y = new Array(totalSeries); 
       y[-1] = point[0]; // -1nth element holds formatted x axis value    
       y[seriesNo] = point[1]; 
       if (series.xAxis.isDatetimeAxis) { 
        y[-1] = Highcharts.dateFormat(dateFormat, point[0]); 
       } else if (series.xAxis.categories) { 
        y[-1] = Highcharts.pick(series.xAxis.categories[point[0]], point[0]); 
       } 

       xValues.push({ x : point[0], y : y }); 
      }) 
     }); 

     if (xNeedsSorting) { 
      xValues.sort(function(a, b) {return a.x - b.x}); 
     } 
     csv = titles.join(itemDelimeter) + lineDelimiter; 
     Highcharts.each(xValues, function(values) { 
      line = []; 
      for (col = -1; col < values.y.length; col++) { 
       line.push(values.y[col]);    
      } 
      csv += line.join(itemDelimeter) + lineDelimiter; 
     }); 

     if (returnOutput) { 
      return csv; 
     } else { 
      var title = ((this.title || {}).text || 'chart') + '.csv', 
       a = document.createElement('a'); 

      if ("download" in a) { // modern browser - no need to use backend script 
       a.href = 'data:attachment/csv,' + encodeURIComponent(csv); 
       a.target = '_blank'; 
       a.download = title; 
       document.body.appendChild(a); 
       a.click(); 
       a.remove(); 
      } else { 
       Highcharts.post(url, { csv : csv, title : title }); 
      } 
     } 
    }; 

    Highcharts.Chart.prototype.getCSV = function() { 
     return this.downloadCSV(true); 
    }; 

    // Now we want to add "Download CSV" to the exporting menu. We post the CSV 
    // to a simple PHP script that returns it with a content-type header as a 
    // downloadable file. 
    // The source code for the PHP script can be viewed at 
    // https://raw.github.com/highslide-software/highcharts.com/master/studies/csv-export/csv.php 
    if (Highcharts.getOptions().exporting) { 
     Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({ 
      text : Highcharts.getOptions().lang.downloadCSV || "Export Data", 
      onclick : function() { this.downloadCSV() } 
     }); 
    } 
}(Highcharts)); 

回答

0

首先的csv數據Highstock導出CSV,您使用的是舊版本的出口CSV,JS插件爲Highcharts。從gitHub下載實際的一個。

然後在#31#47行你會看到series.xData.slice()/series.yData.slice()。這個數組包含原始的x/y值。如果您希望值後dataGrouping(當逼近應用),然後從改變X/YDATA爲x/yProcessedXData:

series.processedXData.slice(); 
series.processedYData.slice(); 
+0

我使用了老版本的原因是因爲我有一個圖表,其中X軸爲datetime我不希望用戶導出爲CSV時,xAxis將爲我在圖表中的每個系列打印。 – Muhaimin 2014-09-25 06:16:47

+0

然後你正在使用哪個版本的Highstock?這是非常重要的信息。 – 2014-09-25 15:22:47

+0

'v2.0.1'。它與最新的有很大不同嗎? – Muhaimin 2014-09-26 01:24:23