Home > Enterprise >  How can I candlestick data grouping in highcharts for different timeframe by laravel
How can I candlestick data grouping in highcharts for different timeframe by laravel

Time:01-13

I try to create a candlestick chart by highchart , I made a chart successfully but i have one difficulty that i not know how to group different timeframe data series ,

example , 1minute 5 minutes 15 minutes 1 hour 4 hour 1 day etc ....

to do this groups , what database need ?

in my Open High Low Close price data , I take them depend on day timeframe from database by laravel .

as follow

   $currenttime = Carbon::now()->format('Y-m-d');
     

        $date =Carbon::today()->format('Y-m-d');
       
        $pricelist = Sell::whereDate('created_at', '=', $date)->orderBy('id','asc')->get();

     

       $priceHL = Sell::whereDate('created_at', '=', $date)->orderBy('price','asc')->get();
   

         $openprice = $pricelist->first()->price;
         $closeprice = $pricelist->last()->price;
         $highprice = $priceHL->last()->price;
         $lowprice = $priceHL->first()->price;

I make them to know Open , close , high and low price of this day by compare Carbon format date ,

after that , I send this price data to another table as follow

  $datecompare =  Price::orderBy('id','asc')->get()->last()->created_at->format('Y-m-d');
   

        if($date != $datecompare ){
   
           
      
          Price::create([
           
            'openprice' => $openprice,
            'closeprice' => $closeprice,
            'highprice' => $highprice,
            'lowprice' => $lowprice,
           ]);

          }

          if($currenttime == $date ){
        
         Price::whereDate('created_at', '=', $date)->update([
            'openprice' => $openprice,
            'closeprice' => $closeprice,
            'highprice' => $highprice,
            'lowprice' => $lowprice,
           ]);

          }

           $priceall = Price ::get();

       

       return view('chart',compact('priceall'));

if current date price data has, filters all price to open close high low and if today not equal to previous day that record in Price table , insert new open close high low price to Price table , So Price table has data to from a candle

enter image description here

if data has in Prices table , I call them to candlestick chart by Highchart ,

$(document).ready(function () {

                          // create the chart
    $('#container').highcharts('StockChart', {
                 
                 rangeSelector: {
                     allButtonsEnabled: true,
                 
                 
                       selected: 1
                   },
                 
                   plotOptions: {
                            candlestick: {
                                color: 'red',
                                upColor: 'green'
                            }
                        },
                 
                 title: {
                     text: 'CC Price'
                 },
                 
                 
                 series: [{
                     type: 'candlestick',
                    
                     name: 'AAPL Stock Price',
                   
                     data: [
                 
                       <?php  foreach($priceall as $price) 
                 
                           {?>
                     
                             [ <?php echo strtotime($price['created_at']) *1000 ; ?>, <?php echo $price['openprice']; ?>,<?php echo $price['highprice']; ?>, <?php echo $price['lowprice']; ?> ,<?php echo $price['closeprice'] ?> ],
                         
                         <?php  }?>
                         ] ,
                    dataGrouping: {
                          units: [
                            [
                              'week', // unit name
                              [1] // allowed multiples
                            ], [
                              'month',
                              [1, 2, 3, 4, 6]
                            ]
                          ]
                        }
                 
                 


}]



});
   
      });

     

Candlestick appear ,but i have one problem is how to set different timeframe data group base on this database output on Highcharts ,

What data still need to do different timeframes grouping , Highcharts can make data grouping by reading x values timestamp ?

if so how can do this , I read some articles they said https://api.highcharts.com/highstock/series.candlestick.grouping can do this . but how? please show me example , I can learn well if there is some examples,

thanks.

CodePudding user response:

You can use dataGrouping the visible time interval between points is set by the array dataGrouping.units.

  dataGrouping: {
    enabled: true,
    units: [
      [
        'month',
        [6]
      ],
    ]
  }

Demo: https://jsfiddle.net/BlackLabel/t9me6uo8/1/

  • Related