【问题标题】:Google-charts date range filter from mysql来自 mysql 的 Google-charts 日期范围过滤器
【发布时间】:2012-11-07 09:31:35
【问题描述】:
$SQLString = "SELECT    
                count(score) as counts,
                score, month,
                date FROM persons  
                GROUP BY day, month, year 
                ORDER BY date asc";     

    $result = mysql_query($SQLString);   
    $num = mysql_num_rows($result);   

# set heading   
    $data[0] = array('day','counts');       
    for ($i=1; $i<($num+1); $i++)
    {
        $data[$i] = array(substr(mysql_result($result, $i-1, "date"), 0, 10),
            (int) mysql_result($result, $i-1, "counts"));
    }   
    echo json_encode($data);

这给了我这样的信息:[["day","counts"],["2012-01-20",1],["2012-02-06",4]

function drawChart() {
            var jsonData = $.ajax({
                url: "charts.php",
                dataType: "json",
                async: false
            }).responseText;

            var obj = jQuery.parseJSON(jsonData);
            var data = google.visualization.arrayToDataTable(obj);

            var options = {
                title: 'Counts'
            };

现在,我想用日期范围控制器构建我的图表:

var control = new google.visualization.ControlWrapper({
                 'controlType': 'ChartRangeFilter',
                 'containerId': 'control',
                 'options': {
                   // Filter by the date axis.
                   'filterColumnIndex': 0,
                   'ui': {
                     'chartType': 'LineChart',
                     'chartOptions': {
                       'chartArea': {'width': '90%'},
                       'hAxis': {'baselineColor': 'none'}
                     },
                     // Display a single series that shows the closing value of the stock.
                     // Thus, this view has two columns: the date (axis) and the stock value (line series).
                     'chartView': {
                       'columns': [0,1]
                     },
                     // 1 day in milliseconds = 24 * 60 * 60 * 1000 = 86,400,000
                     'minRangeSize': 86400000
                   }
                 },
                 // Initial range: 2012-02-09 to 2012-03-20.
                 'state': {'range': {'start': new Date(2012, 1, 1), 'end': new Date(2012, 4, 20)}}
               });

问题是:

["2012-01-20",1],那个 "2012-01-20" 是一个字符串并且那个日期控制器只适用于日期类型,我该怎么办?

谢谢

【问题讨论】:

    标签: php javascript mysql json google-visualization


    【解决方案1】:

    从这里获取数据后:

    var obj = jQuery.parseJSON(jsonData);
    

    您可以解析 obj 并将这些字符串日期替换为日期对象:

    for ( var i = 0; i < obj.<array of some kind that holds rows> i++ ) { 
        obj.<array of some kind that holds rows>.<newdatefield> = 
                          new Date( obj.<array of some kind that holds rows>.<datestring> );
    }
    

    Date conversion link:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多