Dynamic value graph creation in yii2 using GoogleChart? - yii2

Here I wanna draw line chart graph by dynamic value.but in my case for every value of array created different different graph...Please help me I am first time do this task.Thanks in Advances
<?php
$modelEmployee=Employee::find()->select(['id','sales','expenses'])->all();
$arr = array('id'=>array(),
'sales'=>array(),
'expenses'=>array());
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
print_r($arr);
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => array(
array('Year', 'Sales', 'Expenses'),
array($arr['id'],$arr['sales'],$arr['expenses']),
),
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));
?>

first of all the multiple graphs are because you are doing echo inside for loop so it will take only one value and create graph from that.
you have to create an array of values and pass it to the graph widget as following
$graph_data = [];
$graph_data[] = array('Year', 'Sales', 'Expenses');
for($i = 0, $modEm = $modelEmployee; $i < sizeof($modelEmployee); $i++){
$arr['id'] = $modEm[$i]['id'];
$arr['sales'] = $modEm[$i]['sales'];
$arr['expenses'] = $modEm[$i]['expenses'];
$graph_data[] = array($arr['id'],$arr['sales'],$arr['expenses']); //add the values you require as set in the order of Year, Sales , Expenses
} //loop ends here
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $graph_data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));

Try this
action
$model=Employee::find()->select(['id','sales','expenses'])->all();
$data[]=["id","sales","expenses"];
foreach ($model as $item) {
$data[]=[(string) $item['id'],(int) $item['sales'],(int) $item['expenses']];
}
return $this->render('test',['data'=>$data]);
view
echo GoogleChart::widget(array('visualization' => 'LineChart',
'data' => $data,
'options' => array(
'title' => 'My Company Performance2',
'titleTextStyle' => array('color' => '#FF0000'),
'vAxis' => array(
'title' => 'Scott vAxis',
'gridlines' => array(
'color' => 'transparent' //set grid line transparent
)),
'hAxis' => array('title' => 'Scott hAixs'),
'curveType' => 'function', //smooth curve or not
'legend' => array('position' => 'bottom'),
)));

Related

How can I align different lines in PHP code?

PhpStorm 2021.2:
$is_fullcalendar_nearest_days = $expireDateCheckNearest->isPast();
return [
'id' => 'ad' . $adItem->id,
'title' => $adItem->id . '=>' . $adItem->title,
'adLocationsCount' => $adItem->ad_locations_count,
'status' => $adItem->status,
'status_label' => Ad::getStatusLabel($adItem->status),
'ad_type' => $adItem->ad_type,
'ad_type_label' => Ad::getAdTypeLabel($adItem->ad_type),
'price' => $adItem->price,
'price_formatted' => formatCurrencySum($adItem->price),
'is_fullcalendar_nearest_days' => $is_fullcalendar_nearest_days,
];
How can I align lines above with all => and = at same level?

WP query retrieve the src of the attached image

I'm encoding in JSON a bunch of data from a WP Query:
$args = array(
'posts_per_page' => 20,
'post_type' => 'post',
'category' => 6,
'meta_key' => 'custom_total_hits',
'tag' => 'indie-pop',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
'after' => date('Y-m-d', strtotime('-40 days'))
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$output[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'count' => $post->custom_total_hits,
'soundcloud_url' => $post->soundcloud_song,
'soundcloud_id' => $post->soundcloud_ids,
'link' => get_permalink($post),
);
}
echo json_encode($output);
I would like to display in my JSON a key corrisponding to the src of the medium size of the attached image. If I use 'images' => get_attached_media('image', $post->ID) it retrives an array of multiple data which I can not access since I don't know the ID of the attached image when I process the data of my JSON. How can I do to retrieve a first level key - value where the value is the src of the attached image?
get_post_thumbnail_id : Get post thumbnail ID
wp_get_attachment_url : Get attachment URL by attachment id
'images' => parse_url( wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ) );
Would you please try above code?
Try this solution:
$images = array();
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
foreach (get_intermediate_image_sizes() as $size) {
$images[$size] = wp_get_attachment_image_src($post_thumbnail_id, $size);
}
//end
'images' => $images // type_of_size => image_url

yii 1 relation not working in CGridView

