Skip to content

Instantly share code, notes, and snippets.

@Viper007Bond
Created March 13, 2012 01:16
Show Gist options
  • Save Viper007Bond/2025993 to your computer and use it in GitHub Desktop.
Save Viper007Bond/2025993 to your computer and use it in GitHub Desktop.
Graphing car mileage
<?php
/**
* All of this code is in a big class that implements things like meta boxes and
* tons of other stuff but for now here's the relevant functions.
*
* Note the custom post types and custom taxonomies are just in placeholder form.
* I haven't gone through and properly set the labels, etc. for them like I will
* when I release the plugin.
*/
public function action_init() {
register_post_type( 'carinfo_fillup', array(
'labels' => array(
'name' => 'Fuel Fill Ups',
'singular_name' => 'Fuel Fill Up',
'add_new' => 'Add New Fuel Fill Up',
'edit_item' => 'Edit Fuel Fill Up',
'new_item' => 'New Fuel Fill Up',
'all_items' => 'All Fuel Fill Ups',
'view_item' => 'View Fuel Fill Up',
'search_items' => 'Search Fuel Fill Ups',
'menu_name' => 'Car Fuel Fill Ups',
),
'public' => true,
'supports' => array( 'editor', 'custom-fields' ), // custom-fields for debugging
'exclude_from_search' => true,
) );
register_post_type( 'carinfo_service', array(
'label' => 'Car Services',
'public' => true,
'supports' => array( 'title', 'editor', 'custom-fields' ),
) );
register_taxonomy( 'carinfo_car', array( 'carinfo_fillup', 'carinfo_service' ), array(
'label' => 'Cars',
'hierarchical' => true,
) );
register_taxonomy( 'carinfo_location', array( 'carinfo_fillup', 'carinfo_service' ), array(
'hierarchical' => true,
'labels' => array(
'name' => 'Locations',
),
) );
register_taxonomy( 'carinfo_fuel_type', 'carinfo_fillup', array(
'label' => 'Fuel Types',
'hierarchical' => true,
) );
register_taxonomy( 'carinfo_fuel_brand', 'carinfo_fillup', array(
'label' => 'Fuel Brands',
'hierarchical' => true,
) );
register_taxonomy( 'carinfo_service_type', 'carinfo_service', array(
'label' => 'Services',
'hierarchical' => true,
) );
register_taxonomy( 'carinfo_payment_type', array( 'carinfo_fillup', 'carinfo_service' ), array(
'label' => 'Payment Types',
'hierarchical' => true,
) );
wp_register_script( 'google-jsapi', 'https://www.google.com/jsapi', array(), false, true );
}
public function shortcode_graph_mileage( $atts ) {
global $content_width;
$atts = shortcode_atts( array(
'vehicle' => null,
'title' => null,
'width' => $content_width,
'height' => null,
), $atts );
if ( empty( $atts['vehicle'] ) )
return 'Missing vehicle parameter';
if ( ! $vehicle = get_term_by( 'slug', addslashes( $atts['vehicle'] ), 'carinfo_car' ) )
return 'Invalid vehicle slug';
$fillups = new WP_Query( array(
'post_type' => 'carinfo_fillup',
'posts_per_page' => -1,
/*
'tax_query' => array(
array(
'taxonomy' => 'carinfo_car',
'field' => 'id',
'terms' => array( $vehicle->term_id ),
),
),
*/
'orderby' => 'post_date',
'order' => 'ASC',
) );
if ( ! $fillups->have_posts() )
return 'No fillups found. Could be none exist or could be an invalid vehicle name was specified.';
$atts['width'] = absint( $atts['width'] );
$atts['height'] = absint( $atts['height'] );
if ( $atts['width'] < 1 )
$atts['width'] = 400;
if ( $atts['height'] < 1 )
$atts['height'] = 300;
if ( is_null( $atts['title'] ) )
$atts['title'] = 'Odometer Reading For ' . $vehicle->name;
wp_enqueue_script( 'google-jsapi' );
add_action( 'wp_print_footer_scripts', array( &$this, 'google_jsapi' ) );
$this->graph_incrementor++;
$graph = array(
'id' => 'vehicleinfo_graph' . $this->graph_incrementor,
'width' => $atts['width'],
'height' => $atts['height'],
'title' => $atts['title'],
'data' => array(
'cols' => array(
array(
'id' => 'date',
'label' => 'Date',
'type' => 'date',
),
array(
'id' => 'odometer',
'label' => 'Miles',
'type' => 'number',
),
),
'rows' => array(),
),
);
foreach ( $fillups->posts as $fillup ) {
$odometer = round( get_post_meta( $fillup->ID, 'carinfo_odometer', true ) );
$graph['data']['rows'][] = array(
'c' => array(
array(
'v' => (int) mysql2date( 'U', $fillup->post_date ),
'f' => mysql2date( get_option( 'date_format' ), $fillup->post_date ),
),
array( 'v' => $odometer ),
),
);
}
$this->graphs[] = $graph;
return '<div id="' . esc_attr( $graph['id'] ) . '" style="width:' . intval( $graph['width'] ) . 'px;height:' . intval( $graph['height'] ) . 'px;"></div>';
}
public function google_jsapi() {
global $content_width;
?>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(VehicleInfo_DrawCharts);
function VehicleInfo_DrawCharts() {
// All graph data
var graphs = <?php echo json_encode( $this->graphs ); ?>;
// Draw each graph
for ( var g = 0; g < graphs.length; g++ ) {
// Convert Unix timestamps to Date() objects
for ( var gd = 0; gd < graphs[g].data.rows.length; gd++ ) {
graphs[g].data.rows[gd].c[0].v = new Date( graphs[g].data.rows[gd].c[0].v * 1000 );
}
var data = new google.visualization.DataTable( graphs[g].data );
var options = {
'title':graphs[g].title,
'width':graphs[g].width,
'height':graphs[g].height,
'pointSize':5,
};
var chart = new google.visualization.LineChart( document.getElementById( graphs[g].id ) );
chart.draw( data, options );
}
}
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment