Open Flash Chart API2

1 篇文章 / 0 new
author
Open Flash Chart API2

使用 Open Flash Chart API2 來顯示圖表, 雖然原始網站似乎沒有再對此API繼續發展, 但一般用途已足夠了
ofc_api 則是由 drupal6 修改而來, 目前已可使用, 是否潛藏問題, 尚不知. API 下載
使用方式有三
1. 直接使用 API 方式作業.
function ofcAPI() {
    // data and labels
    $data = array(180, 201, 3318, 4284, 5635, 6281, 6528, 6460);
    $title = new title('2012 Learning Commons Computer Usage, by Week');
    $d = new hollow_dot();
    $d->size(4)->halo_size(1)->colour('#006666')->tooltip("Week ending #x_label# #val# Logins");
    // define line appearance
    $line = new line();
    $line->set_values($data);
//$line->set_default_dot_style($d);
    $line->set_width(4);
    $line->set_colour('#006666');
// define x-axis
    $x = new x_axis();
    $x->colour('#DDDDDD')
            ->grid_colour('#DDDDDD')
            ->tick_height(5)
            ->stroke(3);
    $x->set_labels_from_array("labels");
    // set the range of values for the y-axis
    $low_value = 0;
    $high_value = 7000;
    $step_value = 1000;
// define y-axis
    $y = new y_axis();
    $y->set_stroke(3);
    $y->set_colour('#DDDDDD');
    $y->set_tick_length(5);
    $y->set_grid_colour('#DDDDDD');
    $y->set_range($low_value, $high_value, $step_value);
 
    $chart = new open_flash_chart();
    $chart->set_title(array("text" => "title"));
    $chart->add_element($line);
    $chart->set_bg_colour('#EEEEEE');
    $chart->set_x_axis($x);
    $chart->set_y_axis($y);
    $cc = '<div class="dash-chart-text">';
    $cc .= ofc_api_render($chart, 400, 250, array('wmode' => 'opaque'));
    $cc .= '</div>';
    $cc .= 'The approximately 100 computers...';
 
    return $cc;
}
2. 透過 swfobject.js 啟動
function ofcswfobject() {<br />
    drupal_add_js(drupal_get_path('module', 'ofc_api') . '/swfobject.js');<br />
    drupal_add_js(drupal_get_path('module', 'ofc_api') . '/json2.js');<br />
    $json = '{"elements":[{"type":"bar","values":[1,2,3,4,5,6,7,8,9]}],"title":{"text":"Sun Jan 12 2014"}}';<br />
    $id = ofc_api_unique_id();<br />
    $data = array(<br />
        '@swf' => url(drupal_get_path('module', 'ofc_api') . '/open-flash-chart.swf'),<br />
        '@id' => $id,<br />
        '@width' => 400,<br />
        '@height' => 200,<br />
        '@method' => 'get-data',<br />
        '@source' => $id,<br />
        '@json' => $json,<br />
    );<br />
    $js = "function @id() { return JSON.stringify(@json); }";<br />
    $js = str_replace(array_keys($data), array_values($data), $js);<br />
    drupal_add_js($js, 'inline', 'header');<br />
<br />
    $js = 'swfobject.registerObject("@id", "9.0.0", false)';<br />
    $js = str_replace(array_keys($data), array_values($data), $js);<br />
    drupal_add_js($js, 'inline', 'header');<br />
    $output[] = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="@width" height="@height" id="@id">';<br />
    $output[] = '<param name="movie" value="@swf" />';<br />
    $output[] = '<param name="flashvars" value="@method=@source" />';<br />
    $output[] = '<!--[if !IE]>-->';<br />
    $output[] = '<object type="application/x-shockwave-flash" data="@swf" width="@width" height="@height">';<br />
    $output[] = '<param name="flashvars" value="@method=@source" />';<br />
    //foreach ($params as $name => $value) {<br />
    //    $output[] = '<param name="' . $name . '" value="' . $value . '" />';<br />
    //}<br />
    $output[] = '<!--<![endif]-->';<br />
    //$output[] = $alt_content;<br />
    $output[] = '<!--[if !IE]>-->';<br />
    $output[] = '</object>';<br />
    $output[] = '<!--<![endif]-->';<br />
    $output[] = '</object>';<br />
    return implode('', str_replace(array_keys($data), array_values($data), $output));<br />
}
3. 透過 open-flash-chart.swf 啟動
function ofcSWF() {
    drupal_add_js(drupal_get_path('module', 'ofc_api') . '/swfobject.js');
    drupal_add_js(drupal_get_path('module', 'ofc_api') . '/json2.js');
    $id = ofc_api_unique_id();
    $json = '{"elements":[{"type":"bar","values":[6,2,6,4,5,6,7,8,9]}],"title":{"text":"Sun Jan 12 2014"}}';
 
    $data = array(
        '@swf' => url(drupal_get_path('module', 'ofc_api') . '/open-flash-chart.swf'),
        '@id' => $id,
        '@width' => 400,
        '@height' => 200,
        '@method' => 'get-data',
        '@source' => $id,
        '@json' => $json,
    );
    $js = "function @id() { return JSON.stringify(@json); }";
    $js = str_replace(array_keys($data), array_values($data), $js);
    drupal_add_js($js, 'inline', 'header');
    //
    $js = 'swfobject.embedSWF("@swf", "@id", "@width", "@height", "9.0.0", "expressInstall.swf", {"@method":"@source"} )';
    $js = str_replace(array_keys($data), array_values($data), $js);
    drupal_add_js($js, 'inline', 'header');
    $output[] = '<div id="@id">';
    //$output[] = $alt_content;
    $output[] = '</div>';
    return implode('', str_replace(array_keys($data), array_values($data), $output));
}
Free Web Hosting