Google charts line chart with non-uniform x-axis values - data-structures

I need to make a google line chart where students answer up to five questions with a value from 1 to 100.
The line chart should have the answer on the y-axis and the time and date of the answer on the x-axis.
Students answer all questions at the same time.
There should be one line for each answer for each student.
Lines for the same question should have the same colour - ie. if there are three students there should be three lines of each colour, one for each student.
My data looks like this in PHP before it gets parsed into a json-string and loaded into Google Charts:
$testtable = array(
'cols' => array(
array('label' => 'Date', 'type' => 'datetime'),
array('label' => 'Student', 'type' => 'string', 'role' => 'tooltip'),
array('label' => 'Answer1', 'type' => 'number'),
array('label' => 'Answer2', 'type' => 'number'),
array('label' => 'Answer3', 'type' => 'number'),
array('label' => 'Answer4', 'type' => 'number'),
array('label' => 'Answer5', 'type' => 'number')
),
'rows' => array(
array('c' => array(
array('v' => 'Date(2014,3,4,17,3,17)'),
array('v' => 'elev1'),
array('v' => 15),
array('v' => 36),
array('v' => 87),
array('v' => 10),
array('v' => 22)
)),
array('c' => array(
array('v' => 'Date(2014,3,4,13,56,22)'),
array('v' => 'elev2'),
array('v' => 11),
array('v' => 66),
array('v' => 87),
array('v' => 23),
array('v' => 27)
)),
array('c' => array(
array('v' => 'Date(2014,3,5,10,27,31)'),
array('v' => 'elev1'),
array('v' => 43),
array('v' => 11),
array('v' => 33),
array('v' => 64),
array('v' => 88)
)),
array('c' => array(
array('v' => 'Date(2014,3,5,12,22,53)'),
array('v' => 'elev2'),
array('v' => 22),
array('v' => 34),
array('v' => 62),
array('v' => 32),
array('v' => 5)
)),
)
);
Here is what I have in mind:
http://s21.postimg.org/p146t1dqf/chart.png
Here is what my chart currently looks like:
http://s11.postimg.org/w6gvfa5n7/chart2.png
I'm pretty sure my data is structured wrong but I can't get my head around the right way to do it. I would be really happy if someone could help.

A LineChart requires one data series per line, so if you want one line per student per question, you need one data series (DataTable column) per student per question (so if you have 3 students and 5 questions, you need 15 data series). Use either the colors or series.<series index>.color options to color the lines.
'cols' => array(
array('label' => 'Date', 'type' => 'datetime'),
array('label' => 'Student1 Answer1', 'type' => 'number'),
array('label' => 'Student1 Answer2', 'type' => 'number'),
array('label' => 'Student1 Answer3', 'type' => 'number'),
array('label' => 'Student1 Answer4', 'type' => 'number'),
array('label' => 'Student1 Answer5', 'type' => 'number'),
array('label' => 'Student2 Answer1', 'type' => 'number'),
array('label' => 'Student2 Answer2', 'type' => 'number'),
array('label' => 'Student2 Answer3', 'type' => 'number'),
array('label' => 'Student2 Answer4', 'type' => 'number'),
array('label' => 'Student2 Answer5', 'type' => 'number'),
array('label' => 'Student3 Answer1', 'type' => 'number'),
array('label' => 'Student3 Answer2', 'type' => 'number'),
array('label' => 'Student3 Answer3', 'type' => 'number'),
array('label' => 'Student3 Answer4', 'type' => 'number'),
array('label' => 'Student3 Answer5', 'type' => 'number')
// etc...
)

Related

Set default (current datetime) to table column - CodeIgniter 3 migration

