2017-03-03 43 views
-1

我有一個要求如下。Javascript圖表作爲輸入

  • 我需要使用圖形輸入時間序列數據(日期數據)。數據通常如下,但配置文件可以根據具體情況進行更改。

現在數據被手動輸入到文本框中,這使得它非常困難。

我想知道是否有解決方案讓用戶在圖表上繪製並在後面生成數據。換句話說,用戶選擇某些點並繪製輪廓。

enter image description here

+1

哪裏輸入的數據從何而來?應該可以從源代碼中獲取數據並直接將其提供給圖表 - 但有趣的問題是[方法](https://developers.google.com/chart/interactive/docs/gallery/linechart#methods )可用於捕獲用戶點擊的位置,例如'getVAxisValue'和'getHAxisValue',但用戶難以精確選擇正確的點... – WhiteHat

+0

[Fusion Charts](http ://www.fusioncharts.com/dev/chart-guide/drag-able-charts/retrieving-data.html)這是我發現的最接近的,但問題是我無法添加任何數據點。 @WhiteHat數據來自數據庫,但需要根據場景進行編輯。 –

回答

1

看到下面的工作片段,點擊圖表上添加一個數據點...

google.charts.load('current', { 
 
    callback: function() { 
 
    var data = new google.visualization.DataTable({ 
 
     "cols": [ 
 
     {"label": "x", "type": "number"}, 
 
     {"label": "y", "type": "number"} 
 
     ] 
 
    }); 
 

 
    var axisMax = 10; 
 
    var ticks = []; 
 
    for (var i = 0; i <= axisMax; i = i + 0.5) { 
 
     ticks.push(i); 
 
    } 
 

 
    var options = { 
 
     chartArea: { 
 
     bottom: 64, 
 
     height: '100%', 
 
     left: 64, 
 
     top: 24, 
 
     width: '100%' 
 
     }, 
 
     hAxis: { 
 
     format: '0.0', 
 
     textStyle: { 
 
      fontSize: 9 
 
     }, 
 
     ticks: ticks, 
 
     title: data.getColumnLabel(0), 
 
     viewWindow: { 
 
      min: 0, 
 
      max: axisMax 
 
     } 
 
     }, 
 
     height: 600, 
 
     legend: { 
 
     position: 'top' 
 
     }, 
 
     pointSize: 4, 
 
     vAxis: { 
 
     format: '0.0', 
 
     textStyle: { 
 
      fontSize: 9 
 
     }, 
 
     ticks: ticks, 
 
     title: data.getColumnLabel(1), 
 
     viewWindow: { 
 
      min: 0, 
 
      max: axisMax 
 
     } 
 
     } 
 
    }; 
 

 
    var tableDiv = document.getElementById('table_div'); 
 
    var table = new google.visualization.Table(tableDiv); 
 
    var chartDiv = document.getElementById('chart_div'); 
 
    var chart = new google.visualization.LineChart(chartDiv); 
 
    var chartLayout = null; 
 

 
    google.visualization.events.addListener(chart, 'ready', function() { 
 
     chartLayout = chart.getChartLayoutInterface(); 
 
    }); 
 
    google.visualization.events.addListener(chart, 'click', function (sender) { 
 
     data.addRow([ 
 
     chartLayout.getHAxisValue(sender.x), 
 
     chartLayout.getVAxisValue(sender.y) 
 
     ]); 
 
     drawChart(); 
 
    }); 
 

 
    function drawChart() { 
 
     chart.draw(data, options); 
 
     table.draw(data); 
 
    } 
 

 
    window.addEventListener('resize', drawChart, false); 
 
    drawChart(); 
 
    }, 
 
    packages:['corechart', 'table'] 
 
});
div { 
 
    text-align: center; 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="chart_div"></div> 
 
<div id="table_div"></div>

+0

希望這會有所幫助,在_full page_模式下效果更好... – WhiteHat

+0

這是一個優雅的解決方案。我將需要爲我的目的重構代碼。我還需要在點上添加拖拽。 –