Need to reference largest value from block data in block view.php
Permalink
I have a custom block that displays as a Google chart via javascript in the view.php. I've customized the vertical axis using custom ticks - but now the chart shows all the custom ticks even if the data doesn't require them all.
So what I've tried to do is figure out what the highest value in my data set is, and then customize the ticks based on that. So if the largest value in my data is 12,345,678, I would have custom ticks for 5M, 10M and 15M; if the highest value is 17,890,123 I'd also have 20M, etc.
I've tried using this code to set a $largest variable to the largest number in the data set and then pass that along to view.php:
And then reference the code in view.php:
I don't get any errors, the largest number in the data is much higher than 5000000 and I know the addon to the $ticks variable is okay because if I change ">" to "<" in the if statement I see 10M on the axis. But you can probably already guess at this point that my php is somewhere between horrible and non-existent. So maybe it's just some stupid logic flaw on my part?
Appreciate anyone who has any suggestions, thanks in advance.
So what I've tried to do is figure out what the highest value in my data set is, and then customize the ticks based on that. So if the largest value in my data is 12,345,678, I would have custom ticks for 5M, 10M and 15M; if the highest value is 17,890,123 I'd also have 20M, etc.
I've tried using this code to set a $largest variable to the largest number in the data set and then pass that along to view.php:
public function save($data) { $largest = 0; foreach ($data as &$value) { if (is_numeric($value)) if ($value > $largest) $largest = $value; } unset($value); // break the reference with the last element $this -> set('largestnum', $largest); parent::save($data); }
And then reference the code in view.php:
<?php $ticks = '{v:5000000, f:"5M"}'; if($largestnum > 5000000) $ticks .= ', {v:10000000, f:"10M"}' ?>
I don't get any errors, the largest number in the data is much higher than 5000000 and I know the addon to the $ticks variable is okay because if I change ">" to "<" in the if statement I see 10M on the axis. But you can probably already guess at this point that my php is somewhere between horrible and non-existent. So maybe it's just some stupid logic flaw on my part?
Appreciate anyone who has any suggestions, thanks in advance.