I am using Lib charts with mysql. i am getting the bar chart properly but there are too many values on x-axis so they are overlapping and another problem is full names on x-axis are not displaying(Like Ana-Adward. Adward is half displayed on graph). i have checked so many settings but i don't know how to solve this issue. if some one knows please help me. Thanks
i had same problem with overlapping, made some changes in library to not show every caption.
i use vertical bar chart, so in verticalbarchart.php, after "// Draw caption text on bar" insert:
$roznica1 = abs(($value-$prevvalue1)/$value);
$prevvalue1 = $prevvalue;
$roznica = abs(($value-$prevvalue)/$value);
$prevvalue = $value;
if ((($roznica < 0.2 && strlen($value)>3)||($roznica1 < 0.2 && strlen($value)>5)) && $columnWidth<20) $value="";
and before "for ($i = 0; $i < $pointCount; $i++) {" insert:
$prevvalue=0;
$prevvalue1=0;
it's not neat solution but it works!
Related
I'm a junior PHP developer.
For a costumer I need to place some strings into a pdf. I'm using FPDI and I like it.
I have an existing template PDF and I need to insert every characters of a string into a little graphic box (see image).
Every characters must have 2 millimeters (8px approximately) from each others.
Every strings can have different length, so I thought do like this:
$name = 'namenamename';
$stringcount = strlen($name)-1;
$countspace = $stringcount*2;
//121 = coordinate of first box
for ($x=121; $x <= $x+$countspace; $x = $x+2) {
for ($i=0; $i <= $stringcount; $i++) {
$pdf->SetXY($x, 37);
$pdf->Write(0,$name[$i]);
}
}
That doesn't work. This is the error:
Maximum execution time of 30 seconds
Can you help me please with the correct approach and with good explanation for a newbie? :)
Try this code:
$name = 'namenamename';
$string_length = strlen($name);
$coordinate = 121; //Give to the variable coordinate the beginning value, in this case 121
for ($i=0; $i < $string_length; $i++){ //make only one loop for the string length so the loop ends when there is no more characters
$char = substr($name,$i,1); // this is "the tricky part", with substr you can grab each character with its position in the string
$pdf -> SetXY($coordinate, 37); // here you put the coordinate for the character
$pdf -> Write(0, $char); // write it
$coordinate += 2; // and increment it by two, since the character are two spaces away from each other
}
hope that will help..
Maybe not a great solution but you can modify the execution time with this line of code
set_time_limit ( $seconds );
Anyway give it a try but i think that is more an error in the logic of the loop maybe.
Can you say exactly the coordinate where you need the two first characters, the first is 121 + something or 121?
I need to split a big chunk of text into paragraphs of the same size, with the exact same number of string lines to keep them uniforms, i'm currently doing it with php with the following code.
function arrangeText(){
if (have_posts()) : while (have_posts()) : the_post();
$content = strtolower(get_the_content());
$content = trim(strip_tags($content));
$post_len = strlen($content);
$segmentCont = 1;
$auxOffset = 0;
$auxOffset2 = 0;
$limit = 896;
$limitTotal = 0;
while($limitTotal<$post_len){
if($segmentCont==2) {
$limit = 943;
}
$segmentCont++;
$limitTotal = $limitTotal + $limit;
if($limitTotal<$post_len)
{ $auxOffset = strpos($content," ",$limitTotal);}
else
{ $auxOffset = $post_len;}
$post_segment = trim(substr($content,$auxOffset2,$auxOffset-$auxOffset2));
$auxOffset2 = $auxOffset;
echo_segment($post_segment);
}
endwhile;
endif;
}
This actually works but even though im counting each character, some characters may take up to three character spaces, so my paragraphs get different number of lines, or the last line is sliced in half, if i use a monotypefont everything goes smoothly, but my client wants a specific font, and i've been trying all sort of things with no luck, so i come to you the big guys for help. The only thing i can imagine to solve this is a very complex script to get the width of each character im echoing and add that value to a counting variable each time, and do a conditional (inside a loop i guess) to check if the total width of the characters exceeds the total width of my paragraph if so slice it before the last " "(white space) it founds(preventing the code to cut words in half), but i have no idea if this is possible with php, js, jquery or anything that can help me out on a website, im sure there is an easy way to do what i'm trying here but maybe im just not smart enough, so pls help me out =).
Edit 1: My bad, the mono-spaced font won't do the trick either, they were getting cut and overflowing below my p tags ( overflow:hidden ) and i though they were working until i read the text, so i guess that even that they are mono-spaced, their character sizes are different too.
You can use this code to measure widths of characters and create a "dictionary".
You can tweak it to return relative values (lets say w = 1, i = 0.5 so it will be more flexible).
The fiddle is here: http://jsfiddle.net/Gw4AV/
var chars = ["a", "b", "c", "i", "w"];
var widths = [];
for (var i = 0; i < chars.length; i++)
{
var span = $("<span>").text(chars[i]);
$("body").append(span);
widths.push(span.width());
}
alert(widths);
I think a really good idea would be to have a dictionary of Key,Value being Character,Size and then as you iterate through instead of just each character counting as one they count as "Size' (fast lookup with the dictionary). However, this may not work if the characters don't exactly equal a multiple of the other character's size.
What do you think about that?
Hello I am working on a site that uses expedias api. Basically I get a number of people per room, and I want to echo out a little man image for each person. So if I have occupancy of 5 for example and I need to echo 5 tags with the little man as a src. Any idea how to do this??
Well let's say you have the amount of people stored in a variable.
$occupancy = 5;
You can then plug in that number into a for loop, and have the program cycle through that many times.
for($n = 0; $n < $occupancy; $n++) {
// Disco
}
You can read more about control structures here.
You should be interested in str_repeat().
Something like this should work:
$img_multi = str_repeat('<img src="man.png" alt="man"/>', $repeat);
echo $img_multi;
Revisiting this answer, a much more efficient solution:
Assuming the image is 12px wide by 16px high - adjust for your needs.
$width = 12 * $repeat;
$height = 16;
echo '<span style="'
.'display: inline-block;'
.'width: '.$width.'px;'
.'height: '.$height.'px;'
.'background-image: url(man.png);'
.'"></span>';
This will produce a single element of the appropriate size to show $repeat copies of the image, side-by-side.
First result googling "php loops"
Might be worth trying.
I want to display a color between red, yellow, green depending on a number between 1 to 100.
1 being green and 100 being red, 50 being yellow. I want to basically create a gradient between that.
So far, I tried:
$r = floor(255 * ($number / 100));
$g = 255 - $r;
And it somewhat does it, but gives me brownish & dark colors, & no yellow at all.
It's because you shouldn't change both channels at once but rise R in the first half and lower G in the second.
Try a function like this:
function GreenYellowRed($number) {
$number--; // working with 0-99 will be easier
if ($number < 50) {
// green to yellow
$r = floor(255 * ($number / 50));
$g = 255;
} else {
// yellow to red
$r = 255;
$g = floor(255 * ((50-$number%50) / 50));
}
$b = 0;
return "$r,$g,$b";
}
To test it:
$output = "";
for ($i = 1; $i <= 100; $i++) {
$rgb = GreenYellowRed($i);
$output .= "<div style='background-color: rgb($rgb)'>$rgb</div>";
}
echo $output;
I've found that dealing with the HSV color model is easier than the RGB model. It helps you easily choose the color you want to work with; with RGB you'd need to understand how different values of R, G and B will combine to give you the color you want/don't want.
Also, this SO question might be useful: How can I cycle through hex color codes in PHP?
I don't know of a mathematical model for a "color curve" that passes through specified RGB color values (e.g. what you describe as green/yellow/red), which would allow you to calculate any intermediate color in that curve. In any case, a model of a function (which is what that would be) is only as good as the data points it needs to fit, so you 'd have to be much more specific than green/yellow/red to get decent results even if someone points out the math.
Remember that we are not interested in mathematical interpolation here, but rather in "color-space interpolation" (a term which I just made up) -- in other words, what would look like a "natural" interpolation to a human.
An easier solution for those of us who do not have the necessary color theory knowledge, and which I 'd suggest, is to pre-select a number of colors with a color picker tool, divide the 0-100 range into as many bands as the colors you picked, and use simple integer division to project from 0-100 to a color band.
Food for thought: Indeed, how does SO decide the color of the upvote count for comments?
Update: I just asked the above over on meta. Let's see...
After a bit of looking, none of the solutions looked pleasing. As stated above, HSV is probably the way to go, since modern browsers can render color with it just fine.
To get a good idea of the colors you are working with, check out this color wheel:
http://www.colorspire.com/rgb-color-wheel/
I want to start with blue, so I use 255 for normalization.
function temp_color($temp){
$start = 40;
$end = 85;
$normal = round(255-((($temp - $start)/($end-$start))*255));
$color = "hsl($normal, 100%, 30%);";
$span = "<span style=\"color: $color\">$temp</span>";
return $span;
}
I have a problem drawing different functions with PHP (GD, of course).
I managed to draw different functions but whenever the parameters of the function change - the function floats wherever it wants.
Let us say that I have a first function y=x^2 and I have to draw it from -5 to 5. This means that the first point would be at (-5;25). And I can move that to whatever point I want if I know that. But if I choose y=2x^2 with an interval x=(-5;5). The first point is at (-5;50). So I need help in calculating how to move any function to, let's say, (0;0).
The functions are parabola/catenary alike.
What you want to do is find the maximum boundaries of the graph you are making. To do this you have to check each inflection point as well as the range bounds. Store each coordinate pair in an array
Part 1 [Range Bounds]:
Collect the coordinates from the range bounds.
<?php
$ybound[] = f($minX);
$ybound[] = f($maxX);
Part 2 [Inflections]:
This part is more difficult. You can either have a series of equations to solve for inflections for each type of parabola, or you can just brute force it. To do this, just choose a small increment, (what ever your small increment is for drawing the line), I will use 0.1
<?php
for($x = $minX; $x <= $maxX; $x += 0.1) {
$ybound[] = f($x);
}
Note, if you brute force, you can skip Part 1, otherwise, it would be faster if you could figure out the inflections for the scope of your project
Part 3 [Min Max]:
Now you get the min and max values from the array of possible y values.
<?php
$minY = min($ybound);
$maxY = max($ybound);
Part 4 [Shift]:
Now that you have this, it should be very simple to adjust. You take the top left corner and set that to 0,0 by adjusting each new coordinate to that value.
<?php
$shiftX = -$minX;
$shiftY = $maxY;
With this info, you can also determine your image size
<?php
$imageX = $maxX - $minX;
$imageY = $maxY - $minY;
Then as you generate your coordinates, you will shift each one, by adding the shift value to the coordinate.
<?php
for($x = -$minX; $x <= $maxX; $x += 0.1) {
$ycoor = $shiftY - f($x);
$xcoor = $x + $shiftX;
//draw ...
}
Drawing the axis is also easy,
<?php
$xaxis = $shiftY;
$yaxis = $shiftX;
(I think I have all my signs correct. Forgive me if they are off)
You first need to determine the bounding box of your function. Then, you calculate the width and the height, and you normalize so it fits into a rectangle whose top left coordinate is (0,0). Maybe you will also need to scale the figure to get it at a specific size.