I am trying to get relation where companies table have primary key companyID and division table have Foreign key companyID , what I need in where clause is WHERE companies.companyID = division.companies
relation in my model is :
public function relations()
{
return array(
'company' => array(self::BELONGS_TO, 'Companies', 'CompanyID'),
);
}
My Model->search() function is
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.CompanyID', $this->CompanyID, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and my admin.php view is:
<?php
$this->breadcrumbs = array(
'Divisions' => array('index'),
'Manage',
);
$this->menu = array(
array('label' => 'List Divisions', 'url' => array('index')),
array('label' => 'Create Divisions', 'url' => array('create')),
);
");
?>
<div class="row">
<?php
$this->renderPartial('_dropdownfilter', array(
'model' => $model,
));
?>
</div><!-- end dropdown partial form -->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
?>
You need to add together=true to your criteria.
$criteria->together = true;
It'll add join to query. Some information about lazy loading http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/
If you want to display company name,just do this in view.Don't change anything in model->search().
array(
'name'=>'Name',
'value'=>$model->company->name //here name is column name in company table.
),
In your gridview code do the following changes.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'divisions-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'companies',//fied from division table which refers to companyId from company table.
'header' => 'Company',
'value' => '$data->company->company_name'
),
'CompanyID',
'DivisionID',
'Name',
array(
'class' => 'CButtonColumn',
),
),
));
And in your model->search()
public function search()
{
$criteria=new CDbCriteria;
$criteria->with ='company';
$criteria->compare('company.company_name', $this->companies, true );
$criteria->compare('DivisionID',$this->DivisionID, true);
$criteria->compare('CompanyID',$this->CompanyID, true);
$criteria->compare('Name',$this->Name,true, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

Drupal Tableselects

I can (finally!!!) set a query to return results for a checkbox(es) set within a collapsible fieldset in Drupal 7- however when I try to put it into a tableselect, I get no results. Would someone be able to check out this code and see if you can tell me why? I also have a screen shot of both results- but since I am new here it won't allow me to post it.
$form = array();
$secnum = 1;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$options = array();
foreach ($result as $record) {
$options[$record->secser_id] = $record->ser_name;
}
$form['secser']['1'] = array(
'#title' => t('Basic Sanitation'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$form['secser']['1']['secser'] = array(
'#title' => t('Choices'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => $options,
'#description' => t('choose!'),
);
$form['secser']['2'] = array(
'#title' => t('Community Systems'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$secnum = 2;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$opt2 = array();
foreach ($result as $record) {
$opt2[$record->secser_id] = $record->ser_name;
}
$header = array(
'ser_name' => t('Choose Service(s)'),
);
$form['secser']['2']['secser'] = array(
'#type' => 'tableselect',
'#title' => t('Community Systems'),
'#header' => $header,
'#options' => array($opt2),
'#multiple' => TRUE,
);
I haven't tried running your code, but it looks like your options are an array which contains another array.
#options' => array($opt2),
because $opt2 is an array, this should be:
#options' => $opt2,
the same way you have the options set up in your checkboxes

Drupal Html array. How to reference a changing radio selection within a fieldset

I cannot make this Drupal html array fire the visible property on change of radio option. I have moved everything inside of the fieldset, not sure if this makes any difference.
Does anyone know why it isn't firing?
function services_formation_founders($form, &$form_state) {
$form = array();
$form['#tree'] = TRUE;
$form['description'] = array(
'#type' => 'item',
'#title' => t('Founders form'),
);
$form['founder']['add_officer'] = array(
'#type' => 'fieldset',
'#title' => t('Add Founder'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(TRUE,
),
),
);
$form['founder']['add_officer']['founder_type'] = array(
'#type' => 'radios',
'#options' => array(
'individual' => t('Individual'),
'corporate' => t('Corporation'),
),
'#default_value'=>'individual',
'#title' => t('What type of Founder?')
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
$form['founder']['add_officer']['individual'] = array(
'#type' => 'textfield',
'#title' => t('Individual'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "individual"),
),
),
);
$form['founder']['add_officer']['corporation'] = array(
'#type' => 'textfield',
'#title' => t('Corporation'),
'#states' => array(
'visible' => array(
':input[name="founder_type"' => array('value' => "corporate"),
),
),
);
return $form;
}
Place $ sign at line no 2
$form = array();