//Assign values and labels switch ($wpbi_chart->type) { case chart::DONUT: case chart::PIE: if (sizeof($label_x) > 0) { $wpbi_chart->set_x_axis_labels($label_x, $vo_chart->chart_x_labels_size, $vo_chart->chart_x_labels_color); } foreach ($data as $key => $value) { //Overwrite data value for pie chart in order to show labels (via pie_value object) if (sizeof($label_x) > 0) { for ($idx = 0; $idx < sizeof($value); $idx++) { $value[$idx] = new pie_value($value[$idx], $label_x[$idx]); } } $wpbi_chart->set_tooltip($wpbi_dialog['charts']['pie']['tooltip']); $wpbi_chart->create_element($key, $value); $wpbi_chart->elements[$key]->set_colours($wpbi_settings['pie-chart']['color-set']); } break; case chart::BAR_STACKED: if (sizeof($label_x) > 0) { $wpbi_chart->set_x_axis_labels($label_x, $_POST[$wpbi_settings['parameter']['ch-x-label-size']], $_POST[$wpbi_settings['parameter']['ch-x-label-color']]); $wpbi_chart->x_axis_istime = sizeof($istime_cols) > 0; if ($wpbi_chart->x_axis_istime) { $wpbi_chart->x_axis_labels->labels = $wpbi_chart->convert_to_time($wpbi_chart->x_axis_labels->labels); } } $wpbi_chart->set_y_axis_labels_color($_POST[$wpbi_settings['parameter']['ch-y-label-color']]); $wpbi_chart->set_y_axis_labels_size($_POST[$wpbi_settings['parameter']['ch-y-label-size']]); foreach ($data_stacked as $key => $value) { $wpbi_chart->create_element('BAR_STACKED', $value);
function get_html_4_chart($id) { global $wpdb, $qy_table_databases, $qy_table_queries, $wpbi_sql, $wpbi_settings, $wpbi_url, $wpbi_dialog, $lng, $language, $template_site; $template_site->set_filenames(array('chart' => $wpbi_url['tpl']['root-path'] . $wpbi_url['tpl']['nvd3chart'])); $chart_id = $id; //id or key $search_by_key = false; if (!is_numeric($chart_id)) { if (strlen($chart_id) == 32 && strpos($chart_id, ' ') === false) { $search_by_key = true; } else { return '<p>Could not create table. Wrong ID provided.</p>'; } } $test_output = ''; //Store the test output //Get chart metadata $selected_charts = $chart_id; $vo_chart = new vo_chart(NULL); $vo_chart->set_chart_id($selected_charts); $vo_chart->set_chart_key($selected_charts); $dao_chart = new dao_chart($wpdb, $wpbi_sql['tname']['charts']); $vo_chart = $search_by_key ? $dao_chart->select_by_key($vo_chart) : $dao_chart->select($vo_chart); $vo_chart = $vo_chart[0]; //Get cols metadata $vo_ch_cols = new vo_ch_cols(); $vo_ch_cols->set_ch_id($vo_chart->chart_id); $dao_ch_cols = new dao_ch_cols($wpdb, $wpbi_sql['tname']['chart-cols']); $vo_ch_cols = $dao_ch_cols->select($vo_ch_cols); //Get selected query $dao_query = new dao_query($wpdb, $wpbi_sql['tname']['queries']); $tgt_query = new vo_query($vo_chart->query_id, NULL, NULL, NULL, NULL); $vo_query = $dao_query->select($tgt_query); $vo_query = $vo_query[0]; //Select associated db $dao_database = new dao_database($wpdb, $wpbi_sql['tname']['databases']); $tgt_database = new vo_database($vo_query->db_id, NULL, NULL, NULL, NULL); $vo_database = $dao_database->select($tgt_database); $vo_database = $vo_database[0]; //Create wpdb object and execute the query $my_test_db = new wpdb($vo_database->user, $vo_database->pass, $vo_database->name, $vo_database->host); $query = new query($vo_query->statement, $wpdb, $wpbi_sql['tname']['vars']); $total_rows = $my_test_db->get_results($query->count_qy_results(), 'ARRAY_N'); if (count($total_rows) == 0) { return "<div style='padding:40px;'><h1 style='color: red;'>The query returned no data</h1>" . "<p>Please check the DB connectivity or your SQL query syntax</p></div>"; } $total_rows = intval($total_rows[0][0]); //get query resultset $my_test_rows = $my_test_db->get_results($query->stmt, 'ARRAY_N'); //get columns $x_label_cols = array(); $color_cols = array(); $tx_label_cols = array(); $values_cols = array(); $istime_cols = array(); $stacked_label_cols = array(); $stacked_label_cols_color = array(); $col_idx = 0; foreach ($vo_ch_cols as $vo_ch_col) { array_push($tx_label_cols, $vo_ch_col->col_label); array_push($color_cols, $vo_ch_col->col_color); if ($vo_ch_col->is_label) { array_push($x_label_cols, $col_idx); } if ($vo_ch_col->is_value) { array_push($values_cols, $col_idx); array_push($stacked_label_cols, $vo_ch_col->col_label); array_push($stacked_label_cols_color, $vo_ch_col->col_color); } if ($vo_ch_col->is_time) { array_push($istime_cols, $col_idx); } $col_idx++; } //Create chart $wpbi_chart = new chart($my_test_rows); $wpbi_chart->set_name($vo_chart->chart_name); $wpbi_chart->set_tooltip($vo_chart->chart_tooltip); $wpbi_chart->set_width($vo_chart->chart_width . ($vo_chart->chart_width_percent ? '%' : '')); $wpbi_chart->set_height($vo_chart->chart_height . ($vo_chart->chart_height_percent ? '%' : '')); $wpbi_chart->set_bg_colour($vo_chart->chart_bg_color); $wpbi_chart->set_title($vo_chart->chart_title); $wpbi_chart->set_title_color($vo_chart->chart_title_color); $wpbi_chart->set_title_size($vo_chart->chart_title_size); $wpbi_chart->set_type($vo_chart->chart_type); $wpbi_chart->set_x_axis_step_percent($vo_chart->chart_x_grid_lines); $wpbi_chart->set_y_axis_step_percent($vo_chart->chart_y_grid_lines); $wpbi_chart->set_time_format($vo_chart->chart_time_format); //Get values, labels, colors $label_color = array(); $stacked_label_color = array(); $row_idx = 0; $label_tmp_x = ""; foreach ($my_test_rows as $my_test_row) { for ($col_idx = 0; $col_idx < sizeof($my_test_row); $col_idx++) { //Colors $label_color[$tx_label_cols[$col_idx]] = $color_cols[$col_idx]; if (in_array($col_idx, $values_cols)) { $stacked_label_color[$tx_label_cols[$col_idx]] = $color_cols[$col_idx]; } if (in_array($col_idx, $values_cols)) { //create different array of values for each selected column $current_value = is_string($my_test_row[$col_idx]) ? $my_test_row[$col_idx] : floatval($my_test_row[$col_idx]); if (isset($data[$tx_label_cols[$col_idx]])) { array_push($data[$tx_label_cols[$col_idx]], $current_value); } else { $data[$tx_label_cols[$col_idx]] = array(); array_push($data[$tx_label_cols[$col_idx]], $current_value); } } if (in_array($col_idx, $values_cols)) { //create different array of values for each selected column (for stacked bar chart) $current_value = floatval($my_test_row[$col_idx]); if (isset($data_stacked[$row_idx])) { array_push($data_stacked[$row_idx], $current_value); } else { $data_stacked[$row_idx] = array(); array_push($data_stacked[$row_idx], $current_value); } } if (in_array($col_idx, $x_label_cols)) { //Concatenate selected columns $label_tmp_x = $label_tmp_x == '' ? $my_test_row[$col_idx] : $label_tmp_x . $wpbi_dialog['charts']['x-label']['concat-string'] . $my_test_row[$col_idx]; } } if ($label_tmp_x != NULL) { $label_x[] = $label_tmp_x; $label_tmp_x = ''; } $row_idx++; } //Assign values and labels switch ($wpbi_chart->type) { case chart::DONUT: case chart::PIE: if (sizeof($label_x) > 0) { $wpbi_chart->set_x_axis_labels($label_x, $vo_chart->chart_x_labels_size, $vo_chart->chart_x_labels_color); } foreach ($data as $key => $value) { //Overwrite data value for pie chart in order to show labels (via pie_value object) if (sizeof($label_x) > 0) { for ($idx = 0; $idx < sizeof($value); $idx++) { $value[$idx] = new pie_value($value[$idx], $label_x[$idx]); } } $wpbi_chart->set_tooltip($wpbi_dialog['charts']['pie']['tooltip']); $wpbi_chart->create_element($key, $value); $wpbi_chart->elements[$key]->set_colours($wpbi_settings['pie-chart']['color-set']); } break; case chart::BAR_STACKED: if (sizeof($label_x) > 0) { $wpbi_chart->set_x_axis_labels($label_x, $_POST[$wpbi_settings['parameter']['ch-x-label-size']], $_POST[$wpbi_settings['parameter']['ch-x-label-color']]); $wpbi_chart->x_axis_istime = sizeof($istime_cols) > 0; if ($wpbi_chart->x_axis_istime) { $wpbi_chart->x_axis_labels->labels = $wpbi_chart->convert_to_time($wpbi_chart->x_axis_labels->labels); } } $wpbi_chart->set_y_axis_labels_color($_POST[$wpbi_settings['parameter']['ch-y-label-color']]); $wpbi_chart->set_y_axis_labels_size($_POST[$wpbi_settings['parameter']['ch-y-label-size']]); foreach ($data_stacked as $key => $value) { $wpbi_chart->create_element('BAR_STACKED', $value); $wpbi_chart->elements['BAR_STACKED']->set_colours($stacked_label_color); } break; case chart::STACKED_AREA: case chart::LINE_AREA: if (sizeof($label_x) > 0) { $wpbi_chart->set_x_axis_labels($label_x, $vo_chart->chart_x_labels_size, $vo_chart->chart_x_labels_color); } $wpbi_chart->set_y_axis_labels_color($vo_chart->chart_y_labels_color); $wpbi_chart->set_y_axis_labels_size($vo_chart->chart_y_labels_size); foreach ($data as $key => $value) { $wpbi_chart->create_element($key, $value); $wpbi_chart->elements[$key]->set_colour($label_color[$key]); $wpbi_chart->elements[$key]->set_fill_colour($label_color[$key]); $wpbi_chart->elements[$key]->set_fill_alpha(0.5); } break; case chart::RADAR: if (sizeof($label_x) > 0) { $wpbi_chart->set_y_axis_labels($label_x, $vo_chart->chart_y_labels_size, $vo_chart->chart_y_labels_color); } foreach ($data as $key => $value) { $wpbi_chart->create_element($key, $value); $wpbi_chart->elements[$key]->set_colour($label_color[$key]); } break; case chart::BAR_HORIZONTAL: if (sizeof($label_x) > 0) { $wpbi_chart->set_y_axis_labels($label_x, $vo_chart->chart_y_labels_size, $vo_chart->chart_y_labels_color); } $wpbi_chart->set_x_axis_labels_color($vo_chart->chart_x_labels_color); $wpbi_chart->set_x_axis_labels_size($vo_chart->chart_x_labels_size); foreach ($data as $key => $value) { $wpbi_chart->create_element($key, $value); $wpbi_chart->elements[$key]->set_colour($label_color[$key]); } break; default: if (sizeof($label_x) > 0) { $wpbi_chart->set_x_axis_labels($label_x, $vo_chart->chart_x_labels_size, $vo_chart->chart_x_labels_color, sizeof($istime_cols) > 0); $wpbi_chart->x_axis_istime = sizeof($istime_cols) > 0; } else { $wpbi_chart->set_x_axis_labels_color($vo_chart->chart_x_labels_color); $wpbi_chart->set_x_axis_labels_size($vo_chart->chart_x_labels_size); } $wpbi_chart->set_y_axis_labels_color($vo_chart->chart_y_labels_color); $wpbi_chart->set_y_axis_labels_size($vo_chart->chart_y_labels_size); foreach ($data as $key => $value) { $wpbi_chart->create_element($key, $value); $wpbi_chart->elements[$key]->set_colour($label_color[$key]); } break; } //Set legends $wpbi_chart->set_y_legend($vo_chart->chart_y_legend, $vo_chart->chart_y_legend_size, $vo_chart->chart_y_legend_color); $wpbi_chart->set_x_legend($vo_chart->chart_x_legend, $vo_chart->chart_x_legend_size, $vo_chart->chart_x_legend_color); $wpbi_chart->set_x_label($vo_chart->chart_x_axis_label); $wpbi_chart->set_x_axis_labels_rotation($vo_chart->chart_x_labels_rotation); $wpbi_chart->set_y_axis_labels_rotation($vo_chart->chart_y_labels_rotation); $wpbi_chart->set_y_axis_color($vo_chart->chart_y_color); $wpbi_chart->set_y_axis_grid_color($vo_chart->chart_y_grid_color); $wpbi_chart->set_y_axis_thickness($vo_chart->chart_y_thickness); $wpbi_chart->set_x_axis_color($vo_chart->chart_x_color); $wpbi_chart->set_x_axis_grid_color($vo_chart->chart_x_grid_color); $wpbi_chart->set_x_axis_thickness($vo_chart->chart_x_thickness); $wpbi_chart->set_x_precision($vo_chart->chart_x_axis_precision); $wpbi_chart->set_y_precision($vo_chart->chart_y_axis_precision); $wpbi_chart->set_y_range($vo_chart->chart_y_axis_range); $wpbi_chart->set_y_label($vo_chart->chart_y_axis_label); $wpbi_chart->set_y_currency($vo_chart->chart_y_axis_currency); $wpbi_chart->set_snapshot($vo_chart->chart_snapshot == 1); $wpbi_chart->set_stacked($vo_chart->chart_stacked == 1); //Main legend for stacked chart if ($wpbi_chart->type == chart::BAR_STACKED) { $stacked_keys = array(); for ($i = 0; $i < sizeof($stacked_label_cols_color); $i++) { array_push($stacked_keys, new bar_stack_key($stacked_label_cols_color[$i], $stacked_label_cols[$i], 10)); } $wpbi_chart->elements['BAR_STACKED']->set_keys($stacked_keys); } $wpbi_chart->build(); //Import scripts echo ' <link rel="stylesheet" href="' . $wpbi_url['nvd3']['css'] . '" type="text/css" /> <script type="text/javascript" src="' . $wpbi_url['nvd3']['d3js'] . '"></script> <script type="text/javascript" src="' . $wpbi_url['nvd3']['nvd3'] . '"></script> '; //Set template variables $template_site->assign_vars(array('CH_NEW_CHART_NAME' => $selected_charts, 'CH_NEW_CHART_WIDTH' => $wpbi_chart->width, 'CH_NEW_CHART_HEIGHT' => $wpbi_chart->height, 'CH_NEW_CHART_RESIZE' => 'chart_resize', 'CH_NEW_CHART_JSON' => $wpbi_chart->get_json_code(), 'CH_NEW_CHART_NVD3_CODE' => $wpbi_chart->get_nvd3_chart_code(), 'CH_NEW_CHART_NVD3_HTML' => $wpbi_chart->get_nvd3_chart_html(), 'CH_NEW_CHART_NVD3_DATA' => $wpbi_chart->get_nvd3_chart_data(), 'CH_NEW_CHART_NVD3_PLACEHOLDER' => $wpbi_chart->get_nvd3_chart_placeholder())); ob_start(); $template_site->pparse('chart'); $chart_output = ob_get_contents(); ob_end_clean(); return $chart_output; }