function get_graph_values($field, $args)
 {
     global $frm_entry_meta, $frm_field, $frmdb, $wpdb;
     $values = $labels = $f_values = $f_labels = $rows = $cols = array();
     $pie = false;
     extract($args);
     $show_key = (int) $show_key;
     if ($show_key and $show_key < 5) {
         $show_key = 10;
     }
     $options = array('width' => $width, 'height' => $height, 'legend' => 'none');
     if (!empty($colors)) {
         $options['colors'] = $colors;
     }
     $options['title'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", FrmAppHelper::truncate($field->name, $truncate, 0));
     if ($show_key) {
         $options['legend'] = array('position' => 'right', 'textStyle' => array('fontSize' => $show_key));
     }
     $fields = $f_inputs = array();
     $fields[$field->id] = $field;
     if ($ids) {
         $ids = explode(',', $ids);
         foreach ($ids as $id_key => $f) {
             $ids[$id_key] = $f = trim($f);
             if (!$f or empty($f)) {
                 unset($ids[$id_key]);
                 continue;
             }
             if ($add_field = $frm_field->getOne($f)) {
                 $fields[$add_field->id] = $add_field;
                 $ids[$id_key] = $add_field->id;
             }
             unset($f);
             unset($id_key);
         }
     } else {
         $ids = array();
     }
     $cols['xaxis'] = array('type' => 'string');
     if ($x_axis) {
         $x_field = $frm_field->getOne($x_axis);
         $query = $x_query = "SELECT meta_value, item_id FROM {$frmdb->entry_metas} em";
         if (!$x_field) {
             $x_query = "SELECT id, {$x_axis} FROM {$frmdb->entries} e";
         }
         if ($user_id) {
             $query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
             if ($x_field) {
                 $x_query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
             }
         }
         if ($x_field) {
             if (isset($allowed_col_types)) {
                 $cols['xaxis'] = array('type' => in_array($x_field->type, $allowed_col_types) ? $x_field->type : 'string', 'id' => $x_field->id);
             }
             $options['hAxis'] = array('title' => stripslashes($x_field->name));
             $x_query .= " WHERE em.field_id='{$x_field->id}'";
             if (!empty($x_start)) {
                 if ($x_field->type == 'date') {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and meta_value >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if ($x_field->type == 'date') {
                     $x_end = date('Y-m-d', strtotime($x_end));
                 }
                 $x_query .= " and meta_value <= '{$x_end}'";
             }
         } else {
             $cols['xaxis'] = array('type' => 'string');
             $x_query .= " WHERE form_id=" . $field->form_id;
             if (!empty($x_start)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and e.{$x_axis} >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_end = date('Y-m-d', strtotime($x_end)) . ' 23:59:59';
                 }
                 $x_query .= " and e.{$x_axis} <= '{$x_end}'";
             }
         }
         $q = array();
         foreach ($fields as $f_id => $f) {
             if ($f_id != $field->id) {
                 $q[$f_id] = $query . " WHERE em.field_id='{$f_id}'" . ($user_id ? " AND user_id='{$user_id}'" : '');
             }
             unset($f);
             unset($f_id);
         }
         $query .= " WHERE em.field_id='{$field->id}'";
         if ($user_id) {
             $query .= " AND user_id='{$user_id}'";
             $x_query .= " AND user_id='{$user_id}'";
         }
         $inputs = $wpdb->get_results($query, ARRAY_A);
         $x_inputs = $wpdb->get_results($x_query, ARRAY_A);
         if (!$x_inputs) {
             $x_inputs = array('id' => '0');
         }
         unset($query);
         unset($x_query);
         foreach ($q as $f_id => $query) {
             $f_inputs[$f_id] = $wpdb->get_results($query, ARRAY_A);
             unset($query);
         }
         unset($q);
     } else {
         if ($user_id) {
             $inputs = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE em.field_id='{$field->id}' AND user_id='{$user_id}'");
         } else {
             $inputs = $frm_entry_meta->get_entry_metas_for_field($field->id);
         }
         foreach ($fields as $f_id => $f) {
             if ($f_id != $field->id) {
                 $f_inputs[$f_id] = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE em.field_id='{$f_id}'" . ($user_id ? " AND user_id='{$user_id}'" : ''));
             }
             unset($f_id);
             unset($f);
         }
     }
     $inputs = array_map('maybe_unserialize', $inputs);
     $inputs = stripslashes_deep($inputs);
     foreach ($f_inputs as $f_id => $f) {
         $f = array_map('maybe_unserialize', $f);
         $f_inputs[$f_id] = stripslashes_deep($f);
         unset($f_id);
         unset($f);
     }
     if (isset($allowed_col_types)) {
         //add columns for each field
         foreach ($fields as $f_id => $f) {
             $cols[stripslashes($f->name)] = array('type' => in_array($f->type, $allowed_col_types) ? $f->type : 'number', 'id' => $f->id);
             unset($f);
             unset($f_id);
         }
         unset($allowed_col_types);
     }
     $field_options = maybe_unserialize($field->options);
     $field->field_options = maybe_unserialize($field->field_options);
     global $frm_posts;
     if ($frm_posts and isset($frm_posts[$field->form_id])) {
         $form_posts = $frm_posts[$field->form_id];
     } else {
         $form_posts = $frmdb->get_records($frmdb->entries, array('form_id' => $field->form_id, 'post_id >' => 1), '', '', 'id,post_id');
         if (!$frm_posts) {
             $frm_posts = array();
         }
         $frm_posts[$field->form_id] = $form_posts;
     }
     if (!empty($form_posts)) {
         if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
             if ($field->field_options['post_field'] == 'post_category') {
                 $field_options = FrmProFieldsHelper::get_category_options($field);
             } else {
                 if ($field->field_options['post_field'] == 'post_custom' and $field->field_options['custom_field'] != '') {
                     //check custom fields
                     foreach ($form_posts as $form_post) {
                         $meta_value = get_post_meta($form_post->post_id, $field->field_options['custom_field'], true);
                         if ($meta_value) {
                             if ($x_axis) {
                                 $inputs[] = array('meta_value' => $meta_value, 'item_id' => $form_post->id);
                             } else {
                                 $inputs[] = $meta_value;
                             }
                         }
                     }
                 } else {
                     //if field is post field
                     if ($field->field_options['post_field'] == 'post_status') {
                         $field_options = FrmProFieldsHelper::get_status_options($field);
                     }
                     foreach ($form_posts as $form_post) {
                         $post_value = $wpdb->get_var("SELECT " . $field->field_options['post_field'] . " FROM {$wpdb->posts} WHERE ID=" . $form_post->post_id);
                         if ($post_value) {
                             if ($x_axis) {
                                 $inputs[] = array('meta_value' => $post_value, 'item_id' => $form_post->id);
                             } else {
                                 $inputs[] = $post_value;
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($field->type == 'data') {
         foreach ($inputs as $k => $i) {
             if (is_numeric($i)) {
                 if (is_array($inputs[$k]) and isset($inputs[$k]['meta_value'])) {
                     $inputs[$k]['meta_value'] = FrmProFieldsHelper::get_data_value($inputs[$k]['meta_value'], $field, array('truncate' => 'truncate_label'));
                 } else {
                     $inputs[$k] = FrmProFieldsHelper::get_data_value($inputs[$k], $field, array('truncate' => 'truncate_label'));
                 }
             }
             unset($k);
             unset($i);
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $x_temp = array();
         foreach ($x_inputs as $x_input) {
             if ($x_field) {
                 $x_temp[$x_input['item_id']] = $x_input['meta_value'];
             } else {
                 $x_temp[$x_input['id']] = $x_input[$x_axis];
             }
         }
         $x_inputs = apply_filters('frm_graph_value', $x_temp, $x_field ? $x_field : $x_axis, $args);
         unset($x_temp);
         unset($x_input);
     }
     if ($x_axis and $inputs) {
         $y_temp = array();
         foreach ($inputs as $input) {
             $y_temp[$input['item_id']] = $input['meta_value'];
         }
         foreach ($ids as $f_id) {
             if (!isset($f_values[$f_id])) {
                 $f_values[$f_id] = array();
             }
             $f_values[$f_id][key($y_temp)] = 0;
             unset($f_id);
         }
         $inputs = $y_temp;
         unset($y_temp);
         unset($input);
     }
     $inputs = apply_filters('frm_graph_value', $inputs, $field, $args);
     foreach ($f_inputs as $f_id => $f) {
         $temp = array();
         foreach ($f as $input) {
             if (is_array($input)) {
                 $temp[$input['item_id']] = $input['meta_value'];
                 foreach ($ids as $d) {
                     if (!isset($f_values[$d][$input['item_id']])) {
                         $f_values[$d][$input['item_id']] = 0;
                     }
                     unset($d);
                 }
             } else {
                 $temp[] = $input;
             }
             unset($input);
         }
         $f_inputs[$f_id] = apply_filters('frm_graph_value', $temp, $fields[$f_id], $args);
         unset($temp);
         unset($input);
         unset($f);
     }
     if (in_array($field->type, array('select', 'checkbox', 'radio', '10radio', 'scale')) and (!isset($x_inputs) or !$x_inputs)) {
         if ($limit == '') {
             $limit = 10;
         }
         $field_opt_count = count($field_options);
         if ($field_options) {
             foreach ($field_options as $opt_key => $opt) {
                 $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field->field_options);
                 $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
                 $count = 0;
                 if (empty($opt)) {
                     continue;
                 }
                 $opt = stripslashes_deep($opt);
                 foreach ($inputs as $in) {
                     if (FrmAppHelper::check_selected($in, $field_val)) {
                         if ($data_type == 'total') {
                             $count += $field_val;
                         } else {
                             $count++;
                         }
                     }
                 }
                 $new_val = FrmAppHelper::truncate($opt, $truncate_label, 2);
                 if ($count > 0 or $field_opt_count < $limit or !$count and $include_zero) {
                     $labels[$new_val] = $new_val;
                     $values[$new_val] = $count;
                 }
                 unset($count);
                 foreach ($f_inputs as $f_id => $f) {
                     foreach ($f as $in) {
                         if (!isset($f_values[$f_id])) {
                             $f_values[$f_id] = array();
                         }
                         if (!isset($f_values[$f_id][$new_val])) {
                             $f_values[$f_id][$new_val] = 0;
                         }
                         if (FrmAppHelper::check_selected($in, $field_val)) {
                             if ($data_type == 'total') {
                                 $f_values[$f_id][$new_val] += $field_val;
                             } else {
                                 $f_values[$f_id][$new_val]++;
                             }
                         }
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
             if ($limit != 10 and count($values) > $limit) {
                 $ordered_vals = $values;
                 arsort($ordered_vals);
                 $l_count = 0;
                 foreach ($ordered_vals as $vkey => $v) {
                     $l_count++;
                     if ($l_count > $limit) {
                         unset($values[$vkey]);
                         unset($labels[$vkey]);
                     }
                     unset($vkey);
                     unset($v);
                 }
                 unset($l_count);
                 unset($ordered_vals);
             }
         }
         if (!in_array($field->type, array('checkbox', '10radio', 'scale'))) {
             //and count($field_options) == 2
             $pie = true;
         }
     } else {
         if ($field->type == 'user_id') {
             $form = $frmdb->get_one_record($frmdb->forms, array('id' => $field->form_id));
             $form_options = maybe_unserialize($form->options);
             $id_count = array_count_values($inputs);
             if ($form->editable and (isset($form_options['single_entry']) and isset($form_options['single_entry_type']) and $form_options['single_entry_type'] == 'user')) {
                 //if only one response per user, do a pie chart of users who have submitted the form
                 $users_of_blog = function_exists('get_users') ? get_users() : get_users_of_blog();
                 $total_users = count($users_of_blog);
                 unset($users_of_blog);
                 $id_count = count($id_count);
                 $not_completed = (int) $total_users - (int) $id_count;
                 $labels = array(__('Completed', 'formidable'), __('Not Completed', 'formidable'));
                 $values = array($id_count, $not_completed);
                 $pie = true;
             } else {
                 //arsort($id_count);
                 foreach ($id_count as $val => $count) {
                     $user_info = get_userdata($val);
                     $labels[] = $user_info ? $user_info->display_name : __('Deleted User', 'formidable');
                     $values[] = $count;
                 }
                 if (count($labels) < 10) {
                     $pie = true;
                 }
             }
         } else {
             if (isset($x_inputs) and $x_inputs) {
                 $calc_array = array();
                 foreach ($inputs as $entry_id => $in) {
                     $entry_id = (int) $entry_id;
                     if (!isset($values[$entry_id])) {
                         $values[$entry_id] = 0;
                     }
                     $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                     if (!isset($calc_array[$entry_id])) {
                         $calc_array[$entry_id] = array('count' => 0);
                     }
                     if ($data_type == 'total' or $data_type == 'average') {
                         $values[$entry_id] += (double) $in;
                         $calc_array[$entry_id]['total'] = $values[$entry_id];
                         $calc_array[$entry_id]['count']++;
                     } else {
                         $values[$entry_id]++;
                     }
                     unset($entry_id);
                     unset($in);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $entry_id => $calc) {
                         $values[$entry_id] = $calc['total'] / $calc['count'];
                         unset($entry_id);
                         unset($calc);
                     }
                 }
                 $calc_array = array();
                 foreach ($f_inputs as $f_id => $f) {
                     if (!isset($calc_array[$f_id])) {
                         $calc_array[$f_id] = array();
                     }
                     foreach ($f as $entry_id => $in) {
                         $entry_id = (int) $entry_id;
                         if (!isset($labels[$entry_id])) {
                             $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                             $values[$entry_id] = 0;
                         }
                         if (!isset($calc_array[$f_id][$entry_id])) {
                             $calc_array[$f_id][$entry_id] = array('count' => 0);
                         }
                         if (!isset($f_values[$f_id][$entry_id])) {
                             $f_values[$f_id][$entry_id] = 0;
                         }
                         if ($data_type == 'total' or $data_type == 'average') {
                             $f_values[$f_id][$entry_id] += (double) $in;
                             $calc_array[$f_id][$entry_id]['total'] = $f_values[$f_id][$entry_id];
                             $calc_array[$f_id][$entry_id]['count']++;
                         } else {
                             $f_values[$f_id][$entry_id]++;
                         }
                         unset($entry_id);
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $f_id => $calc) {
                         foreach ($calc as $entry_id => $c) {
                             $f_values[$f_id][$entry_id] = $c['total'] / $c['count'];
                             unset($entry_id);
                             unset($c);
                         }
                         unset($calc);
                         unset($f_id);
                     }
                 }
                 unset($calc_array);
             } else {
                 if (is_array(reset($inputs))) {
                     $id_count = array_map('implode', $inputs);
                     $id_count = array_count_values(array_map('strtolower', $id_count));
                 } else {
                     $id_count = array_count_values(array_map('strtolower', $inputs));
                     arsort($id_count);
                 }
                 $i = 0;
                 foreach ($id_count as $val => $count) {
                     if ($i < $response_count) {
                         if ($field->type == 'user_id') {
                             $user_info = get_userdata($val);
                             $new_val = $user_info->display_name;
                         } else {
                             $new_val = ucwords($val);
                         }
                         $labels[$new_val] = $new_val;
                         $values[$new_val] = $count;
                     }
                     $i++;
                 }
                 foreach ($f_inputs as $f_id => $f) {
                     $id_count = array_count_values(array_map('strtolower', $f));
                     arsort($id_count);
                     $i = 0;
                     foreach ($id_count as $val => $count) {
                         if ($i < $response_count) {
                             if ($field->type == 'user_id') {
                                 $user_info = get_userdata($val);
                                 $new_val = $user_info->display_name;
                             } else {
                                 $new_val = ucwords($val);
                             }
                             $position = array_search($new_val, $labels);
                             if (!$position) {
                                 end($labels);
                                 $position = key($labels);
                                 $labels[$new_val] = $new_val;
                                 $values[$new_val] = 0;
                             }
                             $f_values[$f_id][$new_val] = $count;
                         }
                         $i++;
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $used_vals = $calc_array = array();
         foreach ($labels as $l_key => $label) {
             if (empty($label) and (!empty($x_start) or !empty($x_end))) {
                 unset($values[$l_key]);
                 unset($labels[$l_key]);
                 continue;
             }
             if (in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type == 'pie') {
                     $labels[$l_key] = $label = $inputs[$l_key];
                 } else {
                     $labels[$l_key] = $label = date('Y-m-d', strtotime($label));
                 }
             }
             if (isset($used_vals[$label])) {
                 $values[$l_key] += $values[$used_vals[$label]];
                 unset($values[$used_vals[$label]]);
                 foreach ($ids as $f_id) {
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     if (!isset($f_values[$f_id][$used_vals[$label]])) {
                         $f_values[$f_id][$used_vals[$label]] = 0;
                     }
                     $f_values[$f_id][$l_key] += $f_values[$f_id][$used_vals[$label]];
                     unset($f_values[$f_id][$used_vals[$label]]);
                     unset($f_id);
                 }
                 unset($labels[$used_vals[$label]]);
             }
             $used_vals[$label] = $l_key;
             if ($data_type == 'average') {
                 if (!isset($calc_array[$label])) {
                     $calc_array[$label] = 0;
                 }
                 $calc_array[$label]++;
             }
             unset($label);
             unset($l_key);
         }
         if (!empty($calc_array)) {
             foreach ($calc_array as $label => $calc) {
                 if (isset($used_vals[$label])) {
                     $values[$used_vals[$label]] = $values[$used_vals[$label]] / $calc;
                     foreach ($ids as $f_id) {
                         $f_values[$f_id][$used_vals[$label]] = $f_values[$f_id][$used_vals[$label]] / $calc;
                         unset($f_id);
                     }
                 }
                 unset($label);
                 unset($calc);
             }
         }
         unset($used_vals);
     }
     $combine_dates = false;
     if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
         $combine_dates = apply_filters('frm_combine_dates', true, $x_field);
     }
     if ($combine_dates) {
         if ($include_zero) {
             $start_timestamp = empty($x_start) ? time() : strtotime($x_start);
             $end_timestamp = empty($x_end) ? time() : strtotime($x_end);
             $dates_array = array();
             // Get the dates array
             for ($e = $start_timestamp; $e <= $end_timestamp; $e += 60 * 60 * 24) {
                 $dates_array[] = date('Y-m-d', $e);
             }
             unset($e);
             // Add the zero count days
             foreach ($dates_array as $date_str) {
                 if (!in_array($date_str, $labels)) {
                     $labels[$date_str] = $date_str;
                     $values[$date_str] = 0;
                     foreach ($ids as $f_id) {
                         if (!isset($f_values[$f_id][$date_str])) {
                             $f_values[$f_id][$date_str] = 0;
                         }
                     }
                 }
             }
             unset($dates_array);
             unset($start_timestamp);
             unset($end_timestamp);
         }
         asort($labels);
         global $frmpro_settings;
         foreach ($labels as $l_key => $l) {
             if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type != 'pie' and preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $l)) {
                     global $frmpro_settings;
                     $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
                 }
             }
             unset($l_key);
             unset($l);
         }
         $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
     } else {
         if (isset($x_inputs) and $x_inputs) {
             foreach ($labels as $l_key => $l) {
                 foreach ($ids as $f_id) {
                     //do a last check to make sure all bars/lines have a value for each label
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     unset($fid);
                 }
                 unset($l_key);
                 unset($l);
             }
         }
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
         ksort($labels);
         ksort($values);
     }
     $labels = FrmProAppHelper::reset_keys($labels);
     $values = FrmProAppHelper::reset_keys($values);
     $return = array('total_count' => count($inputs), 'f_values' => $f_values, 'labels' => $labels, 'values' => $values, 'pie' => $pie, 'combine_dates' => $combine_dates, 'ids' => $ids, 'cols' => $cols, 'rows' => $rows, 'options' => $options, 'fields' => $fields);
     if (isset($x_inputs)) {
         $return['x_inputs'] = $x_inputs;
     }
     return $return;
 }
 /**
  * Combine dates when using created-at, updated-at, or date field on x-axis
  *
  * @since 2.0
  *
  * @param boolean $combine_dates - will be true if combining dates
  * @param array $values
  * @param array $labels
  * @param array $tooltips
  * @param array $f_values - additional field values
  * @param array $args - arguments
  */
 public static function combine_dates(&$combine_dates, &$values, &$labels, &$tooltips, &$f_values, $args)
 {
     if (isset($args['x_field']) && $args['x_field'] && $args['x_field']->type == 'date' || in_array($args['x_axis'], array('created_at', 'updated_at'))) {
         $combine_dates = apply_filters('frm_combine_dates', true, $args['x_field']);
     }
     if ($combine_dates === false) {
         return;
     }
     if ($args['include_zero']) {
         $start_timestamp = empty($args['start_date']) ? strtotime('-1 month') : strtotime($args['start_date']);
         $end_timestamp = empty($args['end_date']) ? time() : strtotime($args['end_date']);
         $dates_array = array();
         // Get the dates array
         for ($e = $start_timestamp; $e <= $end_timestamp; $e += 60 * 60 * 24) {
             $dates_array[] = date('Y-m-d', $e);
         }
         unset($e);
         // Add the zero count days
         foreach ($dates_array as $date_str) {
             if (!in_array($date_str, $labels)) {
                 $labels[$date_str] = $date_str;
                 $values[$date_str] = 0;
                 foreach ($args['ids'] as $f_id) {
                     if (!isset($f_values[$f_id][$date_str])) {
                         $f_values[$f_id][$date_str] = 0;
                     }
                 }
             }
         }
         unset($dates_array, $start_timestamp, $end_timestamp);
     }
     asort($labels);
     foreach ($labels as $l_key => $l) {
         if ((isset($args['x_field']) && $args['x_field'] && $args['x_field']->type == 'date' || in_array($args['x_axis'], array('created_at', 'updated_at'))) && !$args['group_by']) {
             if ($args['type'] != 'pie' && preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $l)) {
                 $frmpro_settings = new FrmProSettings();
                 $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
             }
         }
         unset($l_key, $l);
     }
     $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
     $tooltips = FrmProAppHelper::sort_by_array($tooltips, array_keys($labels));
     foreach ($args['ids'] as $f_id) {
         $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
         $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
         ksort($f_values[$f_id]);
         unset($f_id);
     }
 }
 static function get_graph_values($field, $args)
 {
     global $frm_entry_meta, $frm_field, $frmdb, $wpdb;
     $values = $labels = $f_values = $f_labels = $rows = $cols = array();
     $pie = false;
     extract($args);
     $user_id = (int) $user_id;
     $show_key = (int) $show_key;
     if ($show_key and $show_key < 5) {
         $show_key = 10;
     }
     $options = array('width' => $width, 'height' => $height, 'legend' => 'none', 'title' => '', 'titleTextStyle' => '');
     if (!empty($colors)) {
         $options['colors'] = $colors;
     }
     if (!empty($title)) {
         $options['title'] = $title;
     } else {
         $options['title'] = preg_replace("/&#?[a-z0-9]{2,8};/i", "", FrmAppHelper::truncate($field->name, $truncate, 0));
     }
     if (!empty($title_size) or !empty($title_font)) {
         $options['titleTextStyle'] = array('fontSize' => $title_size, 'fontName' => $title_font);
     }
     if ($show_key) {
         $options['legend'] = array('position' => 'right', 'textStyle' => array('fontSize' => $show_key));
     }
     $fields = $f_inputs = array();
     $fields[$field->id] = $field;
     if ($ids) {
         $ids = explode(',', $ids);
         foreach ($ids as $id_key => $f) {
             $ids[$id_key] = $f = trim($f);
             if (!$f or empty($f)) {
                 unset($ids[$id_key]);
                 continue;
             }
             if ($add_field = $frm_field->getOne($f)) {
                 $fields[$add_field->id] = $add_field;
                 $ids[$id_key] = $add_field->id;
             }
             unset($f);
             unset($id_key);
         }
     } else {
         $ids = array();
     }
     $cols['xaxis'] = array('type' => 'string');
     if ($atts) {
         //Sets up variables for get_field_matches function
         global $frm_post_ids;
         $entry_ids = array();
         $after_where = '';
         if (isset($frm_post_ids[$field->id])) {
             $form_posts = $frm_post_ids[$field->id];
         } else {
             $where_post = array('form_id' => $field->form_id, 'post_id >' => 1, 'is_draft' => 0);
             if ($user_id) {
                 $where_post['user_id'] = $user_id;
             }
             $form_posts = $frmdb->get_records($frmdb->entries, $where_post, '', '', 'id,post_id');
         }
         //Returns the entry IDs for fields being used to filter graph data
         foreach ($atts as $orig_f => $val) {
             $entry_ids = FrmProFieldsHelper::get_field_matches(array('entry_ids' => $entry_ids, 'orig_f' => $orig_f, 'val' => $val, 'id' => $field->id, 'atts' => $atts, 'field' => $field, 'form_posts' => $form_posts, 'after_where' => $after_where, 'drafts' => false));
             $after_where = true;
         }
     }
     //If start date is set, prepare the start date
     if ($start_date) {
         $start_date = $wpdb->prepare('%s', date('Y-m-d', strtotime($start_date)));
     }
     //If end date is set, prepare the end date
     if ($end_date) {
         $end_date = $wpdb->prepare('%s', date('Y-m-d', strtotime($end_date)));
     }
     if ($x_axis) {
         if (is_numeric($x_axis)) {
             $x_field = $frm_field->getOne($x_axis);
         } else {
             $x_field = false;
         }
         $query = $x_query = "SELECT em.meta_value, em.item_id FROM {$frmdb->entry_metas} em";
         if (!$x_field) {
             $x_query = "SELECT id, {$x_axis} FROM {$frmdb->entries} e";
         } else {
             $x_query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
         }
         $query .= " LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id)";
         if ($x_field) {
             if (isset($allowed_col_types)) {
                 $cols['xaxis'] = array('type' => in_array($x_field->type, $allowed_col_types) ? $x_field->type : 'string', 'id' => $x_field->id);
             }
             $options['hAxis'] = array('title' => $x_field->name);
             $x_query .= " WHERE em.field_id='{$x_field->id}'";
             if (!empty($x_start)) {
                 if ($x_field->type == 'date') {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and meta_value >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if ($x_field->type == 'date') {
                     $x_end = date('Y-m-d', strtotime($x_end));
                 }
                 $x_query .= " and meta_value <= '{$x_end}'";
             }
         } else {
             $cols['xaxis'] = array('type' => 'string');
             $x_query .= " WHERE form_id=" . $field->form_id;
             if (!empty($x_start)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_start = date('Y-m-d', strtotime($x_start));
                 }
                 $x_query .= " and e.{$x_axis} >= '{$x_start}'";
             }
             if (!empty($x_end)) {
                 if (in_array($x_axis, array('created_at', 'updated_at'))) {
                     $x_end = date('Y-m-d', strtotime($x_end)) . ' 23:59:59';
                 }
                 $x_query .= " and e.{$x_axis} <= '{$x_end}'";
             }
         }
         $q = array();
         foreach ($fields as $f_id => $f) {
             if ($f_id != $field->id) {
                 $q[$f_id] = $query . " WHERE em.field_id=" . (int) $f_id . ($user_id ? " AND user_id='{$user_id}'" : '');
             }
             unset($f);
             unset($f_id);
         }
         $query .= " WHERE em.field_id=" . (int) $field->id;
         if ($user_id) {
             //If user_id parameter is set
             $query .= $wpdb->prepare(" AND user_id=%d", $user_id);
             $x_query .= $wpdb->prepare(" AND user_id=%d", $user_id);
         }
         if ($entry) {
             //If entry_id parameter is set
             $query .= " AND em.item_id in ({$entry})";
             $x_query .= " AND e.id in ({$entry})";
         }
         if ($start_date) {
             //If start_date is set
             $query .= " AND e.created_at >= {$start_date}";
             $x_query .= " AND e.created_at >= {$start_date}";
         }
         if ($end_date) {
             //If end_date is set
             $query .= " AND e.created_at <= {$end_date}";
             $x_query .= " AND e.created_at <= {$end_date}";
         }
         $query .= " AND is_draft=0";
         $x_query .= " AND is_draft=0";
         //Set up $x_query for data from entries fields.
         if ($x_field && $x_field->type == 'data') {
             $linked_field = $x_field->field_options['form_select'];
             $x_query = str_replace('SELECT em.meta_value, em.item_id', 'SELECT dfe.meta_value, em.item_id', $x_query);
             $x_query = str_replace($wpdb->prefix . 'frm_item_metas em', $wpdb->prefix . 'frm_item_metas dfe, ' . $wpdb->prefix . 'frm_item_metas em', $x_query);
             $x_query = str_replace('WHERE', 'WHERE dfe.item_id=em.meta_value AND dfe.field_id=' . $linked_field . ' AND', $x_query);
         }
         //If filtering by another field ID
         if ($atts) {
             //If there are matching entry IDs, add entry IDs onto query
             if (!empty($entry_ids)) {
                 //if field is a post field, retrieve inputs differently
                 if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
                     foreach ($form_posts as $form_post) {
                         if (in_array($form_post->id, $entry_ids)) {
                             $query = $wpdb->get_var($wpdb->prepare("SELECT " . $field->field_options['post_field'] . " FROM {$wpdb->posts} WHERE ID = %d", $form_post->post_id));
                             $temp_inputs[] = array('meta_value' => $query, 'item_id' => $form_post->id);
                         }
                         unset($form_post);
                     }
                     unset($form_posts);
                     $skip_posts_code = true;
                 } else {
                     $query .= " AND em.item_id in (" . implode(',', $entry_ids) . ")";
                 }
                 $x_query .= " AND e.id in (" . implode(',', $entry_ids) . ")";
             } else {
                 //If entry ids is blank
                 $query = false;
                 $x_query = false;
             }
         }
         if ($query) {
             $query = apply_filters('frm_graph_query', $query, $field, $args);
         }
         if ($x_query) {
             $x_query = apply_filters('frm_graph_xquery', $x_query, $field, $args);
         }
         if (isset($temp_inputs)) {
             $inputs = $temp_inputs;
         } else {
             if ($query) {
                 $inputs = $wpdb->get_results($query, ARRAY_A);
             } else {
                 $inputs = false;
             }
         }
         if ($x_query) {
             $x_inputs = $wpdb->get_results($x_query, ARRAY_A);
         } else {
             $x_inputs = false;
         }
         if (!$x_inputs) {
             return;
         }
         unset($query);
         unset($x_query);
         unset($temp_inputs);
         //If there are multiple fields being graphed
         foreach ($q as $f_id => $query) {
             if (!empty($entry_ids)) {
                 //If a filter is set, only get filtered data from additional fields
                 $query .= " AND item_id in (" . implode(',', $entry_ids) . ")";
             } else {
                 if ($entry) {
                     //If entry_id parameter is set, only get data for this entry
                     $query .= " AND item_id in ({$entry})";
                 }
             }
             if ($start_date) {
                 $query .= " AND e.created_at >= {$start_date}";
             }
             if ($end_date) {
                 $query .= " AND e.created_at <= {$end_date}";
             }
             $f_inputs[$f_id] = $wpdb->get_results($query, ARRAY_A);
             unset($query);
         }
         unset($q);
         //If filtering by another field ID
     } else {
         if ($atts) {
             //If there are matching entry IDs, declare inputs
             if (!empty($entry_ids)) {
                 //If field is a post field, retrieve inputs in a different way - TODO: change database call for post fields so filters like entry ID, start_date, user ID, etc. are included
                 if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
                     foreach ($form_posts as $form_post) {
                         if (in_array($form_post->id, $entry_ids)) {
                             $post_ids[$form_post->id] = $form_post->post_id;
                         }
                         unset($form_post);
                     }
                     unset($form_posts);
                     if (!empty($post_ids)) {
                         $inputs = $wpdb->get_col("SELECT {$field->field_options['post_field']} FROM {$wpdb->posts} WHERE ID in (" . implode(',', $post_ids) . ")");
                     }
                     $skip_posts_code = true;
                 } else {
                     $inputs = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE is_draft=0 AND em.field_id='" . (int) $field->id . "' AND item_id in (" . implode(',', $entry_ids) . ")" . ($user_id ? " AND user_id=" . (int) $user_id : '') . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''));
                 }
                 //If there are multiple fields being graphed
                 foreach ($fields as $f_id => $f) {
                     if ($f_id != $field->id) {
                         $f_inputs[$f_id] = $wpdb->get_col("SELECT meta_value FROM {$frmdb->entry_metas} em LEFT JOIN {$frmdb->entries} e ON (e.id=em.item_id) WHERE is_draft=0 AND em.field_id='{$f_id}' AND item_id in (" . implode(',', $entry_ids) . ")" . ($user_id ? " AND user_id=" . (int) $user_id : '') . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''));
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
         } else {
             if ($user_id) {
                 //If UserID is the only filtering parameter defined
                 $inputs = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}frm_item_metas em INNER JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE is_draft=%d AND em.field_id=%d AND user_id=%d" . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''), 0, $field->id, $user_id));
             } else {
                 if ($entry) {
                     //If entry ID is the only filtering parameter defined
                     $inputs = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}frm_item_metas em INNER JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE is_draft=%d AND em.field_id=%d AND em.item_id in ({$entry})", 0, $field->id));
                 } else {
                     //If no user ID, atts, or x_axis defined
                     $inputs = $wpdb->get_col($wpdb->prepare("SELECT em.meta_value FROM {$wpdb->prefix}frm_item_metas em INNER JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE em.field_id=%d AND e.is_draft=%d" . ($start_date ? " AND e.created_at >= {$start_date}" : '') . ($end_date ? " AND e.created_at <= {$end_date}" : ''), $field->id, 0));
                 }
             }
             foreach ($fields as $f_id => $f) {
                 //Get the meta_values for additional fields being graphed
                 if ($f_id != $field->id) {
                     $f_where = array(0, $f_id);
                     if ($user_id) {
                         $f_where[] = $user_id;
                     }
                     $f_inputs[$f_id] = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->prefix}frm_item_metas em LEFT JOIN {$wpdb->prefix}frm_items e ON (e.id=em.item_id) WHERE is_draft=%d AND em.field_id=%d" . ($user_id ? " AND user_id=%d" : '') . ($entry ? " AND item_id in ({$entry})" : ''), $f_where));
                     unset($f_where);
                 }
                 unset($f_id);
                 unset($f);
             }
         }
     }
     if (isset($inputs) && !empty($inputs)) {
         //Break out any inner arrays (for checkbox or multi-select fields) and add them to the end of the $inputs array
         if (!$x_axis && ($field->type == 'data' && ($field->field_options['data_type'] == 'checkbox' || $field->field_options['data_type'] == 'select' && $field->field_options['multiple'] == 1) || $field->type == 'checkbox' || $field->type == 'select' && $field->field_options['multiple'] == 1)) {
             foreach ($inputs as $k => $i) {
                 $i = maybe_unserialize($i);
                 if (!is_array($i)) {
                     unset($k, $i);
                     continue;
                 }
                 unset($inputs[$k]);
                 foreach ($i as $item) {
                     $inputs[] = $item;
                     unset($item);
                 }
                 unset($k, $i);
             }
             $inputs = array_values($inputs);
         }
         //Strip slashes from inputs
         $inputs = stripslashes_deep($inputs);
     }
     if (isset($form_posts)) {
         unset($form_posts);
     }
     foreach ($f_inputs as $f_id => $f) {
         $f = array_map('maybe_unserialize', $f);
         $f_inputs[$f_id] = stripslashes_deep($f);
         unset($f_id);
         unset($f);
     }
     if (isset($allowed_col_types)) {
         //add columns for each field
         $count = 0;
         foreach ($fields as $f_id => $f) {
             if (isset($tooltip_label[$count]) && !empty($tooltip_label[$count])) {
                 //If tooltip label is set, change the tooltip label
                 $cols[$tooltip_label[$count]] = array('type' => in_array($f->type, $allowed_col_types) ? $f->type : 'number', 'id' => $f->id);
                 $count++;
             } else {
                 //if tooltip label is not set, use the field name
                 $cols[$f->name] = array('type' => in_array($f->type, $allowed_col_types) ? $f->type : 'number', 'id' => $f->id);
             }
             unset($f);
             unset($f_id);
         }
         unset($allowed_col_types);
         unset($count);
     }
     //Declare $field_options variable.
     $field_options = $field->options;
     //Declare $form_posts. This will return empty if the form does not create posts.
     $query = "SELECT id, post_id FROM {$wpdb->prefix}frm_items WHERE form_id = %d AND post_id >= %d" . ($user_id ? " AND user_id=%d" : '') . ($entry ? " AND id in ({$entry})" : '');
     $temp_values = array_filter(array($field->form_id, 1, $user_id));
     $form_posts = $wpdb->get_results($wpdb->prepare($query, $temp_values));
     unset($query);
     unset($temp_values);
     //If form creates posts and graph field is a post field or custom field
     if (!empty($form_posts)) {
         if (isset($field->field_options['post_field']) and $field->field_options['post_field'] != '') {
             if ($field->field_options['post_field'] == 'post_category') {
                 $field_options = FrmProFieldsHelper::get_category_options($field);
             } else {
                 if ($field->field_options['post_field'] == 'post_custom' and $field->field_options['custom_field'] != '') {
                     //check custom fields
                     foreach ($form_posts as $form_post) {
                         $meta_value = get_post_meta($form_post->post_id, $field->field_options['custom_field'], true);
                         if ($meta_value) {
                             if ($x_axis) {
                                 $inputs[] = array('meta_value' => $meta_value, 'item_id' => $form_post->id);
                             } else {
                                 $inputs[] = $meta_value;
                             }
                         }
                     }
                 } else {
                     //if field is post field
                     if ($field->field_options['post_field'] == 'post_status') {
                         $field_options = FrmProFieldsHelper::get_status_options($field);
                     }
                     if (!isset($skip_posts_code)) {
                         foreach ($form_posts as $form_post) {
                             $post_value = $wpdb->get_var("SELECT " . $field->field_options['post_field'] . " FROM {$wpdb->posts} WHERE ID=" . $form_post->post_id);
                             if ($post_value) {
                                 if ($x_axis) {
                                     $inputs[] = array('meta_value' => $post_value, 'item_id' => $form_post->id);
                                 } else {
                                     $inputs[] = $post_value;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($field->type == 'data') {
         foreach ($inputs as $k => $i) {
             if (is_numeric($i)) {
                 if (is_array($inputs[$k]) and isset($inputs[$k]['meta_value'])) {
                     $inputs[$k]['meta_value'] = FrmProFieldsHelper::get_data_value($inputs[$k]['meta_value'], $field, array('truncate' => 'truncate_label'));
                 } else {
                     $inputs[$k] = FrmProFieldsHelper::get_data_value($inputs[$k], $field, array('truncate' => 'truncate_label'));
                 }
             }
             unset($k);
             unset($i);
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $x_temp = array();
         foreach ($x_inputs as $x_input) {
             if (!$x_input) {
                 continue;
             }
             if ($x_field) {
                 $x_temp[$x_input['item_id']] = $x_input['meta_value'];
             } else {
                 $x_temp[$x_input['id']] = $x_input[$x_axis];
             }
         }
         $x_inputs = apply_filters('frm_graph_value', $x_temp, $x_field ? $x_field : $x_axis, $args);
         unset($x_temp);
         unset($x_input);
     }
     if ($x_axis and $inputs) {
         $y_temp = array();
         foreach ($inputs as $input) {
             $y_temp[$input['item_id']] = $input['meta_value'];
         }
         foreach ($ids as $f_id) {
             if (!isset($f_values[$f_id])) {
                 $f_values[$f_id] = array();
             }
             $f_values[$f_id][key($y_temp)] = 0;
             unset($f_id);
         }
         $inputs = $y_temp;
         unset($y_temp);
         unset($input);
     }
     //there is no data, so don't graph
     if (!isset($inputs) || empty($inputs)) {
         //TODO: consolidate?
         return array();
     }
     $inputs = apply_filters('frm_graph_value', $inputs, $field, $args);
     foreach ($f_inputs as $f_id => $f) {
         $temp = array();
         foreach ($f as $input) {
             if (is_array($input)) {
                 $temp[$input['item_id']] = $input['meta_value'];
                 foreach ($ids as $d) {
                     if (!isset($f_values[$d][$input['item_id']])) {
                         $f_values[$d][$input['item_id']] = 0;
                     }
                     unset($d);
                 }
             } else {
                 $temp[] = $input;
             }
             unset($input);
         }
         $f_inputs[$f_id] = apply_filters('frm_graph_value', $temp, $fields[$f_id], $args);
         unset($temp);
         unset($input);
         unset($f);
     }
     $tooltips = array();
     if (in_array($field->type, array('select', 'checkbox', 'radio', 'scale')) and (!isset($x_inputs) or !$x_inputs)) {
         if ($limit == '') {
             $limit = 10;
         }
         if (!in_array($field->type, array('checkbox', 'scale'))) {
             //and count($field_options) == 2
             $pie = true;
         }
         //Set default type to pie if field is radio or select
         $field_opt_count = count($field_options);
         if ($field_options) {
             foreach ($field_options as $opt_key => $opt) {
                 $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field->field_options);
                 $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
                 $count = 0;
                 if (empty($opt)) {
                     continue;
                 }
                 foreach ($inputs as $in) {
                     if (FrmAppHelper::check_selected($in, $field_val)) {
                         if ($data_type == 'total') {
                             $count += $field_val;
                         } else {
                             $count++;
                         }
                     }
                 }
                 $new_val = FrmAppHelper::truncate($opt, $pie && ($type == 'default' || $type == 'pie') ? 50 : $truncate_label, 2);
                 //Prevent label truncation if type is pie
                 if ($count > 0 or $field_opt_count < $limit or !$count and $include_zero) {
                     $labels[$opt] = $new_val;
                     $values[$opt] = $count;
                     $tooltips[$opt] = $opt;
                 }
                 unset($count);
                 foreach ($f_inputs as $f_id => $f) {
                     foreach ($f as $in) {
                         if (!isset($f_values[$f_id])) {
                             $f_values[$f_id] = array();
                         }
                         if (!isset($f_values[$f_id][$new_val])) {
                             $f_values[$f_id][$new_val] = 0;
                         }
                         if (FrmAppHelper::check_selected($in, $field_val)) {
                             if ($data_type == 'total') {
                                 $f_values[$f_id][$new_val] += $field_val;
                             } else {
                                 $f_values[$f_id][$new_val]++;
                             }
                         }
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
             if ($limit != 10 and count($values) > $limit) {
                 $ordered_vals = $values;
                 arsort($ordered_vals);
                 $l_count = 0;
                 foreach ($ordered_vals as $vkey => $v) {
                     $l_count++;
                     if ($l_count > $limit) {
                         unset($values[$vkey]);
                         unset($labels[$vkey]);
                         if (isset($tooltips[$vkey])) {
                             unset($tooltips[$vkey]);
                         }
                     }
                     unset($vkey);
                     unset($v);
                 }
                 unset($l_count);
                 unset($ordered_vals);
             }
         }
     } else {
         if ($field->type == 'user_id') {
             $form = $frmdb->get_one_record($wpdb->prefix . 'frm_forms', array('id' => $field->form_id));
             $form_options = maybe_unserialize($form->options);
             $id_count = array_count_values($inputs);
             if ($form->editable and (isset($form_options['single_entry']) and $form_options['single_entry'] and isset($form_options['single_entry_type']) and $form_options['single_entry_type'] == 'user')) {
                 //if only one response per user, do a pie chart of users who have submitted the form
                 $users_of_blog = get_users();
                 $total_users = count($users_of_blog);
                 unset($users_of_blog);
                 $id_count = count($id_count);
                 $not_completed = (int) $total_users - (int) $id_count;
                 $labels = array(__('Completed', 'formidable'), __('Not Completed', 'formidable'));
                 $values = array($id_count, $not_completed);
                 $pie = true;
             } else {
                 //arsort($id_count);
                 foreach ($id_count as $val => $count) {
                     $user_info = get_userdata($val);
                     $labels[] = $user_info ? $user_info->display_name : __('Deleted User', 'formidable');
                     $values[] = $count;
                 }
                 if (count($labels) < 10) {
                     $pie = true;
                 }
             }
         } else {
             if (isset($x_inputs) and $x_inputs) {
                 $calc_array = array();
                 foreach ($inputs as $entry_id => $in) {
                     $entry_id = (int) $entry_id;
                     if (!isset($values[$entry_id])) {
                         $values[$entry_id] = 0;
                     }
                     $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                     if (!isset($calc_array[$entry_id])) {
                         $calc_array[$entry_id] = array('count' => 0);
                     }
                     if ($data_type == 'total' or $data_type == 'average') {
                         $values[$entry_id] += (double) $in;
                         $calc_array[$entry_id]['total'] = $values[$entry_id];
                         $calc_array[$entry_id]['count']++;
                     } else {
                         $values[$entry_id]++;
                     }
                     unset($entry_id);
                     unset($in);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $entry_id => $calc) {
                         $values[$entry_id] = $calc['total'] / $calc['count'];
                         unset($entry_id);
                         unset($calc);
                     }
                 }
                 $calc_array = array();
                 foreach ($f_inputs as $f_id => $f) {
                     if (!isset($calc_array[$f_id])) {
                         $calc_array[$f_id] = array();
                     }
                     foreach ($f as $entry_id => $in) {
                         $entry_id = (int) $entry_id;
                         if (!isset($labels[$entry_id])) {
                             $labels[$entry_id] = isset($x_inputs[$entry_id]) ? $x_inputs[$entry_id] : '';
                             $values[$entry_id] = 0;
                         }
                         if (!isset($calc_array[$f_id][$entry_id])) {
                             $calc_array[$f_id][$entry_id] = array('count' => 0);
                         }
                         if (!isset($f_values[$f_id][$entry_id])) {
                             $f_values[$f_id][$entry_id] = 0;
                         }
                         if ($data_type == 'total' or $data_type == 'average') {
                             $f_values[$f_id][$entry_id] += (double) $in;
                             $calc_array[$f_id][$entry_id]['total'] = $f_values[$f_id][$entry_id];
                             $calc_array[$f_id][$entry_id]['count']++;
                         } else {
                             $f_values[$f_id][$entry_id]++;
                         }
                         unset($entry_id);
                         unset($in);
                     }
                     unset($f_id);
                     unset($f);
                 }
                 if ($data_type == 'average') {
                     foreach ($calc_array as $f_id => $calc) {
                         foreach ($calc as $entry_id => $c) {
                             $f_values[$f_id][$entry_id] = $c['total'] / $c['count'];
                             unset($entry_id);
                             unset($c);
                         }
                         unset($calc);
                         unset($f_id);
                     }
                 }
                 unset($calc_array);
             } else {
                 $id_count = array_count_values(array_map('strtolower', $inputs));
                 arsort($id_count);
                 $i = 0;
                 foreach ($id_count as $val => $count) {
                     if ($i < $response_count) {
                         if ($field->type == 'user_id') {
                             $user_info = get_userdata($val);
                             $new_val = $user_info->display_name;
                         } else {
                             $new_val = ucwords($val);
                         }
                         $labels[$new_val] = $new_val;
                         $values[$new_val] = $count;
                     }
                     $i++;
                 }
                 foreach ($f_inputs as $f_id => $f) {
                     $id_count = array_count_values(array_map('strtolower', $f));
                     arsort($id_count);
                     $i = 0;
                     foreach ($id_count as $val => $count) {
                         if ($i < $response_count) {
                             if ($field->type == 'user_id') {
                                 $user_info = get_userdata($val);
                                 $new_val = $user_info->display_name;
                             } else {
                                 $new_val = ucwords($val);
                             }
                             $position = array_search($new_val, $labels);
                             if (!$position) {
                                 end($labels);
                                 $position = key($labels);
                                 $labels[$new_val] = $new_val;
                                 $values[$new_val] = 0;
                             }
                             $f_values[$f_id][$new_val] = $count;
                         }
                         $i++;
                     }
                     unset($f_id);
                     unset($f);
                 }
             }
         }
     }
     if (isset($x_inputs) and $x_inputs) {
         $used_vals = $calc_array = array();
         foreach ($labels as $l_key => $label) {
             if (empty($label) and (!empty($x_start) or !empty($x_end))) {
                 unset($values[$l_key]);
                 unset($labels[$l_key]);
                 if (isset($tooltips[$l_key])) {
                     unset($tooltips[$l_key]);
                 }
                 continue;
             }
             if (in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type == 'pie') {
                     $labels[$l_key] = $label = $inputs[$l_key];
                 } else {
                     $labels[$l_key] = $label = date('Y-m-d', strtotime($label));
                 }
             }
             if (isset($used_vals[$label])) {
                 $values[$l_key] += $values[$used_vals[$label]];
                 unset($values[$used_vals[$label]]);
                 foreach ($ids as $f_id) {
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     if (!isset($f_values[$f_id][$used_vals[$label]])) {
                         $f_values[$f_id][$used_vals[$label]] = 0;
                     }
                     $f_values[$f_id][$l_key] += $f_values[$f_id][$used_vals[$label]];
                     unset($f_values[$f_id][$used_vals[$label]]);
                     unset($f_id);
                 }
                 unset($labels[$used_vals[$label]]);
             }
             $used_vals[$label] = $l_key;
             if ($data_type == 'average') {
                 if (!isset($calc_array[$label])) {
                     $calc_array[$label] = 0;
                 }
                 $calc_array[$label]++;
             }
             unset($label);
             unset($l_key);
         }
         if (!empty($calc_array)) {
             foreach ($calc_array as $label => $calc) {
                 if (isset($used_vals[$label])) {
                     $values[$used_vals[$label]] = $values[$used_vals[$label]] / $calc;
                     foreach ($ids as $f_id) {
                         $f_values[$f_id][$used_vals[$label]] = $f_values[$f_id][$used_vals[$label]] / $calc;
                         unset($f_id);
                     }
                 }
                 unset($label);
                 unset($calc);
             }
         }
         unset($used_vals);
     }
     $combine_dates = false;
     if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
         $combine_dates = apply_filters('frm_combine_dates', true, $x_field);
     }
     if ($combine_dates) {
         if ($include_zero) {
             $start_timestamp = empty($x_start) ? time() : strtotime($x_start);
             $end_timestamp = empty($x_end) ? time() : strtotime($x_end);
             $dates_array = array();
             // Get the dates array
             for ($e = $start_timestamp; $e <= $end_timestamp; $e += 60 * 60 * 24) {
                 $dates_array[] = date('Y-m-d', $e);
             }
             unset($e);
             // Add the zero count days
             foreach ($dates_array as $date_str) {
                 if (!in_array($date_str, $labels)) {
                     $labels[$date_str] = $date_str;
                     $values[$date_str] = 0;
                     foreach ($ids as $f_id) {
                         if (!isset($f_values[$f_id][$date_str])) {
                             $f_values[$f_id][$date_str] = 0;
                         }
                     }
                 }
             }
             unset($dates_array);
             unset($start_timestamp);
             unset($end_timestamp);
         }
         asort($labels);
         global $frmpro_settings;
         foreach ($labels as $l_key => $l) {
             if (isset($x_field) and $x_field and $x_field->type == 'date' or in_array($x_axis, array('created_at', 'updated_at'))) {
                 if ($type != 'pie' and preg_match('/^\\d{4}-\\d{2}-\\d{2}$/', $l)) {
                     global $frmpro_settings;
                     $labels[$l_key] = FrmProAppHelper::convert_date($l, 'Y-m-d', $frmpro_settings->date_format);
                 }
             }
             unset($l_key);
             unset($l);
         }
         $values = FrmProAppHelper::sort_by_array($values, array_keys($labels));
         $tooltips = FrmProAppHelper::sort_by_array($tooltips, array_keys($labels));
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::sort_by_array($f_values[$f_id], array_keys($labels));
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
     } else {
         if (isset($x_inputs) and $x_inputs) {
             foreach ($labels as $l_key => $l) {
                 foreach ($ids as $f_id) {
                     //do a last check to make sure all bars/lines have a value for each label
                     if (!isset($f_values[$f_id][$l_key])) {
                         $f_values[$f_id][$l_key] = 0;
                     }
                     unset($fid);
                 }
                 unset($l_key);
                 unset($l);
             }
         }
         foreach ($ids as $f_id) {
             $f_values[$f_id] = FrmProAppHelper::reset_keys($f_values[$f_id]);
             ksort($f_values[$f_id]);
             unset($f_id);
         }
         if ($x_order == '1') {
             ksort($labels);
             ksort($values);
             ksort($tooltips);
         }
     }
     $labels = FrmProAppHelper::reset_keys($labels);
     $values = FrmProAppHelper::reset_keys($values);
     $tooltips = FrmProAppHelper::reset_keys($tooltips);
     //Graph by month or quarter
     if (isset($group_by) && in_array($group_by, array('month', 'quarter'))) {
         foreach ($labels as $key => $label) {
             if ($group_by == 'month') {
                 $labels[$key] = date('F Y', strtotime($label));
             } else {
                 if ($group_by == 'quarter') {
                     $label = date('Y-m-d', strtotime($label));
                     //Convert date to Y-m-d format
                     if (preg_match('/-(01|02|03)-/', $label)) {
                         $labels[$key] = 'Q1 ' . date('Y', strtotime($label));
                     } else {
                         if (preg_match('/-(04|05|06)-/', $label)) {
                             $labels[$key] = 'Q2 ' . date('Y', strtotime($label));
                         } else {
                             if (preg_match('/-(07|08|09)-/', $label)) {
                                 $labels[$key] = 'Q3 ' . date('Y', strtotime($label));
                             } else {
                                 if (preg_match('/-(10|11|12)-/', $label)) {
                                     $labels[$key] = 'Q4 ' . date('Y', strtotime($label));
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $count = count($labels) - 1;
         for ($i = 0; $i < $count; $i++) {
             if ($labels[$i] == $labels[$i + 1]) {
                 unset($labels[$i]);
                 $values[$i + 1] = $values[$i] + $values[$i + 1];
                 unset($values[$i]);
             }
         }
         $labels = FrmProAppHelper::reset_keys($labels);
         $values = FrmProAppHelper::reset_keys($values);
     }
     $labels = apply_filters('frm_graph_labels', $labels, $args, $field);
     $values = apply_filters('frm_final_graph_values', $values, $args, $field);
     $return = array('total_count' => count($inputs), 'f_values' => $f_values, 'labels' => $labels, 'values' => $values, 'pie' => $pie, 'combine_dates' => $combine_dates, 'ids' => $ids, 'cols' => $cols, 'rows' => $rows, 'options' => $options, 'fields' => $fields, 'tooltips' => $tooltips);
     if (isset($x_inputs)) {
         $return['x_inputs'] = $x_inputs;
     }
     return $return;
 }