goat1000 / SVGGraph

PHP SVG graph library
http://www.goat1000.com/svggraph.php
GNU Lesser General Public License v3.0
120 stars 31 forks source link

Logarithmic scale for horizontal axis #51

Open Looper1984 opened 3 years ago

Looper1984 commented 3 years ago

Hi, I was wondering if you could add the option to use a logarithmic scale for the horizontal axis. It would be quite useful for diagrams which contain audio frequencies, as these are usually drawn on a logarithmic scale.

Cheers

goat1000 commented 2 years ago

I've added this to my to-do list - I'll also see if I can come up with a simple "getting started" instructions page.

Looper1984 commented 2 years ago

That's great. Thank you so much! :)

goat1000 commented 2 years ago

Changeset 82ddd8f09b437e4e704f32e103fc3bf7b70ba951 adds support for log_axis_x and log_axis_x_base options.

It works best with a line or scatter graph, but it will work with bar graphs if there are integer keys.

Looper1984 commented 2 years ago

Sweet, thank you for this great feature. :)

the 'axis_min_h' and 'axis_max_h' options don't seem to be working as expected for logarithmic scales. Both of these are fed with the same data and set to display values between 20 and 20,000:

linlogcomparison

`if($RANGE=='sonic' && $SCALE=='lin') { $LOGAXIS=false; $TICKSX=[ 20, 2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000 ]; $MINH=20; $MAXH=20000; }

if($RANGE=='sonic' && $SCALE=='log') { $LOGAXIS=true; $TICKSX=[ 20, 2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000 ]; $MINH=20; $MAXH=20000; }

require_once './SVGGraph/autoloader.php'; $settings = [ 'auto_fit' => true, 'back_colour' => '#fff', 'back_stroke_width' => 0, 'back_stroke_colour' => '#eee', 'stroke_colour' => '#1089da', 'line_stroke_width' => 2, 'axis_colour' => '#444', 'axis_overlap' => 0, 'grid_colour' => '#bbb', 'label_colour' => '#000', 'axis_font' => 'Spartan', 'axis_font_size' => 10, 'fill_under' => false, 'pad_right' => 0, 'pad_left' => 0, 'marker_type' => 'circle', 'marker_size' => NULL, 'marker_colour' => '#1089da', 'link_base' => '/', 'link_target' => '_top', 'minimum_grid_spacing' => 20, 'show_subdivisions' => true, 'show_grid_subdivisions' => true, 'grid_subdivision_colour' => '#eee', 'label_font' => 'Spartan', 'label_h' => 'Frequency (Hz)', 'label_v' => 'dB (V)', 'axis_min_h' => $MINH, 'axis_max_h' => $MAXH, 'axis_min_v' => $MINV, 'axis_max_v' => $MAXV, 'axis_ticks_x' => $TICKSX, 'log_axis_x' => $LOGAXIS, 'structured_data' => true, 'structure' => ['key' => 0, 'value' => 1], ];

$graph = new Goat1000\SVGGraph\SVGGraph(730, 300, $settings);

$type ='LineGraph';

$graph->values($FRRES);

echo $graph->fetch($type, $header=false); ` Is this another user error on my part?

goat1000 commented 2 years ago

No, this is because the log axis is not very good at defining its end points. I'll see if I can improve it.

goat1000 commented 2 years ago

273927eaee7a26ec33ae8e45bcab2fc5fb9f2325 adds support for arbitrary tick marks and improves the start and end points.

Looper1984 commented 2 years ago

Nice! It works very well for me now. Thank you so much!