输入数据后,chart.js上的数据不显示

时间:2018-06-09 08:06:05

标签: chart.js

我尝试使用带有laravel的图表js, 我尝试创建年份价格和名称价格但是当我存储它时,数据不会显示在图表视图中 not show data from databse

这是我的观点创建股票

然后我创建数据, 数据应显示在图表栏上,但在图表栏中不显示anthing

这是我在StockController中的商店功能

    public function store(Request $request)
{
    $stock = new Stock([
        'stockName' => $request->get('stockName'),
        'stockPrice' => $request->get('stockPrice'),
        'stockYear' => $request->get('stockYear'),

        ]);

        $stock -> save();

        return redirect('stocks');

}

这是我的chartController

    public function chart()
{
  $result = \DB::table('stocks')
              ->where('stockName','=','Infosys')
              ->orderBy('stockYear', 'ASC')
              ->get();
  return response()->json($result);
}

这是我在stocks.blade.php上的javascript

var url = "{{url('stocks/chart')}}";
    var Years = new Array();
    var Labels = new Array();
    var Prices = new Array();
    $(document).ready(function(){
      $.get(url, function(response){
        response.forEach(function(data){
            Years.push(data.stockYear);
            Labels.push(data.stockName);
            Prices.push(data.stockPrice);
        });
        var ctx = document.getElementById("canvas").getContext('2d');
            var myChart = new Chart(ctx, {
              type: 'bar',
              data: {
                  labels:Years,
                  datasets: [{
                      label: 'Info harga',
                      data: Years,


                  }]
              },
              options: {
                  scales: {
                      yAxes: [{
                          ticks: {
                              beginAtZero:true
                          }
                      }]
                  }
              }
          });
      });
    });
    </script>

这是我的路线

Route::get('stock/add', 'StockController@create');
Route::post('stock/add', 'StockController@store');
Route::get('stocks', 'StockController@index');
Route::get('stocks/chart', 'StockController@chart');

这是我的stockTable

   public function up()
{
    Schema::create('stocks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('stockName');
        $table->string('stockPrice');
        $table->string('stockYear');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('stocks');
}

如何输入数据可以在图表视图中显示

0 个答案:

没有答案