I have this migration:
public function up(){
$this->dbforge->add_field([
'id'=>['type'=>'int', 'unique'=>true, 'unsigned'=>true,'auto_increment'=>true],
'email'=>['type'=>'varchar', 'constraint'=>200, 'null'=>true],
'password'=>['type'=>'varchar', 'constraint'=>250],
'created_at'=>['type'=>'datetime', 'default' => 'CURRENT_TIMESTAMP'],
]);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('users', TRUE);
}
I am trying to set table with column created_at with default value - current datetime.
I am using 'default' => 'CURRENT_TIMESTAMP', but I am getting this error:
Invalid default value for 'created_at' ....
NOT NULL, created_at datetime NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
I am using CodeIgniter 3 with MySQL.
this worked for me, and is valid in the documentation
Passing strings as fields If you know exactly how you want a field to
be created, you can pass the string into the field definitions with
add_field()
$this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT
'default label'");
Note:
Passing raw strings as fields cannot be followed by add_key() calls on
those fields.
`$this->dbforge->add_field(
array(
'nombre' =>
array(
'type' => 'VARCHAR',
'constraint' => '150',
),
'paterno' =>
array(
'type' => 'VARCHAR',
'constraint' => '80',
),
'materno' =>
array(
'type' => 'VARCHAR',
'constraint' => '80',
),
'correo' =>
array(
'type' => 'varchar',
'constraint' => '150',
'unique'=> TRUE,
),
'username' =>
array(
'type' => 'varchar',
'constraint' => '80',
),
'genero' =>
array(
'type' => 'varchar',
'constraint' => '30',
),
'pass' =>
array(
'type' => 'varchar',
'constraint' => '255',
),
'direccion' =>
array(
'type' => 'varchar',
'constraint' => '200',
),
'telefono' =>
array(
'type' => 'varchar',
'constraint' => '30',
),
'celular' =>
array(
'type' => 'varchar',
'constraint' => '30',
),
'created_at datetime default current_timestamp',
'updated_at datetime default current_timestamp on update current_timestamp',
'status' =>
array(
'type' => 'tinyint',
'constraint' => '1',
),
)
);`
For Codeigniter 4 this is slightly different:
https://codeigniter.com/user_guide/dbmgmt/forge.html#raw-sql-strings-as-default-values
You would need something like this ...
'DateAdded' => array(
'type' => 'TIMESTAMP',
'default' => new RawSql('CURRENT_TIMESTAMP'),
),

How to Make a Status Field Bydefault Enable in Magento Form?

Following is my Form Field Code.
$fieldset->addField('status','select',
array(
'label' => Mage::helper('synclogin')->__('Eshot Status'),
'name' => 'status',
'values' => array(
array(
'value' => 0,
'label' => Mage::helper('synclogin')->__('Disabled'),
),
array(
'value' => 1,
'label' => Mage::helper('synclogin')->__('Enabled'),
),
),
)
);
How to Make a Status Field Bydefault Enable in Magento Form?
$fieldset->addField('status','select',
array(
'label' => Mage::helper('synclogin')->__('Eshot Status'),
'name' => 'status',
'value' => '1',
'values' => array(
array(
'value' => 0,
'label' => Mage::helper('synclogin')->__('Disabled'),
),
array(
'value' => 1,
'label' => Mage::helper('synclogin')->__('Enabled'),
),
),
)
);

Zf2 entity create a custom filters

