I have install the miloschuman\highcharts\Highcharts, website : https://www.yiiframework.com/extension/yii2-highcharts-widget. I have a database with the table columns lme_price and lme_name which I want to display the price of the copper in the highcharts. I'm using PHP.
Below is my code which I have done. This is the code in my models. I create a static function with the query inside it to find the data that I want from the database.
public static function getCopperPrice()
{
$getCopperPrices = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->all();
return $getCopperPrices;
}
Here is my code in the view. I have a table show in the view which show every data from the database.
<div class="lme_index">
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'searchModel'=> $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=> 'DATE',
'attribute' => 'lme_title',
],
[
'label'=> 'MATERIAL',
'attribute'=> 'lme_name',
],
[
'label'=> 'PRICE (US$)',
'attribute'=> 'lme_price',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Here is the code for the highcharts graph.
<div class = "graph">
<?= Highcharts::widget([
'options' => [
'title' => ['text' => 'Copper Price'],
'xAxis' => [
'categories' => []
],
'yAxis' => [
'title' => ['text' => 'Price $US']
],
'plotOption' => [
'series' => [
['name' => 'Copper',
'data' => [lme::getCopperPrices()]],
]
]
]
]);?>
I want to display only 1 material price in the highcharts. I call the function from the model inside the series but the graph shows nothing on it. The coding there didn't show any error to me.
Can someone help me or tell me how to solve this? Thanks.
Chart require integer values to display
public static function getCopperPrice()
{
$cooperPrices = [];
$copperPriceList = Lme::find()
->select('lme_price')
->where(['lme_name' => 'LME Copper'])
->column();
foreach($copperPriceList as $price) {
$cooperPrices[] = (int) price;
}
return $cooperPrices;
}
Related
I want to display total of row vaules in footer.
I have tried several ways but doesn't show proper result.
Any help will be highly appriciated.
I am using kartik grid.
I have tried this Yii2: Kartik Gridview sum of a column in footer
but doesn't work in my case.
$gridColumns = [
[
'class' => 'kartik\grid\SerialColumn',
'contentOptions' => ['class' => 'kartik-sheet-style'],
'width' => '36px',
'header' => '',
'headerOptions' => ['class' => 'kartik-sheet-style']
],
[
'attribute' => 'vno',
'footer'=>'Total'
],
[
'attribute' => 'fliters',
'label' => 'Total Fuel Consumption Liters',
'footer'=> true // Here i want to display the sum of column filer ( Note : the value in grid of filter is a sum(fuel_liters) )
],
[
'attribute' => 'fuel_rate',
'label' => 'Fuel Rate (Per Ltr.)',
'pageSummary'=>true,
'footer'=>true // Here i want to display the sum of column fuel_rate ( Note : the value in grid of filter is a sum(fuel_rate) )
],
[
'attribute' => 'famt',
'label' => 'Total Fuel Consumption Amount',
'pageSummary'=>true,
'footer'=>true // Here i want to display the sum of column fuel_amount ( Note : the value in grid of filter is a sum(fuel_amount) )
],
];
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);
So far I manage to print total in footer
Im my Controller i have wriiten this
$total_ltrs = FuelConsumption::find()->sum('fuel_liters');
$total_amt = FuelConsumption::find()->sum('fuel_amount');
$total_rate = FuelConsumption::find()->sum('fuel_rate');
return $this->render('petrol-report', [
'total_ltrs' => $total_ltrs,
'total_amt' => $total_amt,
'total_rate' => $total_rate,
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
And passed 'footer'=>$total_ltrs 'footer'=>$total_rate and 'footer'=>$total_amt Respectively in all footers.
Now when i do search throgh From Date and TO Date the result is dislaying in grid as per filter.
But the Result of Total in footer keeps unchanged.
I want to do some of only those rows which are in grid.
Can anybody help me on this ??
in your GridView, enable showFooter
in your column, set the footer text
now you will need a function to return your sum and set is a the footer's text.
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showFooter' => true,
'columns' => [
[
'class' => '\common\grid\CheckboxColumn'
],
[
'attribute' => 'myfield1',
'footer' => 'my footer'
],
],
]);
if you want a function, use a static helper:
class MyHelper {
public static function footer() { time(); }
}
'footer' => MyHelper::footer()
I want to show staff has many hobbies from table hobbies in detail view and gridview.
But I got an error exception Trying to get property of non-object
Here's my schema code model:
app\model\TblDataStaff
....
public function getTblDataHobis()
{
return $this->hasMany(TblDataHobies::className(), ['id_staff' => 'id']);
}
view code: view.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
...
['attribute'=>'namHob','value'=>$model->tblDataHobis->id],
...
],
]) ?>
index:
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
......
['attribute'=>'namHob','value'=>function($namHob){return $namHob->tblDataHobis->name_hobby;},],
.....
['class' => 'yii\grid\ActionColumn'],
],]);?>
How to show many hobbies of staff ?
Nothing strange, you have a Trying to get property of non-object error simply because $model->tblDataHobis return an array of TblDataHobies objects.
You could simply try this :
// display hobbies names separated with commas
echo implode(', ', \yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby'));
For DetailView :
'value' => implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
For GridView :
'value' => function($model) {
return implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
},
Try with:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'header' => 'number of hobbies',
'value' => function($data) {
return $data->getTblDataHobis()->count();
}
]
]) ?>
Just get the item data, and after that create a simple yii2 query.
For example my 'worker' has city id, with this id we can find city name by this id.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name:ntext',
'surname:ntext',
'fathers',
[
'attribute' => 'city_id',
'value'=> function($data){
$city = City::find()->where(['id'=>$data->city_id])->one();
$info = $city->name;
return $info;
},
'format'=>'html',
],
'status',
'street',
'in_company',
],
]) ?>
I have gone through this:
http://www.yiiframework.com/extension/yii-barcode-generator-8-types/
https://github.com/Vilochane/Yii-Barcode-Generator
http://www.yiiframework.com/extension/yii2-barcode-generator-8-types/
But doesn't get it work. My gridView:
<?= GridView::widget([
'dataProvider' => new yii\data\ActiveDataProvider(['query' => $model->getLibBookMasters()]),
'summary' => '',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
//'type' => 'raw',
'value'=> function($model){
return \barcode\barcode\BarcodeGenerator::widget(
[
'elementId' => 'lbm_barcode_no',
'value'=> 'lbm_barcode_no',
'type'=>'ean13',
]);},
],
],
]); ?>
I need to pass elementId that do the trick but doesn't found it.
I just installed Barcode Generator and don't know how to play around with.
You need to pass different elementIds. As your code is currently your are passing the literal 'lbm_barcode_no' instead of the value of the lbm_barcode_no attribute of your models. In addition, you have to create the divs where the barcode is to be shown and set the format of the column to raw or html:
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
'format' => 'raw',
'value'=> function($model){
return yii\helpers\Html::tag('div', '', ['id' => 'barcode-'.$model->lbm_barcode_no]).
\barcode\barcode\BarcodeGenerator::widget([
'elementId' => 'barcode-'.$model->lbm_barcode_no,
'value'=> $model->lbm_barcode_no,
'type'=>'ean13',
]);
},
],
],
I prefixed the tags with barcode- to avoid collisions (you never know).
i have Gridview in index i want to show width and height both in one column how can i do it
here is the view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_name',
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => 'fldMaterial.fld_name',
],
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => 'fldSize.fld_width',
],
// 'fld_size_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
i have relation fldSize in model here it is just only displaying fld_width i want to show it in the format fld_width."x".fld_height how can i do it in Yii2
You should simply use value callback, e.g. :
[
'label' => 'Size',
'attribute' => 'fld_size_id',
'value' => function ($model) {
return $model->fldSize->fld_width . 'x' . $model->fldSize->fld_height;
},
],
Sorry that after more than one year, but it works (not a dropdown but Select2).
Here is the code for the form
<?= $form->field($model, 'ID_MACH')->widget(Select2::classname(), [
'data'=> ArrayHelper::map(Maschines::find()->all(),'ID_MACH','fullname'),
'language'=>'ru',
'theme'=>'krajee',
'options'=>['placeholders'=>'suda...',
'prompt'=>'10-'],
'pluginOptions'=>[
'allowclear'=>true
],
Next is the Model for Mashines:
public function getFullName()
{
return $this->customer.','.$this->Manufacturer.','.$this->type.','.$this->serial;}
How can I sort with a customized gridview header?
Please give the difference between label and header in the Yii2 gridview widget dataprovider.
Here is my code:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'header' => 'Page Title',
'label' => 'Title'
],
]); ?>
Do header and label perform the same function?
How can I perform sorting in $data->myTitle?
Here my Output Screen:
I want Page Title, Status, Date Modified should be active.
Thanks in advance.
Found the answer.
Please add attributes to ActiveDataProvider in your Search Model.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
'sort' => ['attributes' => ['myTitle']],
]);
Add the attribute option in widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'attribute' => 'myTitle',
'label' => 'Page Title'
],
]); ?>
Since myTitle is a field from the database and not a custom value, you can just use attribute. The rest may be unnecessary e.g the default class is DataColumn
'columns' => [
[
'attribute' => 'myTitle',
'label' => 'Label',
]
I am not very sure I understand your question, but sorting option can be included in your modelsearch.php. So in your case you have to do like this.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['your_column'=>SORT_ASC]]
]);
if myTitle is a field in the database, why you are using such a long syntax. Just
'Columns'=>[
..
'myTitle',
..
],
should work fine and should be active for sorting as you want
if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. details
[
..
'attribute'=>'myTitle',
'label' => 'Page Title'
..
],