diff --git a/chart.module b/chart.module
index 5a3e374..5d48ccc 100644
--- a/chart.module
+++ b/chart.module
@@ -284,6 +284,7 @@ function chart_build($chart) {
       '#bar_size' => '',
       '#countries' => array(),
       '#georange' => '',
+      '#margins' => array(),
     );
 
     // Allow modules to alter a chart after defaults have been added.
@@ -318,6 +319,7 @@ function chart_build($chart) {
     _chart_append('chbh',  $chart['#bar_size'],                $data);
     _chart_append('chld',  $chart['#countries'],               $data);
     _chart_append('chtm',  $chart['#georange'],                $data);
+    _chart_append('chma',  $chart['#margins'],                 $data);
 
     $charts[$chart['#chart_id']] = $data;
   }
@@ -712,6 +714,19 @@ function _chart_append($attr, $value, &$data) {
     case 'chld':
       $data[$attr] = implode('', $value);
       break;
+
+     // Margins
+    case 'chma':
+      $chma = array($value['#left_margin'],
+                    $value['#right_margin'],
+                    $value['#top_margin'],
+                    $value['#bottom_margin']);
+      $data[$attr] = implode(',', $chma);
+      //Optional legend values
+      if (isset($value['#legend_width']) && isset($value['#legend_height'])) {
+        $data[$attr] .= '|' . $value['#legend_width'] . "," . $value['#legend_height'];
+      }
+      break;
   }
 }
 
@@ -1171,6 +1186,44 @@ function chart_linear_stripes($type = 'c', $color = '000000', $angle = 0, $width
   );
 }
 
+/**
+ * Margins
+ *
+ * @param int $left
+ *   Left margin size in pixels
+ *
+ * @param int $right
+ *   Right margin size in pixels
+ *
+ * @param int $top
+ *   Top margin size in pixels
+ *
+ * @param int $bottom
+ *   Bottom margin size in pixels
+ *
+ * @param int $legend_width
+ *   (optional) Width of legend margin
+ *
+ * @param int $legend_height
+ *   (optional) Height of legend margin
+ *
+ * @return array
+ */
+function chart_margins($left, $right, $top, $bottom, $legend_width = NULL, $legend_height = NULL) {
+  $margins = array(
+    '#left_margin' => $left,
+    '#right_margin' => $right,
+    '#top_margin' => $top,
+    '#bottom_margin' => $bottom,
+  );
+  if ($legend_width && $legend_height) {
+    $margins['#legend_width'] = $legend_width;
+    $margins['#legend_height'] = $legend_height;
+  }
+  return $margins;
+}
+
+
 /*-----------------------------------------------------------------
  * Color Schemes
  *------------------------------------------------------------------*/