public function getInputFilter($em){
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'fullName',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
array('name' => 'SpecialChar')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'EmailAddress',
),
array(
'name' => 'User\Validator\NoEntityExists',
'options'=>array(
'entityManager' =>$em,
'class' => 'User\Entity\User',
'property' => 'email',
'exclude' => array(
array('property' => 'id', 'value' => $this->getId())
)
)
)
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
I want to add a new function in entity called "Special Char" in all input fields, so how does one create a custom filter in a doctrine entity.
I want to add validation to avoid special char in entity because I need to use this in n no of places.
How do I implement this?
From Zend documentation:
namespace Application\Filter;
use Zend\Filter\FilterInterface;
class MyFilter implements FilterInterface
{
public function filter($value)
{
// perform some transformation upon $value to arrive on $valueFiltered
return $valueFiltered;
}
}
Then you should be able to do:
$inputFilter->add(array(
'name' => 'fullName',
'required' => true,
'filters' => array(
array('name' => 'Application\Filter\MyFilter')
…

ZF2 How to validate fields inside fieldset?

I have a field in the form like this:
$this -> add(array(
'type' => 'field-set',
'name' => 'meta_properties',
'options' => array(
'label' => "Meta Properties",
),
'elements' => array(
)
));
$meta_fieldSet = $this -> get('meta_properties');
$meta_fieldSet->add(array(
'name' => 'meta_title',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Title',
),
));
$meta_fieldSet->add(array(
'name' => 'meta_description',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Description',
),
));
$meta_fieldSet->add(array(
'name' => 'meta_keywords',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Keywords',
)
));
And in my input filter class I have
$inputFilter -> add($factory -> createInput(array(
'name' => 'meta_title',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 150,
),
),
),
)));
$inputFilter -> add($factory -> createInput(array(
'name' => 'meta_description',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 150,
),
),
),
)));
$inputFilter -> add($factory -> createInput(array(
'name' => 'meta_keywords',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 150,
),
),
),
)));
It is not validating. How to validate InputFilter?
When creating Fieldsets you need to extend \Zend\Form\Fieldset. Try creating a fieldset like this:
<?php
/**
* Fieldset Example
*/
namespace Module\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class NewFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
// we want to ignore the name passed and give it our own
parent::__construct('appointment');
// Use the model you are associating with your fieldset here
$this->setHydrator(new ClassMethodsHydrator(false))
->setObject(new MyModel)
->setLabel('My Model');
$this->add(array(
'name' => 'fieldset_item_1',
'type' => 'Select',
'options' => array(
'label' => 'FieldSet Item 1',
)
));
$this->add(array(
'name' => 'fieldset_item_2',
'type' => 'Select',
'options' => array(
'label' => 'FieldSet Item 2',
)
));
}
/**
* Sets up the input filter specification and returns it
*/
public function getInputFilterSpecification()
{
return array(
'fieldset_item_1' => array(
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
),
'fieldset_item_2' => array(
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
),
);
}
}
Then you can use it in your form like this:
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'fieldset',
'options' => array(
'label' => '',
'count' => 1,
'should_create_template' => true,
'allow_add' => true,
'use_as_base_fieldset' => false,
'create_new_objects' => true,
'target_element' => array('type' => 'Module\Form\NewFieldset')
),
));
Now when you perform $form->isValid() in a controller it will validate the fieldset. In addition, you can now make some fancy front-end code to dynamically add and remove fieldsets from the form and they will all get validated.

set selected field when define with 'type' => 'select' in magento

I have a custom module where I create a form.
One field is a dropdown with values from a flat file.
$fields = array(
'type_of_service' => array('display' => 'Tip serviciu',
'required' => true,
'type' => 'select',
'options' => $serviceTypeOptions,
How can I set one value to be selected ?
EDIT:
if I use something like this in _prepareForm()
$fieldset->addField('select', 'select', array(
'label' => Mage::helper('tracking')->__('Select'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
'value' => '3',
'values' => array('-1'=>'Please Select..','1' => 'Option1','2' => 'Option2', '3' => 'Option3'),
)),
preselected will be 3.
But I generate my fields previously, and assign those to form with
$formFields = $this->getFormFields();
The problem is that I can't use the addField method, I need to have them created within my function.
Thanks.
try this,
'required' => true,
'value' => '2',
'values' => array(
array('1' => 'Option 1','2' => 'Option 2'),
...
which will select Option 2.
Try this
'required' => true,
'value' => 2,
'values' => array(
array('label' => 'Option 2', value => 2),
array('label' => 'Option 1', value => 1)
)
A full example can be like below
$fieldset->addField(
'target_type',
'select',
array(
'label' => $this->__("Target Type"),
'class' => 'required-entry',
'required' => 'true',
'name' => 'target_type',
'note' => $this->__("Target Type"),
'value' => 2,
'values' => array(
array(
'value' => 2,
'label' => 'Option 2'
)
)
)
);
NOTE : If you want to know about any of the Varien_Data_Form_Elements, you can directly do an inspection of Varien library /lib/Varien/Data/Form/Element/Select.php in your case.

Resources